经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringBoot使用JdbcTemplate访问操作数据库基本用法
来源:jb51  时间:2022/2/22 15:52:20  对本文有异议

Spring对数据库的操作在jdbc上s面做了深层次的封装,使用spring的注入功能,可以把DataSource注册到JdbcTemplate之中。

JdbcTemplateSpring-jdbc包下面,还需要Spring-tx包支持,里面包含事务和异常控制.

一、建一个rumenz_springboot库

创建user表:

  1. create table user(
  2. ? id int primary key auto_increment,
  3. ? name varchar(100) not null default '',
  4. ? domain varchar(100) not null default ''
  5. )engine=innodb default charset=utf8;

二、加入pom的依赖

  1. <dependency>
  2. ?? ?<groupId>org.springframework.boot</groupId>
  3. ?? ?<artifactId>spring-boot-starter-jdbc</artifactId>
  4. </dependency>
  5. <dependency>
  6. ?? ?<groupId>mysql</groupId>
  7. ?? ?<artifactId>mysql-connector-java</artifactId>
  8. ?? ?<scope>runtime</scope>
  9. </dependency>

三、SpringBoot配置文件

application.properties

  1. spring.datasource.url=jdbc:mysql://localhost:3306/rumenz_springboot
  2. spring.datasource.username=root
  3. spring.datasource.password=root1234
  4. spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

四、创建User实体类

  1. @Builder
  2. @Data
  3. @AllArgsConstructor
  4. public class User ?implements RowMapper {
  5. ? ? private Integer id;
  6. ? ? private String name;
  7. ? ? private String domain;
  8.  
  9. ? ? @Override
  10. ? ? public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
  11. ? ? ? ? User user=new User();
  12. ? ? ? ? user.setId(rs.getInt("id"));
  13. ? ? ? ? user.setName(rs.getString("name"));
  14. ? ? ? ? user.setDomain(rs.getString("domain"));
  15. ? ? ? ? return user;
  16. ? ? }
  17. }

五、Service接口

UserService.java

  1. package com.rumenz.lession14.controller.service;
  2.  
  3. import com.rumenz.lession14.controller.entity.User;
  4.  
  5. import java.util.List;
  6.  
  7. /**
  8. ?* @className: UserService
  9. ?* @description: TODO 类描述
  10. ?* @author: 入门小站 rumenz.com
  11. ?* @date: 2021/12/13
  12. ?**/
  13. public interface UserService {
  14. ? ? Integer save(User user);
  15. ? ? List<User> list();
  16. ? ? Integer update(User user);
  17.  
  18. ? ? Integer batchSave();
  19. }

六、Service接口实现类

UserServiceImpl.java

  1. package com.rumenz.lession14.controller.service.Impl;
  2.  
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.json.JsonMapper;
  6. import com.rumenz.lession14.controller.entity.User;
  7. import com.rumenz.lession14.controller.service.UserService;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  10. import org.springframework.jdbc.core.JdbcTemplate;
  11. import org.springframework.stereotype.Service;
  12.  
  13. import java.util.ArrayList;
  14. import java.util.List;
  15.  
  16. /**
  17. ?* @className: UserServiceImpl
  18. ?* @description: TODO 类描述
  19. ?* @author: 入门小站 rumenz.com
  20. ?* @date: 2021/12/13
  21. ?**/
  22.  
  23. @Service
  24. public class UserServiceImpl implements UserService {
  25. ? ? @Autowired
  26. ? ? private JdbcTemplate jdbcTemplate;
  27.  
  28. ? ? @Override
  29. ? ? public Integer save(User user) {
  30. ? ? ? ? int reint = jdbcTemplate.update("insert into user(name,domain) values (?,?)", user.getName(), user.getDomain());
  31. ? ? ? ? return reint;
  32. ? ? }
  33.  
  34. ? ? @Override
  35. ? ? public Integer batchSave() {
  36. ? ? ? ? String sql="insert into user(name,domain) values(?,?)";
  37. ? ? ? ? List<Object[]> par=new ArrayList<>();
  38. ? ? ? ? for (int i = 0; i < 10; i++) {
  39. ? ? ? ? ? ? String[] s=new String[2];
  40. ? ? ? ? ? ? s[0]="入门小站"+i;
  41. ? ? ? ? ? ? s[1]="https://rumenz.com/"+i;
  42. ? ? ? ? ? ? par.add(s);
  43. ? ? ? ? }
  44. ? ? ? ? int[] ints = jdbcTemplate.batchUpdate(sql, par);
  45. ? ? ? ? System.out.println(ints.toString());
  46. ? ? ? ? return 0;
  47. ? ? }
  48.  
  49. ? ? @Override
  50. ? ? public List<User> list() {
  51. ? ? ? ? //User实现RowMapper接口,实现接口里的mapRow方法。
  52. ? ? ? ? List<User> list = jdbcTemplate.query("select * from user",new User());
  53. ? ? ? ? return list;
  54. ? ? }
  55.  
  56. ? ? @Override
  57. ? ? public Integer update(User user) {
  58. ? ? ? ? Integer reint=jdbcTemplate.update("update user set name=?,domain=? where id=?", user.getName(),user.getDomain(),user.getId());
  59. ? ? ? ? //
  60. ? ? ? ? return reint;
  61. ? ? }
  62.  
  63.  
  64. }

七、Controller测试

RumenzController.java

  1. package com.rumenz.lession14.controller;
  2.  
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.rumenz.lession14.controller.entity.User;
  6. import com.rumenz.lession14.controller.service.UserService;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.web.bind.annotation.GetMapping;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11.  
  12. import java.util.List;
  13.  
  14. /**
  15. ?* @className: RumenzController
  16. ?* @description: TODO 类描述
  17. ?* @author: 入门小站 rumenz.com
  18. ?* @date: 2021/12/13
  19. ?**/
  20. @RestController
  21. @RequestMapping("/rumenz")
  22. public class RumenzController {
  23.  
  24.  
  25. ? ? @Autowired
  26. ? ? private UserService userService;
  27.  
  28.  
  29. ? ? //添加数据
  30. ? ? @GetMapping("/save")
  31. ? ? public String save(){
  32. ? ? ? ? User user=User.builder().name("入门小站").domain("https://rumenz.com").build();
  33. ? ? ? ? Integer reint = userService.save(user);
  34. ? ? ? ? return reint.toString();
  35. ? ? }
  36.  
  37. ? ? //批量添加数据
  38. ? ? @GetMapping("/batchSave")
  39. ? ? public String batchSave(){
  40. ? ? ? ? Integer reint = userService.batchSave();
  41. ? ? ? ? return reint.toString();
  42. ? ? }
  43.  
  44. ? ? //查询数据
  45. ? ? @GetMapping("/list")
  46. ? ? public String list() throws JsonProcessingException {
  47. ? ? ? ? List<User> list = userService.list();
  48. ? ? ? ? ObjectMapper objectMapper=new ObjectMapper();
  49. ? ? ? ? String val = objectMapper.writeValueAsString(list);
  50. ? ? ? ? return val;
  51. ? ? }
  52. ? ? //更新数据
  53. ? ? @GetMapping("/update")
  54. ? ? public String update() throws JsonProcessingException {
  55. ? ? ? ? User user=User.builder().id(1).name("入门小站-修改").domain("https://tooltt.com").build();
  56. ? ? ? ? Integer reint = userService.update(user);
  57. ? ? ? ? return reint.toString();
  58. ? ? }
  59.  
  60. }

八、总结

常用CURD操作大致使用以下三个方法:

  • 1.execute方法,用于直接执行SQL语句
  • 2.update方法,用户新增修改删除操作
  • 3.query方法,用于查询方法

到此这篇关于SpringBoot使用JdbcTemplate访问操作数据库基本用法的文章就介绍到这了,更多相关SpringBoot使用JdbcTemplate访问操作数据库内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号