软件编程
位置:首页>> 软件编程>> java编程>> mybatis如何使用Criteria的and和or进行联合查询

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

作者:jingxian  发布时间:2023-02-23 00:44:13 

标签:mybatis,Criteria,and,or,联合查询

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);

来源:https://blog.csdn.net/ai932820942/article/details/82115106

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com