mybatis如何使用Criteria的and和or進(jìn)行聯(lián)合查詢
更新時(shí)間:2021年12月17日 08:40:51 投稿:jingxian
這篇文章主要介紹了mybatis如何使用Criteria的and和or進(jìn)行聯(lián)合查詢,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
Criteria的and和or進(jìn)行聯(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 = ? )
以上只是舉例,實(shí)際不會(huì)去這樣查詢。
使用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);
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中關(guān)于String StringBuffer StringBuilder特性深度解析
這篇文章主要介紹了Java中關(guān)于String StringBuffer StringBuilder特性深度解析,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09SpringBoot沒有讀取到application.yml問題及解決
這篇文章主要介紹了SpringBoot沒有讀取到application.yml問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12Java任務(wù)定時(shí)執(zhí)行器案例的實(shí)現(xiàn)
定時(shí)器會(huì)執(zhí)行指定的任務(wù),本文主要介紹了Java任務(wù)定時(shí)執(zhí)行器案例的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06Java編程實(shí)現(xiàn)beta分布的采樣或抽樣實(shí)例代碼
這篇文章主要介紹了Java編程實(shí)現(xiàn)beta分布的采樣或抽樣實(shí)例,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01