mybatis如何使用Criteria的and和or進行聯(lián)合查詢
更新時間:2021年12月17日 08:40:51 投稿:jingxian
這篇文章主要介紹了mybatis如何使用Criteria的and和or進行聯(lián)合查詢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
Criteria的and和or進行聯(lián)合查詢
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);
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring boot2+jpa+thymeleaf實現(xiàn)增刪改查
這篇文章主要介紹了Spring boot2+jpa+thymeleaf實現(xiàn)增刪改查,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-04-04
一文帶你徹底了解Java8中的Lambda,函數(shù)式接口和Stream
這篇文章主要為大家詳細介紹了解Java8中的Lambda,函數(shù)式接口和Stream的用法和原理,文中的示例代碼簡潔易懂,感興趣的小伙伴可以跟隨小編一起學習一下2023-08-08
關(guān)于RowBounds分頁原理、RowBounds的坑記錄
這篇文章主要介紹了關(guān)于RowBounds分頁原理、RowBounds的坑記錄,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
Spring Boot實現(xiàn)STOMP協(xié)議的WebSocket的方法步驟
這篇文章主要介紹了Spring Boot實現(xiàn)STOMP協(xié)議的WebSocket的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05

