Spring mvc與數據庫的前后端的連接
springboot是基于maven的基礎上管理jar包的,只不過是使用springboot下載jar包只需選中即可,就會自動的在pom.xml文件中配置組件
在pom文件中的jar包的快捷鍵:右鍵--->generate---->depency---->搜索jar包
如果在前后端傳參數是輸入了參數卻返回null , 則說明屬性的名字(id,name等)寫錯了
啟動類:注意 ,啟動類必須在啟動類中進行執行.必能在idea的上面進行啟動,否則會啟動其他的啟動類導致報錯
1
2
3
4
5
6
7
8
9
10
11
|
package cn.tedu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; //啟動類 @SpringBootApplication public class RunApp { public static void main(String[] args) { SpringApplication.run(RunApp. class ); } } |
創建car類(相當于model層)
注意:這里使用的是構造方法 主要的作用是方便new
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
package cn.tedu.pojo; //Model用來封裝數據 public class Car { private int id; private String name; private double price; //Constructor構造方法,用來方便的new public Car(){} public Car( int id, String name, double price) { this .id = id; this .name = name; this .price = price; } public int getId() { return id; } public void setId( int id) { this .id = id; } public String getName() { return name; } public void setName(String name) { this .name = name; } public double getPrice() { return price; } public void setPrice( double price) { this .price = price; } } |
使用三種方式 < 對象 > 進行傳參數;注意:使用此類型進行設置值必須有構造方法
對象的地址值:http://localhost:8080/car/get
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package cn.tedu.controller; //MVC里的C層,用來接受請求和做出響應(springmvc) import cn.tedu.pojo.Car; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController //接受請求,并把json數據返回 @RequestMapping ( "car" ) //規定了url地址的寫法 public class CarController { //方式一值會在網頁中出現 @RequestMapping ( "get" ) public Car get(){ Car c = new Car( 10 , "BMW" , 19.9 ); //出發鉤造函數,此處觸發的是含參構造; return c ; } //方式二值會在網頁中出現 @RequestMapping ( "save3" ) public Car save() { car.setAge( 213 ); car.setSex( "男" ); car.setId( 32 ); car.setPrice( 32 ); return car; } 方式三這種方式的值會在idea中打印不會再網頁中出現 @RequestMapping ( "save3" ) public Car save() { car.setAge( 213 ); car.setSex( "男" ); car.setId( 32 ); car.setPrice( 32 ); System.out.println(car); } |
使用return(值會網頁中出現)的方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package cn.tedu.controller; import cn.tedu.pojo.Car; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.naming.Name; import java.net.URL; import java.util.HashMap; import java.util.Map; //這是一個c層用來接收請求和做出響應 @RestController //@RequestMapping("car")//規定了url的寫法此時的值可以任意寫 public class Controller { @RequestMapping ( "replace" ) public String replace(){ // System.out.println(id+name+age); return "hkjds" ; } //方式二值會在網頁中出現 @RequestMapping ( "save3" ) public Car save() { car.setAge( 213 ); car.setSex( "男" ); car.setId( 32 ); car.setPrice( 32 ); return car; } } } |
使用普通的get的方法進行上傳
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
package cn.tedu.controller; import cn.tedu.pojo.Car; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.naming.Name; import java.net.URL; import java.util.HashMap; import java.util.Map; //這是一個c層用來接收請求和做出響應 @RestController //@RequestMapping("car")//規定了url的寫法此時的值可以任意寫 public class Controller { @RequestMapping ( "get2" ) public void get(Integer id,String name){ //此處使用int類型必須賦值 引用類型不用必須賦值最好使用引用類型 System.out.println(id+name); } @RequestMapping ( "get" ) public void get(Integer id){ //此處使用int類型必須賦值 引用類型不用必須賦值 System.out.println(id); } |
restful風格進行傳參數
restful和普通的get的方法的區別:restful相對比較安全,寫法比較簡單
restful的地址值的:http://localhost:8080/car2/get2/10/jack/9
其他的url地址值://http://localhost:8080/car/get5?id=10&name=jack&price=9.9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package cn.tedu.controller; import cn.tedu.pojo.Car; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping ( "car3" ) //使用restful風格 public class CarController { @RequestMapping ( "get2/{sex}/{id}/{name}" ) //此地方的參數順序必須和下面以及地址值都必須一樣 public void get2( @PathVariable String sex, @PathVariable Integer id, @PathVariable String name){ System.out.println( "數據插入成功" +sex+name+id); // System.out.println("數據插入成功"+name+id); } } |
spring mvc框架進行傳參數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package cn.tedu.controller; import cn.tedu.pojo.Car; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.naming.Name; import java.net.URL; import java.util.HashMap; import java.util.Map; //這是一個c層用來接收請求和做出響應 @RestController //@RequestMapping("car")//規定了url的寫法此時的值可以任意寫 public class Controller { //使用框架接收網站參數 @RequestMapping ( "get3" ) public void get3(Car car){ System.out.println(car.getSex()+car.getName()+car.getId()); } } |
前后端參數傳入并且將數據傳入到數據庫中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package cn.tedu.controller; import cn.tedu.pojo.Car; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.yaml.snakeyaml.events.Event; import javax.naming.Name; import java.sql.*; import java.util.Scanner; @RestController @RequestMapping ( "user" ) public class UserContoller { @RequestMapping ( "save" ) public void save(Integer id,String name,Integer age) throws Exception { System.out.println(id+name+age); Class.forName( "com.mysql.jdbc.Driver" ); //獲取連接 String url = "jdbc:mysql:///cgb2104?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai" ; Connection conn = DriverManager.getConnection(url, "root" , "root" ); //獲取傳輸器 // String sql= "insert into user(id,name) values(?,?)";//給指定的字段設置值 String sql= "insert into user values(?,?,?)" ; //所有字段設置值 PreparedStatement ps = conn.prepareStatement(sql); //給SQL設置參數 ps.setInt( 1 ,id); //給第一個?設置值 ps.setString( 2 ,name); //給第二個?設置值 ps.setInt( 3 ,age); //給第三個?設置值 //執行SQL int rows = ps.executeUpdate(); //釋放資源 -- OOM(OutOfMemory) ps.close(); conn.close(); } |
到此這篇關于Spring mvc是如何實現與數據庫的前后端的連接操作的?的文章就介紹到這了,更多相關Spring mvc與數據庫的前后端的連接內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_58276266/article/details/117932321