首页 > 其他分享 >Example的使用

Example的使用

时间:2022-12-07 14:47:19浏览次数:65  
标签:Object String exists 使用 value criteria property Example

介绍

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

Example中的构造方法

/**
 * 带exists参数的构造方法,默认notNull为false,允许为空
 *
 * @param entityClass
 * @param exists      - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
 */
public Example(Class<?> entityClass) {
   this(entityClass, true);
}


/**
 * 带exists参数的构造方法,默认notNull为false,允许为空
 *
 * @param entityClass
 * @param exists      - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
 */
public Example(Class<?> entityClass, boolean exists) {
   this(entityClass, exists, false)
}
    
/**
 * 带exists参数的构造方法
 *
 * @param entityClass
 * @param exists      - true时,如果字段不存在就抛出异常,false时,如果不存在就不使用该字段的条件
 * @param notNull     - true时,如果值为空,就会抛出异常,false时,如果为空就不使用该字段的条件
 */
public Example(Class<?> entityClass, boolean exists, boolean notNull) {
   this.exists = exists;
   this.notNull = notNull;
   this.oredCriteria = new ArrayList();
   this.table = EntityHelper.getEntityTable(entityClass);
   this.propertyMap = this.table.getPropertyMap();
   this.ORDERBY = new Example.OrderBy(this, this.propertyMap);
}

创建Example

Example example = new Example(XXX.class);

对 example 的实体类的单表进行查询

example.createCriteria().andGreaterThan("id",1).andLessThan("id",100);
List<XXX> list = mapper.selectByExample(example);

or

Criteria criteria = example.createCriteria();
if (in.getId() != null) {
  criteria.andLessThanOrEqualTo("id", in.getId());//<=id
}
List<XXX> list = mapper.selectByExample(example);
PageInfo<XXX> pageInfo = new PageInfo<>(list);
return buildPage(pageInfo);

 

常用查询条件

example.setOrderByClause("字段名 ASC");    // 添加升序排列条件,DESC为降序
example.setDistinct(false);               // 去除重复,boolean型,true为选择不重复的记录
criteria.andIsNull(String property, Object value);        // 添加字段为null的条件
criteria.andIsNotNull(String property, Object value);     // 添加字段不为null的条件
criteria.andEqualTo(String property, Object value);       // 添加字段等于value条件
criteria.andNotEqualTo(String property, Object value);    // 添加字段不等于value条件
criteria.andGreaterThan(String property, Object value);   // 添加字段大于value条件
criteria.andGreaterThanOrEqualTo(String property, Object value);   // 添加字段大于等于value条件
criteria.andLessThan(String property, Object value);      // 添加字段小于value条件
criteria.andLessThanOrEqualTo(String property, Object value);    // 添加字段小于等于value条件
criteria.andIn(String property, Iterable values);         // 添加字段值在Iterable(eg:List<?>)条件
criteria.andNotIn(String property, Iterable values);      // 添加字段值不在Iterable(eg:List<?>)条件
criteria.andLike(String property, String value); // 添加字段值为value的模糊查criteria.andLike("name","%"+value+"%");
criteria.andNotLike("name","%"+value+"%");       // 添加字段值不为value的模糊查询条件
criteria.andBetween(String property, Object value1, Object value2);       // 添加字段值在value1和value2之间条件
criteria.andNotBetween(String property, Object value1, Object value2);    // 添加字段值不在value1和value2之间条件

 

标签:Object,String,exists,使用,value,criteria,property,Example
From: https://www.cnblogs.com/cgy-home/p/16962999.html

相关文章