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)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java中關于String StringBuffer StringBuilder特性深度解析
這篇文章主要介紹了Java中關于String StringBuffer StringBuilder特性深度解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09SpringBoot沒有讀取到application.yml問題及解決
這篇文章主要介紹了SpringBoot沒有讀取到application.yml問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12Java編程實現(xiàn)beta分布的采樣或抽樣實例代碼
這篇文章主要介紹了Java編程實現(xiàn)beta分布的采樣或抽樣實例,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01