dao接口:

    1. package com.wzy.jdbctemplate.dao;
    2. import com.wzy.jdbctemplate.pojo.Book;
    3. import java.util.List;
    4. public interface BookDao {
    5. void deleteBookByList(List<Object[]> list);
    6. }

    dao接口的实现类:

    1. package com.wzy.jdbctemplate.dao.impl;
    2. import com.wzy.jdbctemplate.dao.BookDao;
    3. import com.wzy.jdbctemplate.pojo.Book;
    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.Arrays;
    9. import java.util.List;
    10. @Repository//创建对象
    11. public class BookDaoImpl implements BookDao {
    12. @Autowired//自动装配
    13. JdbcTemplate jdbcTemplate;
    14. @Override
    15. public void deleteBookByList(List<Object[]> list) {
    16. String sql = "delete from book where id=?";
    17. int[] ints = jdbcTemplate.batchUpdate(sql, list);
    18. System.out.println(Arrays.toString(ints));
    19. }
    20. }

    service层:

    1. package com.wzy.jdbctemplate.service;
    2. import com.wzy.jdbctemplate.dao.BookDao;
    3. import com.wzy.jdbctemplate.pojo.Book;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.stereotype.Service;
    6. import java.util.List;
    7. @Service//创建对象
    8. public class BookService {
    9. @Autowired//自动装配
    10. private BookDao bookDao;
    11. public void deleteList(List<Object[]> list){
    12. bookDao.deleteBookByList(list);
    13. }
    14. }

    测试类:

    1. package com.wzy.jdbctemplate.test;
    2. import com.wzy.jdbctemplate.pojo.Book;
    3. import com.wzy.jdbctemplate.service.BookService;
    4. import org.junit.Test;
    5. import org.springframework.context.ApplicationContext;
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;
    7. import java.util.ArrayList;
    8. import java.util.Collections;
    9. import java.util.List;
    10. import static org.junit.Assert.*;
    11. public class BookServiceTest {
    12. ApplicationContext applicationContext =
    13. new ClassPathXmlApplicationContext("com/wzy/jdbctemplate/spring.xml");
    14. BookService bookService = applicationContext.getBean("bookService",BookService.class);
    15. @Test
    16. public void deleteList(){
    17. List<Object[]> list = new ArrayList<>();
    18. Object[] obj1 = {1};
    19. Object[] obj2 = {2};
    20. Object[] obj3 = {3};
    21. Collections.addAll(list,obj1,obj2,obj3);
    22. bookService.deleteList(list);
    23. }
    24. }