spring-210728-04—JdbcTemplate操作数据库-批量添加&修改&删除
演示
UserDao03.java(dao层接口)
package com.bgy.spring.dao;
import java.util.List;
public interface UserDao03 {
void batchAddUser(List<Object[]> batchArgs);
void batchUpdateUser(List<Object[]> batchArgs);
void batchDeleteUser(List<Object[]> batchArgs);
}
UserDaoImpl03.java(这里实现DML操作)
package com.bgy.spring.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserDaoImpl03 {
@Autowired
private UserDao03 userDao03;
public void batchAdd(List<Object[]> batchArgs){
userDao03.batchAddUser(batchArgs);
}
public void batchUpdate(List<Object[]> batchArgs){
userDao03.batchUpdateUser(batchArgs);
}
public void batchDelete(List<Object[]> batchArgs){
userDao03.batchDeleteUser(batchArgs);
}
}
UserService03.java(这里调用dao)
package com.bgy.spring.service;
import com.bgy.spring.dao.UserDao03;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.Arrays;
import java.util.List;
@Repository
public class UserService03 implements UserDao03 {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void batchAddUser(List<Object[]> batchArgs) {
String sql = "insert into t_user values(?,?,?)";
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(Arrays.toString(ints));
}
@Override
public void batchUpdateUser(List<Object[]> batchArgs) {
String sql = "update t_user set username=?,ustatus=? where user_id=?";
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(Arrays.toString(ints));
}
@Override
public void batchDeleteUser(List<Object[]> batchArgs) {
String sql = "delete from t_user where user_id=?";
int[] ints = jdbcTemplate.batchUpdate(sql, batchArgs);
System.out.println(Arrays.toString(ints));
}
}
TestUser03.java(测试)
import com.bgy.spring.service.UserService03;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.ArrayList;
import java.util.List;
public class TestUser03 {
@Test
public void test01(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
UserService03 userService03 = context.getBean("userService03", UserService03.class);
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 = {"1111","裴擒虎","打野"};
Object[] o2 = {"2222","西施","中路"};
Object[] o3 = {"3333","狂铁","对抗路"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
userService03.batchAddUser(batchArgs);
}
@Test
public void test02(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
UserService03 userService03 = context.getBean("userService03", UserService03.class);
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 = {"裴擒虎","打野3-0","1111"};
Object[] o2 = {"西施","中路1-1","2222"};
Object[] o3 = {"狂铁","对抗路2-1","3333"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
userService03.batchUpdateUser(batchArgs);
}
@Test
public void test03(){
ApplicationContext context = new ClassPathXmlApplicationContext("bean01.xml");
UserService03 userService03 = context.getBean("userService03", UserService03.class);
List<Object[]> batchArgs = new ArrayList<>();
Object[] o1 = {"1111"};
Object[] o2 = {"2222"};
Object[] o3 = {"3333"};
batchArgs.add(o1);
batchArgs.add(o2);
batchArgs.add(o3);
userService03.batchDeleteUser(batchArgs);
}
}
bean01.xml(Spring配置文件)
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="com.bgy.spring"></context:component-scan>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/spring_test_210728_01?serverTimezone=UTC&useSSL=false" />
<property name="username" value="root" />
<property name="password" value="admin" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
User.java(实体类)
package com.bgy.spring.entity;
public class User {
private String userId;
private String username;
private String ustatus;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUstatus() {
return ustatus;
}
public void setUstatus(String ustatus) {
this.ustatus = ustatus;
}
@Override
public String toString() {
return "User{" +
"userId='" + userId + '\'' +
", username='" + username + '\'' +
", ustatus='" + ustatus + '\'' +
'}';
}
}
|