服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Spring mvc是如何实现与数据库的前后端的连接操作的?

Spring mvc是如何实现与数据库的前后端的连接操作的?

2021-09-22 11:24黄金龙666 Java教程

今天给大家带来的是关于Spring mvc的相关知识,文章围绕着Spring mvc是如何实现与数据库的前后端的连接操作的展开,文中有非常详细的介绍及代码示例,需要的朋友可以参考下

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&amp;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

延伸 · 阅读

精彩推荐