Mybatis 的动态SQL,批量增删查改

个人博客网:https://wushaopei.github.io/    (你想要这里多有)

批量增删改的接口:

public interface BookService {
	
        //批量增加	
	
	int saveList(List records);
	//批量查找
	
	List selectList(List ids);

        //批量删除
	int deleteList(List ids);

        //批量修改
	int updateList(List bookList);
	
}

接口实现类:

@Service 
public class BookServiceImpl implements BookService{
	
	
	@Autowired
	BookMapper bookMapper;
	
	
	@Override
	public int saveList(List list) {
		// TODO Auto-generated method stub
		int count = bookMapper.inserts(list);
		return count;
	}


	@Override
	public List selectList(List ids) {
		// TODO Auto-generated method stub
		
		List books = bookMapper.selectByIds(ids);
		return books;
	}


	@Override
	public int deleteList(List ids) {
		// TODO Auto-generated method stub
		return bookMapper.deleteByPrimaryKeys(ids);
	}


	@Override
	public int updateList(List bookList) {
		// TODO Auto-generated method stub
		
		return bookMapper.updateByPrimaryKeys(bookList);
	}

	
}

对应的实体类 JavaBean :

public class Book {
    public Book(Integer id, String name, String author, BigDecimal price, Integer sales, Integer stock) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.price = price;
		this.sales = sales;
		this.stock = stock;
	}
............省略

mapper.xml 中SQL 语句的编写:

  
  
    delete from t_book
    where id in     
	
	      #{id,jdbcType=INTEGER}
	 
  

    
 
	
	
		insert into t_book (id,name,author,price,sales,stock) values
		
		
		
		(null, #{Book.name}, #{Book.author}, #{Book.price}, #{Book.sales}, #{Book.stock})
		
		
	
	

  
 
   
	    update t_book
	    	
	    	 
	    	 	
	    			
	    				when id=#{Book.id} then #{Book.name}
	    			
	    		
	    	 
	    	 
	    	 	
	    			
	    				when id=#{Book.id} then #{Book.author}
	    			
	    		
	    	 
	    	 
	    	 	
	    			
	    				when id=#{Book.id} then #{Book.price}
	    			
	    		
	    	 
	    	 
	    	 	
	    			
	    				when id=#{Book.id} then #{Book.sales}
	    			
	    		
	    	 
	    	 
	    	 	
	    			
	    				when id=#{Book.id} then #{Book.stock}
	    			
	    		
	    	 
	    	  	
	    	where 
	    	 
	    		id=#{Book.id,jdbcType=INTEGER}
	    	
  
	 

  
  
  
 

测试: test 对批量操作进行测试是否成功:

/*
	 *  批量插入
	 * */
	@Test
	public void InsertBookServices()throws SQLException {
		
		List bookList = new ArrayList<>();
		bookList.add(new Book(null,"生活1","奕1君",new BigDecimal(1),1,1));
		bookList.add(new Book(null,"生活2","奕2君",new BigDecimal(2),2,2));
		bookList.add(new Book(null,"生活3","奕3君",new BigDecimal(3),3,3));
		bookList.add(new Book(null,"生活4","奕4君",new BigDecimal(4),4,4));
		bookList.add(new Book(null,"生活5","奕5君",new BigDecimal(5),5,5));
		
		int count = bookService.saveList(bookList);
		System.out.println(count);
	}
	
	/*
	 *  批量查询
	 * */
	@Test
	public void SelectBookService()throws SQLException {
		
		List Ids = new ArrayList();
		Ids.add(1);
		Ids.add(2);
		Ids.add(3);
		Ids.add(4);
				
		List Books = bookService.selectList(Ids);

		for(Book book : Books) {
			System.out.println(book.toString());
		}
	}
	
	/*
	 *  批量删除
	 * */
	@Test
	public void DeleteBookService()throws SQLException {
		
		List Ids = new ArrayList();
		Ids.add(1);
		Ids.add(2);
		Ids.add(3);
		Ids.add(4);
				
		int  counts = bookService.deleteList(Ids);
		System.out.println(counts);
		
	}
	
	/*
	 *  批量更新
	 * */
	@Test
	public void UpdateBookService()throws SQLException {
		
		List bookList = new ArrayList<>();
		bookList.add(new Book(6,"生活6","奕6君",new BigDecimal(1),1,1));
//		bookList.add(new Book(7,"生活7","奕7君",new BigDecimal(2),2,2));
		bookList.add(new Book(8,"生活8","奕8君",new BigDecimal(3),3,3));
		bookList.add(new Book(9,"生活9","奕9君",new BigDecimal(4),4,4));
		bookList.add(new Book(10,"生活10","奕10君",new BigDecimal(5),5,5));
		
		int count = bookService.updateList(bookList);
		System.out.println(count);
		
		
	}

链接:https://pan.baidu.com/s/1oAYg5X8eeqf18dUTU1bUpA
提取码:jznv
复制这段内容后打开百度网盘手机App,操作更方便哦

你可能感兴趣的:(MYSQL,SSM)