天天看点

Mybatis逆向工程生成的selectByExample,selectByPrimaryKey和select详解

Mybatis的逆向工程生成的Example类主要进行实体类的复杂查询

通用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 list = this.skuMapper.selectByExample(example);

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

select的代码如下

Sku sku2 = new Sku();

sku2.setId(27359021549L);

List 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

如何使用

随便点开一个Example实体类:

//升序还是降序:字段+空格+asc(desc)
protected String orderByClause;
 
//去除重复:true去重,false则反之
protected boolean distinct;
 
//自定义查询条件
protected List<Criteria> oredCriteria;
           

一、mapper接口中的方法解析

mapper接口中的函数及方法

Mybatis逆向工程生成的selectByExample,selectByPrimaryKey和select详解

二、example实例解析

mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分

xxxExample example = new xxxExample(); 
Criteria criteria = new Example().createCriteria();
           
Mybatis逆向工程生成的selectByExample,selectByPrimaryKey和select详解

三、应用举例

1.查询

① selectByPrimaryKey()

User user = XxxMapper.selectByPrimaryKey(100);
//相当于select * from user where id = 100 
           

② selectByExample() 和 selectByExampleWithBLOGs()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
criteria.andUsernameIsNull();
example.setOrderByClause("username asc,email desc");
List<?>list = XxxMapper.selectByExample(example);
//相当于:select * from user where username = 'wyw' and  username is null order by username asc
           

注:在iBator逆向工程生成的文件XxxExample.java中包含一个static的内部类Criteria,Criteria中的方法是定义SQL 语句where后的查询条件。

Criteria:使用 Criteria 进行查询,主要要清晰的是 Hibernate 提供了那些类和方法来满足开发中查询条件的创建和组装。

2.插入数据

①insert()

User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("admin");
user.setPassword("admin")
user.setEmail("[email protected]");
XxxMapper.insert(user);
//相当于:insert into user(ID,username,password,email) values ('dsfgsdfgdsfgds','admin','admin')

           

3.更新数据

①updateByPrimaryKey()

User user =new User();
user.setId("dsfgsdfgdsfgds");
user.setUsername("wyw");
user.setPassword("wyw");
user.setEmail("[email protected]");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set username='wyw', password='wyw', email='[email protected]' where id='dsfgsdf
           

②updateByPrimaryKeySelective()

User user = new User();
user.setId("dsfgsdfgdsfgds");
user.setPassword("wyw");
XxxMapper.updateByPrimaryKey(user);
//相当于:update user set password='wyw' where id='dsfgsdfgdsfgds'

           

③ updateByExample() 和 updateByExampleSelective()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
User user = new User();
user.setPassword("wyw");
XxxMapper.updateByPrimaryKeySelective(user,example);
//相当于:update user set password='wyw' where username='admin'
           

注意:updateByExample()更新所有的字段,包括字段为null的也更新,建议使用 updateByExampleSelective()更新想更新的字段

4.删除数据

①deleteByPrimaryKey()

XxxMapper.deleteByPrimaryKey(1); 
//相当于:delete from user where id=1 
           

②deleteByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("admin");
XxxMapper.deleteByExample(example);
//相当于:delete from user where username='admin'
           

5.查询数据数量

①countByExample()

UserExample example = new UserExample();
Criteria criteria = example.createCriteria();
criteria.andUsernameEqualTo("wyw");
int count = XxxMapper.countByExample(example);
//相当于:select count(*) from user where username='wyw'