MyBatis Generator 生成的example 如何使用 and or 简单混合查询
简单介绍:
查询条件1:a=? and (b=? or c=?) 不支持
查询条件2:(a=? And b=?) or (a=? And c=?) 支持
写法1:

1 DemoExample example=new DemoExample();
2
3 DemoExample.Criteria criteria1=example.createCriteria();
4 criteria1.andAEqualTo(?).andBEqualTo(?);
5
6 DemoExample.Criteria criteria2=example.createCriteria();
7 criteria2.andAEqualTo(?).andCEqualTo(?);
8
9 example.or(criteria2);
10
11 SqlSession sqlSession = MyBatisUtil.openSession();
12 DemoMapper m = sqlSession.getMapper(DemoMapper.class);
13 m.countByExample(example);
14 //生成的sql语句
15 select count(*) from demo WHERE ( a = ? and b = ? ) or ( a = ? and c = ? )

写法2:

1 DemoExample example=new DemoExample();
2
3 example.or().andAEqualTo(?).andBEqualTo(?);
4 example.or().andAEqualTo(?).andCEqualTo(?);
5
6 SqlSession sqlSession = MyBatisUtil.openSession();
7 DemoMapper m = sqlSession.getMapper(DemoMapper.class);
8 m.countByExample(example);
9 //生成的sql语句
10 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文件,新增方法
1 public Criteria andOrDemo(String value){
2 addCriterion("(b = \""+value+"\" or c = \""+value+"\")");
3 return (Criteria) this;
4 }
DemoAction.java

1 DemoExample example=new DemoExample();
2 Criteria criteria = example.createCriteria();
3 criteria.andAEqualTo(?).andOrDemo(?);
4
5 SqlSession sqlSession = MyBatisUtil.openSession();
6 DemoMapper m = sqlSession.getMapper(DemoMapper.class);
7 m.countByExample(example);
8 //生成的sql语句
9 select count(*) from demo WHERE ( a = ? and ( b = ? or c = ? ))

MyBatis Generator 生成的example 如何使用 and or 简单混合查询的更多相关文章
- MyBatis - MyBatis Generator 生成的example 如何使用 and or 简单混合查询
简单介绍: Criteria,包含一个Cretiron的集合,每一个Criteria对象内包含的Cretiron之间是由AND连接的,是逻辑与的关系. oredCriteria,Example内有一个 ...
- 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 生成的example 使用 and or 简单混合查询
MyBatis Generator 生成的example 使用 and or 简单混合查询 参考博客:https://www.cnblogs.com/kangping/p/6001519.html 简 ...
- 【记录】Mybatis Generator生成数据对象Date/TimeStamp 查询时间格式化
Mybatis Generator是很好的工具帮助我们生成表映射关联代码,最近博主遇到一个问题,找了很久才解决, 就是用Mybatis Generator生成实体类的时候,Date 时间无法格式化输出 ...
- 取代 Mybatis Generator,这款代码生成神器配置更简单,开发效率更高!
作为一名 Java 后端开发,日常工作中免不了要生成数据库表对应的持久化对象 PO,操作数据库的接口 DAO,以及 CRUD 的 XML,也就是 mapper. Mybatis Generator 是 ...
- Mybatis Generator生成工具配置文件详解
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration ...
随机推荐
- 构造方法调用另一个构造方法,用this
using System; class Person { public int age; public string name; public Person(int age, string name) ...
- ES6 箭头函数--特性
如果箭头表达式仅仅就是简化了函数的命名,我们为什么要改变原来的习惯而去使用它呢?所以我们需要了解一下箭头函数的特性. 箭头函数内部没有constructor方法,也没有prototype,所以不支持n ...
- 微信小程序--扫描二维码
场景---在微信中扫描朋友发来的二维码后进入小程序,其实那个地址是带有参数的,那么如何接收那个参数呢,其实就是进入小程序页面的onLoad生命周期行数的options参数里面.
- Python学习笔记第九周
目录: 一.基础概念 1.paramiko模块 2.进程与线程 1.进程 2.线程 3.进程与线程的区别 4.Python GIL 5.thread模块 6.Join与Daemon 7.线程锁(Mut ...
- java ip 正则表达式
private static boolean isBoolIp(String ipAddress) { String ip = "(?:(?:25[0-5]|2[0-4][0-9]|[01] ...
- 特征选择之Chi卡方检验
特征选择之Chi卡方检验 卡方值越大,说明对原假设的偏离越大,选择的过程也变成了为每个词计算它与类别Ci的卡方值,从大到小排个序(此时开方值越大越相关),取前k个就可以. 针对英文纯文本的实验结果表明 ...
- ORB(oriented FAST and rotated BRIEF)特征提取与检测
ORB采取FAST算法检测特征点,采取BRIEF算法计算特征点描述子. 1.检测特征点 检测候选特征点周围一圈的像素值,若有足够多的像素值与候选特征点的差异都较大,则认为该候选特征点是特征点. 对于上 ...
- SQL Server导入导出表及备份恢复
1. 导出: 2. 导入
- 苹果手机不兼容autoplay属性
var audio=new Audio("music/music.mp3"); audio.preload="preload"; // 自动播放解决苹果不兼容a ...
- day 020 常用模块02
主要内容: 什么是序列化 pickle shelve json configparser(模块) 一 序列化 我们在存储数据或者网络传输数据的时候,需要对我们的对象进行处理,把对象处理成方便存储和 传 ...