通用Mapper的example实例使用

在最近的实习生学习中,渐渐使用到了通用Mapper,但是之前根本就没接触过。其实同效果的SQL不难,但是改成相应的example难免有些不熟悉,在这里收集一些方法的使用,前半段是从其他地方抄然后小改了一下的,后半段是我自己的实操问题。为了尊重作者我在这里注明下是CSDNZzoujy(且不管他也是转的)

一、example实例

mybatis的逆向工程中会生成实例及实例对应的example,example用于添加条件,相当where后面的部分 (这一段是抄的,感觉跟我的理解不符,但是又不敢确定….)
在我的理解里,Example为我们创建的实例 Example.createCriteria()为我们创建了条件容器,然后将我们的筛选条件一一添加到里面,最后用这个实例去查询(这是我的理解,可能有错!)
Example example = new xxxExample(entity.class);
Criteria criteria = new Example().createCriteria();

方法 说明
example.setOrderByClause(“字段名 ASC”) 添加升序排列条件,DESC为降序
example.setDistinct(false) 去除重复,boolean型,true为选择不重复的记录。
criteria.andXxxIsNull 添加字段xxx为null的条件
criteria.andXxxIsNotNull 添加字段xxx不为null的条件
criteria.andXxxEqualTo(value) 添加xxx字段等于value条件
criteria.andXxxNotEqualTo(value) 添加xxx字段不等于value条件
criteria.andXxxGreaterThan(value) 添加xxx字段大于value条件
criteria.andXxxGreaterThanOrEqualTo(value) 添加xxx字段大于等于value条件
criteria.andXxxLessThan(value) 添加xxx字段小于value条件
criteria.andXxxLessThanOrEqualTo(value) 添加xxx字段小于等于value条件
criteria.andXxxIn(List<?>) 添加xxx字段值在List<?>条件
criteria.andXxxNotIn(List<?>) 添加xxx字段值不在List<?>条件
criteria.andXxxLike(“%”+value+”%”) 添加xxx字段值为value的模糊查询条件
criteria.andXxxNotLike(“%”+value+”%”) 添加xxx字段值不为value的模糊查询条件

二、转载应用举例

1.查询

① selectByPrimaryKey()

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

② selectByExample() 和 selectByExampleWithBLOGs()

Example example = new Example(entity.class);
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,email desc

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

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','[email protected]');

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='dsfgsdfgdsfgds'

②updateByPrimaryKeySelective()

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

③ updateByExample() 和 updateByExampleSelective()

Example example = new Example(entity.class);
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()

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

5.查询数据数量

①countByExample()

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

三、自我实操实例

1.多条件排序

根据多条件排序直接就在后续添加排序字段(Entity是我的DO实体类)

Example example = new Example(Affiche.class);
Example.Criteria criteria = example.createCriteria();
example.orderBy(DataConstants.SHOW_TIME).desc();
example.orderBy(DataConstants.ID).desc();

2.or条件

or条件的查询,必须建立两个criteria对象,然后出了or条件字段,其他查询条件字段必须相同,这里是我的实例就不改了,DataConstants类存放的是我的常用常量字段,DbConstants和NumConstans是存放用的String类型和Integer类型的常用值。

Example example = new Example(Affiche.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo(DataConstants.AFFICHE_TYPE,type)
            .andEqualTo(DataConstants.INSTITUTION_ID,institutionId)
            .andEqualTo(DataConstants.DELETE_TIME,DbConstants.NO_DEL_VAL)
            .andEqualTo(DataConstants.IF_DISPLAY,NumConstants.IS_DISPLAY)
            .andEqualTo(DataConstants.SHOW_STATUS,NumConstants.IS_PUBLISHED)
            .andLessThanOrEqualTo(DataConstants.SHOW_TIME,new Date());
    if(StringUtils.isNotBlank(keyWord)){
        criteria.andLike(DataConstants.AFFICHE_TITLE,keyWord);
    }

    Example.Criteria criteria1 = example.createCriteria();
    criteria1.andEqualTo(DataConstants.AFFICHE_TYPE,type)
            .andEqualTo(DataConstants.INSTITUTION_ID,institutionId)
            .andEqualTo(DataConstants.DELETE_TIME,DbConstants.NO_DEL_VAL)
            .andEqualTo(DataConstants.IF_DISPLAY,NumConstants.IS_DISPLAY)
            .andEqualTo(DataConstants.SHOW_STATUS,NumConstants.IS_PUBLISHED)
            .andLessThanOrEqualTo(DataConstants.SHOW_TIME,new Date());
    if(StringUtils.isNotBlank(keyWord)){
        criteria1.andLike(DataConstants.AFFICHE_CONTENT,keyWord);
    }
    example.or(criteria1);
    List<Affiche> afficheList = afficheMapper.selectByExample(example);

3.IN条件

in条件入参可以是集合形式,如下的List<Integer> ids具体的实现方法,目前只会用分析不了原理

    Example example = new Example(Affiche.class);
    example.createCriteria().andEqualTo(DbConstants.DELETE_TIME, DbConstants.NO_DEL_VAL)
            .andEqualTo(DataConstants.INSTITUTION_ID, institutionId)
            .andIn(DataConstants.ID, ids);

目前能想到的就之后这些了,日后如果有更多的了解,会后续添加。希望自己能成长的更快一些吧!

猜你喜欢

转载自blog.csdn.net/qq_35174296/article/details/81389017
今日推荐