1.背景
1、什么是JdbcTemplate
(1)Spring框架对JDBC进行封装,使用JdbcTemplate方便实现对数据库操作
2.使用的环境准备
(1)引入相关jar包
2.编写spring配置文件bean01.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!--1.组件扫描-->
<context:component-scan base-package="com.ldp.jdbctemplate"></context:component-scan>
<!--2.数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ldp-data?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
</bean>
<!--3.JdbcTemplate对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
3.model、dao接口、dao实现、dao测试结构搭建
model
dao接口
package com.ldp.jdbctemplate.dao;
import com.ldp.jdbctemplate.model.UserOrder;
import java.util.List;
/**
* @Copyright (C) XXXXXXXXXXX科技股份技有限公司
* @Author: lidongping
* @Date: 2021-01-15 9:43
* @Description:
*/
public interface IUserOrderDao {
/**
* @param userOrder
* @return
*/
int save(UserOrder userOrder);
/**
* @param id
* @return
*/
int delete(Integer id);
/**
* @param userOrder
* @return
*/
int update(UserOrder userOrder);
/**
*
* @param id
* @return
*/
UserOrder queryById(Integer id);
/**
*
* @return
*/
List<UserOrder> queryList();
/**
* @return
*/
int count();
}
dao实现(结构而已,还没具体写内部实现,准备好,好使用spring的jdbcTemplate实现)
package com.ldp.jdbctemplate.dao.impl;
import com.ldp.jdbctemplate.dao.IUserOrderDao;
import com.ldp.jdbctemplate.model.UserOrder;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Copyright (C) XXXXXXXXXXX科技股份技有限公司
* @Author: lidongping
* @Date: 2021-01-15 9:46
* @Description:
*/
@Repository
public class UserOrderDaoImpl implements IUserOrderDao {
@Override
public int save(UserOrder userOrder) {
return 0;
}
@Override
public int delete(Integer id) {
return 0;
}
@Override
public int update(UserOrder userOrder) {
return 0;
}
@Override
public UserOrder queryById(Integer id) {
return null;
}
@Override
public List<UserOrder> queryList() {
return null;
}
@Override
public int count() {
return 0;
}
}
dao测试
package com.ldp.jdbctemplate.dao.impl;
import com.ldp.jdbctemplate.dao.IUserOrderDao;
import com.ldp.jdbctemplate.model.UserOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import java.util.List;
/**
* @Copyright (C) XXXXXXXXXXX科技股份技有限公司
* @Author: lidongping
* @Date: 2021-01-15 9:47
* @Description:
*/
@SpringJUnitConfig(locations = "classpath:bean01.xml")
public class UserOrderDaoImplTest {
@Autowired
private IUserOrderDao userOrderDao;
@Test
public void save() {
UserOrder userOrder = new UserOrder();
userOrder.setOrderNo("NO1001");
userOrder.setProductName("苹果");
int result = userOrderDao.save(userOrder);
System.out.println("受影响行数:" + result);
}
@Test
public void delete() {
int result = userOrderDao.delete(2);
System.out.println("受影响行数:" + result);
}
@Test
public void update() {
UserOrder userOrder = new UserOrder();
userOrder.setId(2);
userOrder.setProductName("苹果-修改");
int result = userOrderDao.save(userOrder);
System.out.println("受影响行数:" + result);
}
@Test
public void queryById() {
UserOrder userOrder = userOrderDao.queryById(2);
System.out.println("查询结果:" + userOrder);
}
@Test
public void queryList() {
List<UserOrder> userOrders = userOrderDao.queryList();
System.out.println("查询结果:" + userOrders);
}
@Test
public void count() {
int count = userOrderDao.count();
System.out.println("总共条数:" + count);
}
}
3.jdbcTemplate实现增删改查
首先注入jdbcTemplate
增删改查具体实现
package com.ldp.jdbctemplate.dao.impl;
import com.ldp.jdbctemplate.dao.IUserOrderDao;
import com.ldp.jdbctemplate.model.UserOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* @Copyright (C) XXXXXXXXXXX科技股份技有限公司
* @Author: lidongping
* @Date: 2021-01-15 9:46
* @Description:
*/
@Repository
public class UserOrderDaoImpl implements IUserOrderDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int save(UserOrder userOrder) {
// 建议在mysql的可视化客户端(Navicat)写好sql后复制到这里
String sql = "INSERT INTO user_order (order_no,product_name) VALUES (?,?)";
return jdbcTemplate.update(sql, userOrder.getOrderNo(), userOrder.getProductName());
}
@Override
public int delete(Integer id) {
String sql = "DELETE FROM user_order WHERE id=?";
return jdbcTemplate.update(sql, id);
}
@Override
public int update(UserOrder userOrder) {
String sql = "UPDATE user_order SET product_name=? WHERE id=?";
return jdbcTemplate.update(sql, userOrder.getProductName(), userOrder.getId());
}
@Override
public UserOrder queryById(Integer id) {
String sql = "SELECT id,order_no orderNo,product_name productName FROM user_order WHERE id=?";
return jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<UserOrder>(UserOrder.class), id);
}
@Override
public List<UserOrder> queryList() {
String sql = "SELECT id,order_no,product_name FROM user_order";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<UserOrder>(UserOrder.class));
}
@Override
public int count() {
String sql = "SELECT count(1) num FROM user_order";
return jdbcTemplate.queryForObject(sql, Integer.class);
}
}
4.jdbcTemplate批量操作
1.批量增加、批量删除、批量修改代码实现
@Override
public int batchSave(List<UserOrder> userOrders) {
// 建议在mysql的可视化客户端(Navicat)写好sql后复制到这里
String sql = "INSERT INTO user_order (order_no,product_name) VALUES (?,?)";
// 批量添加测试
List<Object[]> batchArgs = new ArrayList<>();
for (UserOrder userOrder : userOrders) {
Object[] o = {userOrder.getOrderNo(), userOrder.getProductName()};
batchArgs.add(o);
}
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
return ints.length;
}
@Override
public int batchDelete(List<Integer> ids) {
String sql = "DELETE FROM user_order WHERE id=?";
List<Object[]> batchArgs = new ArrayList<>();
for (Integer id : ids) {
Object[] object = {id};
batchArgs.add(object);
}
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
return ints.length;
}
@Override
public int batchUpdate(List<UserOrder> userOrders) {
// 建议在mysql的可视化客户端(Navicat)写好sql后复制到这里
String sql = "UPDATE user_order SET product_name=? WHERE id=?";
List<Object[]> batchArgs = new ArrayList<>();
for (UserOrder userOrder : userOrders) {
Object[] o = {userOrder.getProductName(), userOrder.getId()};
batchArgs.add(o);
}
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
return ints.length;
}
2.批量增加、批量删除、批量修改代码测试
@Test
public void batchSave() {
List<UserOrder> list = new ArrayList<>();
for (int i = 0; i < 5; i++) {
UserOrder userOrder = new UserOrder();
userOrder.setOrderNo("NO1001-" + i);
userOrder.setProductName("产品名称-" + i);
list.add(userOrder);
}
int count = userOrderDao.batchSave(list);
System.out.println("受影响行数:" + count);
}
@Test
public void batchDelete() {
List<Integer> list = new ArrayList<>();
list.add(4);
list.add(5);
list.add(6);
int count = userOrderDao.batchDelete(list);
System.out.println("受影响行数:" + count);
}
@Test
public void batchUpdate() {
List<UserOrder> list = new ArrayList<>();
for (int i = 8; i <= 12; i++) {
UserOrder userOrder = new UserOrder();
userOrder.setId(i);
userOrder.setProductName("修改-产品名称-" + i);
list.add(userOrder);
}
int count = userOrderDao.batchUpdate(list);
System.out.println("受影响行数:" + count);
}