欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

mybatis如何使用Criteria的and和or进行联合查询

时间:2022-03-08 10:16:47|栏目:JAVA代码|点击:

Criteria的and和or进行联合查询

DemoExample example=new DemoExample ();
DemoExample.Criteria criteria=example.createCriteria();
criteria.andidEqualTo(id);
criteria.andStatusEqualTo("0");
  
DemoExample.Criteria criteria2=example.createCriteria();
criteria2.andidEqualTo(id);
criteria2.andstatusEqualTo("1");
example.or(criteria2);
dao.countByExample(example);

生成如下SQL

select count(*) from demo WHERE ( ID = ? and STATUS = ? ) or( ID = ? and STATUS = ? ) 

以上只是举例,实际不会去这样查询。

使用criteria 查询xx and ( xx or xx)形式的sql

a and (b or c) <==> (a and b) or (a and c)

UserExample userExample = new UserExample();
String email = user.getEmail();
String telephone = user.getTelephone();
userExample.createCriteria().andIdNotEqualTo(userId).andEmailEqualTo(email);//(id != 'a' and email = 'b')
if (StringUtils.isNotBlank(telephone)) {
 Criteria criteria = userExample.createCriteria().andIdNotEqualTo(userId).andTelephoneEqualTo(telephone);//(id != 'a' and telephone = 'c')
 userExample.or(criteria);//(id != 'a' and email = 'b') or (id != 'a' and telephone = 'c')
}
List<User> userList = userService.selectByExample(userExample);

上一篇:SpringBoot 2.0 整合sharding-jdbc中间件实现数据分库分表

栏    目:JAVA代码

下一篇:springboot+thymeleaf+shiro标签的实例

本文标题:mybatis如何使用Criteria的and和or进行联合查询

本文地址:http://www.codeinn.net/misctech/195530.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有