通用mapper中selectByExample,selectByPrimaryKey和select的區別


1  selectByExample幾乎可以解決所有的查詢,select和selectByPrimary是簡化的針對特定情況的解決方法

2  以主鍵為條件進行查詢, selectByExample的代碼如下:

  Example example = new Example(Sku.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("id",27359021549L);
List<Sku> list = this.skuMapper.selectByExample(example);
list.get(0)就是需要的對象


select的代碼如下
Sku sku2 = new Sku();
sku2.setId(27359021549L);
List<Sku> select = this.skuMapper.select(sku2);

select.get(0)就是需要的對象



selectByPrimaryKey的代碼如下:
Sku sku=this.shuMapper.selectByPrimaryKey(27359021549L)
直接得到對象sku


3  當查詢的id為多個id的集合時,可以用selectByPrimaryKey,也可以用selectByExample,后者的criteric有一個方法.andIn()可以處理集合
  select的代碼如下:
ids.forEach(id -> {
this.stockMapper.deleteByPrimaryKey(id);
});

selectByExample的代碼如下:

Example example = new Example(Stock.class);
example.createCriteria().andIn("skuId", ids);
this.stockMapper.deleteByExample(example);




4  因此總結如下:
  當有主鍵時,優先用selectByPrimaryKey,當根據實體類屬性查詢時用select,當有復雜查詢時,如模糊查詢,條件判斷時使用selectByExample
  


免責聲明!

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



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