1.背景

1、什么是JdbcTemplate
(1)Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作

2.使用的环境准备

(1)引入相关jar包
spring之jdbctemplate - 图1
2.编写spring配置文件bean01.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6. <!--1.组件扫描-->
  7. <context:component-scan base-package="com.ldp.jdbctemplate"></context:component-scan>
  8. <!--2.数据库连接池-->
  9. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
  10. <property name="url" value="jdbc:mysql://127.0.0.1:3306/ldp-data?characterEncoding=utf8"/>
  11. <property name="username" value="root"/>
  12. <property name="password" value="admin"/>
  13. <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
  14. </bean>
  15. <!--3.JdbcTemplate对象-->
  16. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  17. <!--注入dataSource-->
  18. <property name="dataSource" ref="dataSource"></property>
  19. </bean>
  20. </beans>

3.model、dao接口、dao实现、dao测试结构搭建
spring之jdbctemplate - 图2
model
spring之jdbctemplate - 图3
dao接口

  1. package com.ldp.jdbctemplate.dao;
  2. import com.ldp.jdbctemplate.model.UserOrder;
  3. import java.util.List;
  4. /**
  5. * @Copyright (C) XXXXXXXXXXX科技股份技有限公司
  6. * @Author: lidongping
  7. * @Date: 2021-01-15 9:43
  8. * @Description:
  9. */
  10. public interface IUserOrderDao {
  11. /**
  12. * @param userOrder
  13. * @return
  14. */
  15. int save(UserOrder userOrder);
  16. /**
  17. * @param id
  18. * @return
  19. */
  20. int delete(Integer id);
  21. /**
  22. * @param userOrder
  23. * @return
  24. */
  25. int update(UserOrder userOrder);
  26. /**
  27. *
  28. * @param id
  29. * @return
  30. */
  31. UserOrder queryById(Integer id);
  32. /**
  33. *
  34. * @return
  35. */
  36. List<UserOrder> queryList();
  37. /**
  38. * @return
  39. */
  40. int count();
  41. }

dao实现(结构而已,还没具体写内部实现,准备好,好使用spring的jdbcTemplate实现)

  1. package com.ldp.jdbctemplate.dao.impl;
  2. import com.ldp.jdbctemplate.dao.IUserOrderDao;
  3. import com.ldp.jdbctemplate.model.UserOrder;
  4. import org.springframework.stereotype.Repository;
  5. import java.util.List;
  6. /**
  7. * @Copyright (C) XXXXXXXXXXX科技股份技有限公司
  8. * @Author: lidongping
  9. * @Date: 2021-01-15 9:46
  10. * @Description:
  11. */
  12. @Repository
  13. public class UserOrderDaoImpl implements IUserOrderDao {
  14. @Override
  15. public int save(UserOrder userOrder) {
  16. return 0;
  17. }
  18. @Override
  19. public int delete(Integer id) {
  20. return 0;
  21. }
  22. @Override
  23. public int update(UserOrder userOrder) {
  24. return 0;
  25. }
  26. @Override
  27. public UserOrder queryById(Integer id) {
  28. return null;
  29. }
  30. @Override
  31. public List<UserOrder> queryList() {
  32. return null;
  33. }
  34. @Override
  35. public int count() {
  36. return 0;
  37. }
  38. }

dao测试

  1. package com.ldp.jdbctemplate.dao.impl;
  2. import com.ldp.jdbctemplate.dao.IUserOrderDao;
  3. import com.ldp.jdbctemplate.model.UserOrder;
  4. import org.junit.jupiter.api.Test;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
  7. import java.util.List;
  8. /**
  9. * @Copyright (C) XXXXXXXXXXX科技股份技有限公司
  10. * @Author: lidongping
  11. * @Date: 2021-01-15 9:47
  12. * @Description:
  13. */
  14. @SpringJUnitConfig(locations = "classpath:bean01.xml")
  15. public class UserOrderDaoImplTest {
  16. @Autowired
  17. private IUserOrderDao userOrderDao;
  18. @Test
  19. public void save() {
  20. UserOrder userOrder = new UserOrder();
  21. userOrder.setOrderNo("NO1001");
  22. userOrder.setProductName("苹果");
  23. int result = userOrderDao.save(userOrder);
  24. System.out.println("受影响行数:" + result);
  25. }
  26. @Test
  27. public void delete() {
  28. int result = userOrderDao.delete(2);
  29. System.out.println("受影响行数:" + result);
  30. }
  31. @Test
  32. public void update() {
  33. UserOrder userOrder = new UserOrder();
  34. userOrder.setId(2);
  35. userOrder.setProductName("苹果-修改");
  36. int result = userOrderDao.save(userOrder);
  37. System.out.println("受影响行数:" + result);
  38. }
  39. @Test
  40. public void queryById() {
  41. UserOrder userOrder = userOrderDao.queryById(2);
  42. System.out.println("查询结果:" + userOrder);
  43. }
  44. @Test
  45. public void queryList() {
  46. List<UserOrder> userOrders = userOrderDao.queryList();
  47. System.out.println("查询结果:" + userOrders);
  48. }
  49. @Test
  50. public void count() {
  51. int count = userOrderDao.count();
  52. System.out.println("总共条数:" + count);
  53. }
  54. }

3.jdbcTemplate实现增删改查

