<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