java:mybatis:工具类example详解

一、mapper接口中的方法解析
mapper接口中的部分常用方法及功能如下:

方法 功能说明
int countByExample(UserExample example) thorws SQLException 按条件计数
int deleteByPrimaryKey(Integer id) thorws SQLException 按主键删除
int deleteByExample(UserExample example) thorws SQLException 按条件删除
String/Integer insert(User record) thorws SQLException 插入数据(返回值为ID)
User selectByPrimaryKey(Integer id) thorws SQLException 按主键查询
ListselectByExample(UserExample example) thorws SQLException 按条件查询
ListselectByExampleWithBLOGs(UserExample example) thorws SQLException 按条件查询(包括BLOB字段)。只有当数据表中的字段类型有为二进制的才会产生
int updateByPrimaryKey(User record) thorws SQLException 按主键更新
int updateByPrimaryKeySelective(User record) thorws SQLException 按主键更新值不为null的字段
int updateByExample(User record, UserExample example) thorws SQLException 按条件更新
int updateByExampleSelective(User record, UserExample example) thorws SQLException 按条件更新值不为null的字段

二、Example类解析
mybatis的逆向工程中会生成实体类及实体类对应的example类,example类用于添加条件,相当where后面的部分。
xxxExample example = new xxxExample(); 
Criteria criteria = new Example().createCriteria();
example类中的部分常用方法及功能如下:

方法 功能说明
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的模糊查询条件
criteria.andXxxBetween(value1,value2) 添加xxx字段值在value1和value2之间条件
criteria.andXxxNotBetween(value1,value2) 添加xxx字段值不在value1和value2之间条件

注:在mybatis逆向工程生成的文件XxxExample.java中包含一个static的内部类Criteria,Criteria中的方法是定义SQL 语句where后的查询条件。
三、总结
XxxExample.java只能实现简单条件增删改查,复杂的功能还需要自己编写sql代码来实现。

参考:https://www.cnblogs.com/wxywxy/p/10697173.html

一、example类干什么用的?

理论上通过example类可以构造你想到的任何筛选条件。

例子是展示此类用法的最好方式.

Example类可以用来生成一个几乎无限的where子句.

可以让你不用再mybatis里写一大堆mapper.xml文件

注:在使用example类前 先学下MBG配置(Mybatis-Generator) http://www.mybatis.org/generator/

我使用的是Maven项目,所以导入Mapper的Maven依赖

<dependency>  
  <groupId>tk.mybatis</groupId>  
  <artifactId>mapper</artifactId>  
  <version>3.2.0</version>  
</dependency>  

二、了解example成员变量

     //升序还是降序
     //参数格式:字段+空格+asc(desc)
     protected String orderByClause;
     //去除重复
     //true是选择不重复记录
     protected boolean distinct;
     //自定义查询条件
     //Criteria的集合,集合中对象是由or连接
     protected List<Criteria> oredCriteria;
     //内部类Criteria包含一个Cretiron的集合,
     //每一个Criteria对象内包含的Cretiron之间
     //是由AND连接的
     public static class Criteria extends GeneratedCriteria {
         protected Criteria() {
             super(); 
         }
     }
     //是mybatis中逆向工程中的代码模型
     protected abstract static class GeneratedCriteria
     {…..}
     //是最基本,最底层的Where条件,用于字段级的筛选
     public static class Criterion {……}


 
(greater than) - 指相关的列必须大于方法参数中的值 
= (greater than or equal) - 指相关的列必须大于等于方法参数中的值 
< (less than) - 指相关的列必须小于于方法参数中的值 
<= (less than or equal) - 指相关的列必须小于等于方法参数中的值 
LIKE - 指相关的列必须 “like” 方法参数中的值. 这个方法不用必须加入 ‘%’, 您必须设置方法参数中的值. 
NOT LIKE - 指相关的列必须 “not like” 方法参数中的值. 这个方法不用必须加入 ‘%’, 您必须设置方法参数中的值. 
BETWEEN - 指相关的列必须在 “between” 方法参数中的两个值之间. 
NOT BETWEEN - 指相关的列必须不在 “not between” 方法参数中的两个值之间. 
IN - 指相关的列必须在传入的方法参数的list中. 
NOT IN - 指相关的列必须不在传入的方法参数的list中.

 

Example example = new Example(user.class);
 
for(int i=0;i < 10;i++) {
    example.createCriteria().andEqualTo("roleCode",bean.getRoleCode)
        .andNotIn("status",Arrays.asList("0","1"))  //注意用的是类中的属性,不是数据库中的属性
        .andCondition("(FLAG <> '0' OR FLAG IS NULL)")
        .andCondition("(role_nodecode is null or role_nodecode         ='"+fean.getNextRoleNodeCode+"')");
    example.selectProperties("serialNo", "roleCode", "employeeCode", "roleNodeCode");   
    example.setDistinct(true); // 去除重复,boolean型,true为选择不重复的记录。
    example.orderBy("serialNo"); // 对应实体类中属性名
    example.setOrderByClause("SERIAL_NO desc"); // 对应表结构字段名称
 
    // 排序另外写法
    example.orderBy("serialNo").asc().orderBy("employeeCode").asc();
 
    List<AfwFlowDtl> afwFlowDtlList = afwFlowDtlMapper.selectList(example);
 
    example.clear();// 把条件清空,可以循环使用,嵌套在for循环里面使用

}

注:

这里面有一个坑,就是.andCondition("(role_nodecode is null or role_nodecode ='"+fean.getNextRoleNodeCode+"')");

若是和面的参数role_nodecode是字符串类型,对应的value值 要加' '


 

发布了260 篇原创文章 · 获赞 119 · 访问量 51万+

猜你喜欢

转载自blog.csdn.net/weixin_38750084/article/details/103619467