首先注入jdbcTemplate
spring之jdbctemplate - 图4
增删改查具体实现

  1. package com.ldp.jdbctemplate.dao.impl;
  2. import com.ldp.jdbctemplate.dao.IUserOrderDao;
  3. import com.ldp.jdbctemplate.model.UserOrder;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.jdbc.core.BeanPropertyRowMapper;
  6. import org.springframework.jdbc.core.JdbcTemplate;
  7. import org.springframework.stereotype.Repository;
  8. import java.util.List;
  9. /**
  10. * @Copyright (C) XXXXXXXXXXX科技股份技有限公司
  11. * @Author: lidongping
  12. * @Date: 2021-01-15 9:46
  13. * @Description:
  14. */
  15. @Repository
  16. public class UserOrderDaoImpl implements IUserOrderDao {
  17. @Autowired
  18. private JdbcTemplate jdbcTemplate;
  19. @Override
  20. public int save(UserOrder userOrder) {
  21. // 建议在mysql的可视化客户端(Navicat)写好sql后复制到这里
  22. String sql = "INSERT INTO user_order (order_no,product_name) VALUES (?,?)";
  23. return jdbcTemplate.update(sql, userOrder.getOrderNo(), userOrder.getProductName());
  24. }
  25. @Override
  26. public int delete(Integer id) {
  27. String sql = "DELETE FROM user_order WHERE id=?";
  28. return jdbcTemplate.update(sql, id);
  29. }
  30. @Override
  31. public int update(UserOrder userOrder) {
  32. String sql = "UPDATE user_order SET product_name=? WHERE id=?";
  33. return jdbcTemplate.update(sql, userOrder.getProductName(), userOrder.getId());
  34. }
  35. @Override
  36. public UserOrder queryById(Integer id) {
  37. String sql = "SELECT id,order_no orderNo,product_name productName FROM user_order WHERE id=?";
  38. return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<UserOrder>(UserOrder.class), id);
  39. }
  40. @Override
  41. public List<UserOrder> queryList() {
  42. String sql = "SELECT id,order_no,product_name FROM user_order";
  43. return jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserOrder>(UserOrder.class));
  44. }
  45. @Override
  46. public int count() {
  47. String sql = "SELECT count(1) num FROM user_order";
  48. return jdbcTemplate.queryForObject(sql, Integer.class);
  49. }
  50. }

测试:执行上面的dao测试方法即可

4.jdbcTemplate批量操作

1.批量增加、批量删除、批量修改代码实现

  1. @Override
  2. public int batchSave(List<UserOrder> userOrders) {
  3. // 建议在mysql的可视化客户端(Navicat)写好sql后复制到这里
  4. String sql = "INSERT INTO user_order (order_no,product_name) VALUES (?,?)";
  5. // 批量添加测试
  6. List<Object[]> batchArgs = new ArrayList<>();
  7. for (UserOrder userOrder : userOrders) {
  8. Object[] o = {userOrder.getOrderNo(), userOrder.getProductName()};
  9. batchArgs.add(o);
  10. }
  11. int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
  12. return ints.length;
  13. }
  14. @Override
  15. public int batchDelete(List<Integer> ids) {
  16. String sql = "DELETE FROM user_order WHERE id=?";
  17. List<Object[]> batchArgs = new ArrayList<>();
  18. for (Integer id : ids) {
  19. Object[] object = {id};
  20. batchArgs.add(object);
  21. }
  22. int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
  23. return ints.length;
  24. }
  25. @Override
  26. public int batchUpdate(List<UserOrder> userOrders) {
  27. // 建议在mysql的可视化客户端(Navicat)写好sql后复制到这里
  28. String sql = "UPDATE user_order SET product_name=? WHERE id=?";
  29. List<Object[]> batchArgs = new ArrayList<>();
  30. for (UserOrder userOrder : userOrders) {
  31. Object[] o = {userOrder.getProductName(), userOrder.getId()};
  32. batchArgs.add(o);
  33. }
  34. int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
  35. return ints.length;
  36. }

2.批量增加、批量删除、批量修改代码测试

  1. @Test
  2. public void batchSave() {
  3. List<UserOrder> list = new ArrayList<>();
  4. for (int i = 0; i < 5; i++) {
  5. UserOrder userOrder = new UserOrder();
  6. userOrder.setOrderNo("NO1001-" + i);
  7. userOrder.setProductName("产品名称-" + i);
  8. list.add(userOrder);
  9. }
  10. int count = userOrderDao.batchSave(list);
  11. System.out.println("受影响行数:" + count);
  12. }
  13. @Test
  14. public void batchDelete() {
  15. List<Integer> list = new ArrayList<>();
  16. list.add(4);
  17. list.add(5);
  18. list.add(6);
  19. int count = userOrderDao.batchDelete(list);
  20. System.out.println("受影响行数:" + count);
  21. }
  22. @Test
  23. public void batchUpdate() {
  24. List<UserOrder> list = new ArrayList<>();
  25. for (int i = 8; i <= 12; i++) {
  26. UserOrder userOrder = new UserOrder();
  27. userOrder.setId(i);
  28. userOrder.setProductName("修改-产品名称-" + i);
  29. list.add(userOrder);
  30. }
  31. int count = userOrderDao.batchUpdate(list);
  32. System.out.println("受影响行数:" + count);
  33. }

完美!