通用mapper中select(),selectByPrimaryKey()和selectByExample()的区别

<1>selectByExample()  
selectByExample几乎可以解决所有的查询,select和selectByPrimary是简化的针对特定情况的解决方法

以主键为条件进行查询, 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)就是需要的对象

<2>select()
select的代码如下

Sku sku2 = new Sku();
 sku2.setId(27359021549L);
 List<Sku> select = this.skuMapper.select(sku2);

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

<3>selectByPrimaryKey()
selectByPrimaryKey的代码如下:

Sku sku=this.shuMapper.selectByPrimaryKey(27359021549L)

直接得到对象sku

<4>复杂情况时
例如当查询的id为多个id的集合时,可以用selectByPrimaryKey,也可以用selectByExample,后者的criteric有一个方法.andIn()可以处理集合
  
  
selectByExample的代码如下:

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

selecttByPrimaryKey的代码如下:

for(Integer id : ids){
    this.stockMapper.deleteByPrimaryKey(id);
});

<5>总结
  当有主键时,优先用selectByPrimaryKey,当根据实体类属性查询时用select,当有复杂查询时,如模糊查询,条件判断时使用selectByExample

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值