关于通用MapperExample使用记录

环境准备
  • 需要集成 mybatis 的 generator 插件,方便自动生成 实体类和 mapper 类,还可以生成xml,不过一般我们都不用 xml
  • baseMapper 需要继承 ExampleMapper 不过只需要继承 Mapper 就可以了,因为 Mapper 已经继承了 ExampleMapper

Example 的用法

首先需要说明一点 ,和 Example 使用相同的还有 Condition 类 该类继承自 Example,使用方法和 Example 完全一样,只是为了避免语义有歧义重命名的一个类,这里我们都用 Example 来说明

  • 创建 Example :
1
Example example = new Example(XXX.class);

其中构造方法为生成的 model 实体类,还有 2 个构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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

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

然后可以对 example 的实体类的单表进行查询了

1
2
3
4
Example example = new Example(XXX.class);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.or().andLessThan("id", 41);
List<XXX> list = mapper.selectByExample(example);

以上查询的条件是,查询 id 大于 100 并且小于 151 或者 id 小于 41 的记录

还可以写成 sql 的方式:

1
2
3
4
5
Example example = new Example(XXX.class);
example.createCriteria().andCondition("id > 100 and id <151 or id < 41");

// andCondition() 方法可以叠加使用,像这样
example.createCriteria().andCondition("id > 100 and id <151").orCondition("id <41");

andCondition() 有2中使用方法:
andCondition(String condition) : 手写条件,例如 “length(name)<5”
andCondition(String condition, Object value) : 手写左边条件,右边用value值,例如 “length(name)=” “5”
orCondition() 也是类似的

example 里有很多 mysql 常用的方法,使用方法和 elasticsearch 的 java api 很类似,这里列举几个

  • Set<String> selectColumns : 查询的字段
  • Set<String> excludeColumns : 排除的查询字段
  • Map<String, EntityColumn> propertyMap : 属性和列对应
  • andAllEqualTo : 将此对象的所有字段参数作为相等查询条件,如果字段为 null,则为 is null
  • andGreaterThan : and 条件 大于
  • andBetween : and 条件 between
  • andEqualTo : 将此对象的不为空的字段参数作为相等查询条件 还有一种有 value 参数的是 = 条件
  • andGreaterThanOrEqualTo : and 条件 》=

还有一些一看就知道意思的

  • andIn
  • andIsNotNull
  • andIsNull
  • andLessThan
  • andLessThanOrEqualTo
  • andNotLike

上面是以 and 条件举例 ,or的条件也是一样的

最新文章

  1. Java 基础高级2 网络编程
  2. Android开发自学笔记(Android Studio)&mdash;4.2TextView及其子类
  3. Pandas-数据探索
  4. java开发JDBC连接数据库详解
  5. SQL SERVER中的扩展属性
  6. MYSQL 安装更新,使用,管理,备份和安全等
  7. ELK+redis集群搭建
  8. django 用imagefiled访问图片
  9. google chrome 32 升级变更找回user agent(google chrome lose user agent)
  10. centos 6.6编译安装nginx--来自阿里云帮助文档
  11. javascript高级知识点——函数的长度
  12. Android 反射-换一种方式编程
  13. word2-寻找社交新浪微博中的目标用户
  14. Visual studio2010和Modelsim配置SystemC开发(转)
  15. js 获取 时间戳的三种方法
  16. 利用GSEA对基因表达数据做富集分析
  17. listView从底部回到顶部代码实现
  18. Spring的Assert工具类的用法
  19. elastic search使用
  20. 第二章习题 C++

热门文章

  1. Kafka之--自动启动zookeeper &amp; kafka 脚本
  2. 每天五分钟Go - 常量
  3. 【洛谷P1140 相似基因】动态规划
  4. FormData提交文件(十四)
  5. Rowid和Rownum
  6. 构建后端第2篇之---springb @ComponentScan注解使用
  7. Vue 动态绑定CSS样式
  8. ES6与ES2015、ES2016以及ECMAScript的区别
  9. connect()函数阻塞问题
  10. Shell-15-脚本练习