MyBatis Generator 生成的example 使用 and or 简单混合查询
MyBatis Generator 生成的example 使用 and or 简单混合查询
参考博客:https://www.cnblogs.com/kangping/p/6001519.html
简单介绍:
Example类用于构造复杂的筛选条件
1、Criterion[标准,准则,规范,准据] 条件
Criterion是最基本,最底层的Where条件,用于字段级的筛选,例如:字段 in | not in | like | > | >= | < | <= | is not null | is null 等
实例:某字段【user】,Criterion为【 user is not null】
MyBatis Generator会为每个字段产生如上的Criterion,如果表的字段比较多,产生的Example类会十分庞大。
理论上通过Example类可以构造你想到的任何筛选条件。
注意:但分页一般不用,它会将数据全部查询出来,在内存中分页,查询效率较慢
public Criteria or() {
Criteria criteria = createCriteriaInternal();
oredCriteria.add(criteria);
return criteria;
}
查询条件1:a=? and (b=? or c=?) 不支持
查询条件2:(a=? And b=?) or (a=? And c=?) 支持
写法一:
DemoExample example=new DemoExample(); DemoExample.Criteria criteria1=example.createCriteria();
criteria1.andAEqualTo(?).andBEqualTo(?); DemoExample.Criteria criteria2=example.createCriteria();
criteria2.andAEqualTo(?).andCEqualTo(?); example.or(criteria2); SqlSession sqlSession = MyBatisUtil.openSession();
DemoMapper m = sqlSession.getMapper(DemoMapper.class);
m.countByExample(example);
生成SQL语句:
select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )
写法二:
DemoExample example=new DemoExample(); example.or().andAEqualTo(?).andBEqualTo(?);
example.or().andAEqualTo(?).andCEqualTo(?); SqlSession sqlSession = MyBatisUtil.openSession();
DemoMapper m = sqlSession.getMapper(DemoMapper.class);
m.countByExample(example);
生成SQL语句:
select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )
查询条件3:(a=? and (b=? or c=?)) 支持
假设两个搜索项,A项搜索,可搜索b,c(bc或关系),B项搜索可搜索a,B项搜索与A项搜索是与关系。
修改DemoExample.java文件,新增方法:
public Criteria andOrDemo(String value){
addCriterion("(b = \""+value+"\" or c = \""+value+"\")");
return (Criteria) this;
}
DemoAction.java
DemoExample example=new DemoExample();
Criteria criteria = example.createCriteria();
criteria.andAEqualTo(?).andOrDemo(?); SqlSession sqlSession = MyBatisUtil.openSession();
DemoMapper m = sqlSession.getMapper(DemoMapper.class);
m.countByExample(example);
//生成的sql语句
select count(*) from demo WHERE ( a = ? and ( b = ? or c = ? ))
项目应用:
Step1:应用generatorConfig.xml自动根据表字段生成PO类,mapper接口与mapper.xml映射文件
Step2:创建Controller类,Service接口及Service实现类,Controller调用Service,service实现类核心代码:
public String getTraceaccountByPin(String pin) {
UserAccountExample example = new UserAccountExample();
UserAccountExample.Criteria criteria = example.createCriteria();
criteria.andAccountEqualTo(pin);
criteria.andDelFlagEqualTo(0);
criteria.andAccountstateEqualTo(1);
List<UserAccount> accounts = mapper.selectByExample(example);
if (accounts.size() > 0 && accounts.get(0) != null) {
return accounts.get(0).getTraceaccount();
}
return null;
}
说明:在generatorConfig.xml自动生成的mapper.xml文件的SQL中自动追加条件 and account=? and delFlag=? and accountsState=?
其他
Example类的distinct字段用于指定DISTINCT查询。
orderByClause字段用于指定ORDER BY条件,这个条件没有构造方法,直接通过传递字符串值指定。
MyBatis Generator 生成的example 使用 and or 简单混合查询的更多相关文章
- mybatis Generator生成代码及使用方式
本文原创,转载请注明:http://www.cnblogs.com/fengzheng/p/5889312.html 为什么要有mybatis mybatis 是一个 Java 的 ORM 框架,OR ...
- Maven下用MyBatis Generator生成文件
使用Maven命令用MyBatis Generator生成MyBatis的文件步骤如下: 1.在mop文件内添加plugin <build> <finalName>KenShr ...
- MyBatis Generator生成DAO——序列化
MyBatis Generator生成DAO 的时候,生成的类都是没有序列化的. 还以为要手工加入(開始是手工加入的),今天遇到分页的问题,才发现生成的时候能够加入插件. 既然分页能够有插件.序列化是 ...
- 利用org.mybatis.generator生成实体类
springboot+maven+mybatis+mysql 利用org.mybatis.generator生成实体类 1.添加pom依赖: 2.编写generatorConfig.xml文件 ( ...
- 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化
Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...
- Mybatis Generator生成工具配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
- 使用MyBatis Generator生成DAO
虽然MyBatis很方便,但是想要手写全部的mapper还是很累人的,好在MyBatis官方推出了自动化工具,可以根据数据库和定义好的配置直接生成DAO层及以下的全部代码,非常方便. 需要注意的是,虽 ...
- Mybatis Generator生成Mybatis Dao接口层*Mapper.xml以及对应实体类
[前言] 使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件,Mybatis-Generator的作用就是充当了一个代码生成器的角色,使用代码生成器不仅可以简化我 ...
- Mybatis Generator生成数据库自带的中文注释
1.相关jar包 <!-- mybatis生成 jar包 --> <dependency> <groupId>org.mybatis.generator</g ...
随机推荐
- locale - 地域定义文件的描述
描述 地域 定义文件含有 localedef(1) 命令所需的全部信息. 定义文件由几个小节组成, 一个小节详细地描述地域的一个范畴. 语法 地域定义文件以一个包含有如下关键字的文件头开头: < ...
- mongo 数据库操作
启动和关闭数据库 启动 # mongodb 默认使用执行 mongod 命令所处的盘的根目录下 /data/db 作为自己的数据存储目录 # 所以在第一次执行该命令之前先自己动手新建一个 /d ...
- MFC学习笔记2---简单计算器
前言 学习了鸡啄米网页的前三部分后,我们就可以做一个小软件出来了,我选择先做一个计算器. 这是Win7系统自带的计算器: 为了提升成就感,我将计算器的大部分内容去除,于是就变成这样: 这样就只剩下了1 ...
- 2019 ACM/ICPC 全国邀请赛(西安)J And And And (树DP+贡献计算)
Then n - 1n−1 lines follow. ii-th line contains two integers f_{a_i}(1 \le f_{a_i} < i)fai(1≤fa ...
- ubuntu不能登陆
开机按shift,找到之前的内核版本或者recovery 安装vmtools 报错Not enough free space to extract VMwareTools 解决办法:将此文件夹复制到另 ...
- php内置函数分析array_count_values()
PHP_FUNCTION(array_count_values) { zval *input, /* Input array */ *entry, /* An entry in the input a ...
- Django【第21篇】:Ajax之FormData
ajax补充--------FormData等... 一.回顾上节知识点 1.什么是json字符串? 轻量级的数据交换格式 2.定时器:关于setTimeout setTimeout(foo,3000 ...
- Spring Boot 整合Spring Data JPA
Spring Boot整合Spring Data JPA 1)加入依赖 <dependency> <groupId>org.springframework.boot</g ...
- PHP超大文件上传与下载
前段时间做视频上传业务,通过网页上传视频到服务器. 视频大小 小则几十M,大则 1G+,以一般的HTTP请求发送数据的方式的话,会遇到的问题:1,文件过大,超出服务端的请求大小限制:2,请求时间过长, ...
- HDU 5687 Problem C ( 字典树前缀增删查 )
题意 : 度熊手上有一本神奇的字典,你可以在它里面做如下三个操作: 1.insert : 往神奇字典中插入一个单词 2.delete: 在神奇字典中删除所有前缀等于给定字符串的单词 3.search: ...