关于通用Mapper new Example使用记录
2024-09-07 20:46:17
关于通用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 |
|
然后可以对 example 的实体类的单表进行查询了
1 |
Example example = new Example(XXX.class); |
以上查询的条件是,查询 id 大于 100 并且小于 151 或者 id 小于 41 的记录
还可以写成 sql 的方式:
1 |
Example example = new Example(XXX.class); |
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的条件也是一样的
最新文章
- Java 基础高级2 网络编程
- Android开发自学笔记(Android Studio)&mdash;4.2TextView及其子类
- Pandas-数据探索
- java开发JDBC连接数据库详解
- SQL SERVER中的扩展属性
- MYSQL 安装更新,使用,管理,备份和安全等
- ELK+redis集群搭建
- django 用imagefiled访问图片
- google chrome 32 升级变更找回user agent(google chrome lose user agent)
- centos 6.6编译安装nginx--来自阿里云帮助文档
- javascript高级知识点——函数的长度
- Android 反射-换一种方式编程
- word2-寻找社交新浪微博中的目标用户
- Visual studio2010和Modelsim配置SystemC开发(转)
- js 获取 时间戳的三种方法
- 利用GSEA对基因表达数据做富集分析
- listView从底部回到顶部代码实现
- Spring的Assert工具类的用法
- elastic search使用
- 第二章习题 C++