【Spring】JdbcTemplate:批量操作

public interface BookDao {
	public void add(Book book);

	public void delete(String id);

	public void myupdate(Book book);

	public int findSelect();

	public Book findObjectSelect(String id);

	public List<Book> findListBook();

	public void batchAdd(List<Object[]> batchArgs);

	public void batchUpdate(List<Object[]> batchArgs);

	public void batchDelete(List<Object[]> batchArgs);
}
 
@Repository
public class BookDaoImpl implements BookDao {
    @Autowired  //注入jdbcTemplate
    private JdbcTemplate jdbcTemplate;
    
    public void add(Book book) {  //用jdbcTemplate做添加
    	//创建SQL语句
    	String sql="insert into t_book values(?,?,?)";  //id非自增时
    	//调用方法实现
    	int update=jdbcTemplate.update(sql,  
    			book.getUserId(),book.getUsername(),book.getUstatus());
    	System.out.println("添加成功:"+update);
    }//因1个javabean代表1个记录、返影响行数update

	@Override
	public void myupdate(Book book) {
		String sql="update t_book set username=?,ustatus=? where user_id=?"; 
		int update=jdbcTemplate.update(sql,  
    			book.getUsername(),book.getUstatus(),book.getUserId());
		System.out.println("修改成功:"+update);
		
		
	}

	@Override
	public void delete(String id) {
		String sql="delete from t_book where user_id=?"; 
		int update=jdbcTemplate.update(sql,  id);
		System.out.println("删除成功:"+update);
		
	}

	@Override
	public int findSelect() {
		String sql="select count(*) from t_book"; 
		int count=jdbcTemplate.queryForObject(sql, Integer.class);
		return count;
	}

	@Override
	public Book findObjectSelect(String id) {
		String sql="select * from t_book where user_id=?"; 
		Book book=jdbcTemplate.queryForObject(
				sql, new BeanPropertyRowMapper<Book>(Book.class), id);
		return book;
	}

	@Override
	public List<Book> findListBook() {
		String sql="select * from t_book"; 
		List<Book> listBook=jdbcTemplate.query(
				sql, new BeanPropertyRowMapper<Book>(Book.class));
		return listBook;
	}

	@Override
	public void batchAdd(List<Object[]> batchArgs) {
    	String sql="insert into t_book values(?,?,?)"; 
		int[] listBook=jdbcTemplate.batchUpdate(sql, batchArgs);  
		System.out.println("批量添加成功:"+Arrays.toString(listBook));
		
	}

	@Override
	public void batchUpdate(List<Object[]> batchArgs) {
		String sql="update t_book set username=?,ustatus=? where user_id=?"; 
		int[] listBook=jdbcTemplate.batchUpdate(sql, batchArgs);
		System.out.println("批量修改成功:"+Arrays.toString(listBook));
		
	}

	@Override
	public void batchDelete(List<Object[]> batchArgs) {
		String sql="delete from t_book where user_id=?"; 
		int[] listBook=jdbcTemplate.batchUpdate(sql, batchArgs);
		System.out.println("批量删除成功:"+Arrays.toString(listBook));
		
	}
	 
	
    
    
    
}





public class TestBook {


    @Test
    public void testAccount() {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("bean1.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
            //批量删除:据各id值来删除各条记录
            List<Object[]> batchArgs=new ArrayList<>();
            Object [] data1= {"6"}; 
            Object [] data2= {"8"}; 
            Object [] data3= {"10"};
            batchArgs.add(data1);
            batchArgs.add(data2);
            batchArgs.add(data3);
            bookService.batchDelete(batchArgs);
    }

}
import javax.annotation.Resource;
@Service
public class BookService {
    //注入dao
    @Autowired
    private BookDao userDao;  //创建dx
    //批量删除
    public void batchDelete(List<Object[]> batchArgs) {
    	userDao.batchDelete(batchArgs); 
    }
}
<?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"
       xmlns:tx="http://www.springframework.org/schema/tx"
       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
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 组件扫描 -->
    <context:component-scan base-package="com.test"></context:component-scan>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql://localhost:3306/spring" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>

    <!-- JdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值