Mybatis 的動態SQL,批量增刪查改


 個人博客網:https://wushaopei.github.io/    (你想要這里多有)

批量增刪改的接口:

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

        //批量刪除
	int deleteList(List<Integer> ids);

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

接口實現類:

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


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


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


	@Override
	public int updateList(List<Book> 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 語句的編寫:

  <!-- 批量根據id進行刪除 --> <delete id="deleteByPrimaryKeys" parameterType="java.util.List" > delete from t_book where id in <foreach collection="list" item="id" open="(" close=")" separator="," > #{id,jdbcType=INTEGER} </foreach> </delete> <!-- 批量進行插入數據 --> <insert id="inserts" parameterType="java.util.List"> insert into t_book (id,name,author,price,sales,stock) values <foreach collection="list" item="Book" separator="," index="index"> (null, #{Book.name}, #{Book.author}, #{Book.price}, #{Book.sales}, #{Book.stock}) </foreach> </insert> <!-- 批量根據id進行修改 --> <update id="updateByPrimaryKeys" parameterType="java.util.List" > update t_book <trim prefix="set" suffixOverrides=","> <trim prefix="name=case" suffix="end,"> <foreach collection="list" item="Book" index="index"> <if test="Book.name!=null"> when id=#{Book.id} then #{Book.name} </if> </foreach> </trim> <trim prefix="name=case" suffix="end,"> <foreach collection="list" item="Book" index="index"> <if test="Book.author!=null"> when id=#{Book.id} then #{Book.author} </if> </foreach> </trim> <trim prefix="name=case" suffix="end,"> <foreach collection="list" item="Book" index="index"> <if test="Book.price!=null"> when id=#{Book.id} then #{Book.price} </if> </foreach> </trim> <trim prefix="name=case" suffix="end,"> <foreach collection="list" item="Book" index="index"> <if test="Book.sales!=null"> when id=#{Book.id} then #{Book.sales} </if> </foreach> </trim> <trim prefix="name=case" suffix="end,"> <foreach collection="list" item="Book" index="index"> <if test="Book.stock!=null"> when id=#{Book.id} then #{Book.stock} </if> </foreach> </trim> </trim> where <foreach collection="list" separator="or" item="Book" index="index"> id=#{Book.id,jdbcType=INTEGER} </foreach> </update> <!-- 批量根據id查找 --> <select id="selectByIds" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select id, name, author, price, sales, stock from t_book where id in <foreach collection="list" item="id" open="(" close=")" separator="," > #{id,jdbcType=INTEGER} </foreach> </select> 

測試: test 對批量操作進行測試是否成功:

/* * 批量插入 * */ @Test public void InsertBookServices()throws SQLException { List<Book> 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<Integer> Ids = new ArrayList(); Ids.add(1); Ids.add(2); Ids.add(3); Ids.add(4); List<Book> Books = bookService.selectList(Ids); for(Book book : Books) { System.out.println(book.toString()); } } /* * 批量刪除 * */ @Test public void DeleteBookService()throws SQLException { List<Integer> 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<Book> 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,操作更方便哦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM