本篇内容主要讲解“spring mvc实现与数据库的前后端的连接操作的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring mvc实现与数据库的前后端的连接操作的方法”吧!Spring
本篇内容主要讲解“spring mvc实现与数据库的前后端的连接操作的方法”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Spring mvc实现与数据库的前后端的连接操作的方法”吧!
SpringBoot是基于Maven的基础上管理jar包的,只不过是使用springboot下载jar包只需选中即可,就会自动的在pom.xml文件中配置组件
在pom文件中的jar包的快捷键:右键--->generate---->depency---->搜索jar包
如果在前后端传参数是输入了参数却返回null , 则说明属性的名字(id,name等)写错了
package cn.tedu; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;//启动类@SpringBootApplicationpublic class RunApp { public static void main(String[] args) { SpringApplication.run(RunApp.class); }}
创建car类(相当于model层)
注意:这里使用的是构造方法 主要的作用是方便new
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
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);}
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; } } }
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和普通的get的方法的区别:restful相对比较安全,写法比较简单
restful的地址值的:http://localhost:8080/car2/get2/10/jack/9
其他的url地址值://http://localhost:8080/car/get5?id=10&name=jack&price=9.9
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); } }
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()); } }
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实现与数据库的前后端的连接操作的方法”有了更深的了解,不妨来实际操作一番吧!这里是编程网网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
--结束END--
本文标题: Spring mvc实现与数据库的前后端的连接操作的方法
本文链接: https://www.lsjlt.com/news/296715.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0