dao接口:
package com.wzy.jdbctemplate.dao;import com.wzy.jdbctemplate.pojo.Book;import java.util.List;public interface BookDao {void deleteBookByList(List<Object[]> list);}
dao接口的实现类:
package com.wzy.jdbctemplate.dao.impl;import com.wzy.jdbctemplate.dao.BookDao;import com.wzy.jdbctemplate.pojo.Book;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.Arrays;import java.util.List;@Repository//创建对象public class BookDaoImpl implements BookDao {@Autowired//自动装配JdbcTemplate jdbcTemplate;@Overridepublic void deleteBookByList(List<Object[]> list) {String sql = "delete from book where id=?";int[] ints = jdbcTemplate.batchUpdate(sql, list);System.out.println(Arrays.toString(ints));}}
service层:
package com.wzy.jdbctemplate.service;import com.wzy.jdbctemplate.dao.BookDao;import com.wzy.jdbctemplate.pojo.Book;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import java.util.List;@Service//创建对象public class BookService {@Autowired//自动装配private BookDao bookDao;public void deleteList(List<Object[]> list){bookDao.deleteBookByList(list);}}
测试类:
package com.wzy.jdbctemplate.test;import com.wzy.jdbctemplate.pojo.Book;import com.wzy.jdbctemplate.service.BookService;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import java.util.ArrayList;import java.util.Collections;import java.util.List;import static org.junit.Assert.*;public class BookServiceTest {ApplicationContext applicationContext =new ClassPathXmlApplicationContext("com/wzy/jdbctemplate/spring.xml");BookService bookService = applicationContext.getBean("bookService",BookService.class);@Testpublic void deleteList(){List<Object[]> list = new ArrayList<>();Object[] obj1 = {1};Object[] obj2 = {2};Object[] obj3 = {3};Collections.addAll(list,obj1,obj2,obj3);bookService.deleteList(list);}}
