mybatis and,or復(fù)合查詢操作
要查詢的sql:
select * from user where name = ? and (age=? or city=?);
方法1:不使用Example查詢
直接在usermapper.xml中修改sql
方法2:使用Example查詢
sql可轉(zhuǎn)換成
select * from user where (name = ? and age=?) or (name=? and city=?);
然后使用Example查詢
UserExample example=new UserExample(); example.or().orAgeLike("%"+searchParam+"%").andNameEqualTo(userName); example.or().orCityLike("%"+searchParam+"%").andNameEqualTo(userName);
補充知識:MySQL/Mybatis多個AND和OR混用注意事項
mysql中AND的優(yōu)先級高于OR,所以在查詢時,會優(yōu)先執(zhí)行AND條件,除非使用()來將一個AND和OR括起來,這樣才能使得OR得以按照語句的順序執(zhí)行。
如下圖所示:
java測試代碼
@Test public void TestMutil(){ Species species = new Species(); ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add("長喙蚤"); arrayList.add("尤氏"); List<Species> querySpeciesesListByMutilCondition = this.speciesMapper.querySpeciesesListByMutilCondition(arrayList, species.getEnglishName(), species.getHost(), species.getPosition(), species.getLocation(), species.getPhylum(), species.getClassName(), species.getOrder(), species.getFamily(), species.getJenus()); for (Species s : querySpeciesesListByMutilCondition) { System.out.println(s); } System.out.println(querySpeciesesListByMutilCondition.size()); }
Mapper文件中沒有使用()放在語句中執(zhí)行情況
<select id="querySpeciesesListByMutilCondition" resultType="Species"> SELECT * FROM t_json_species <where> <if test="englisName != null and englisName != ''">AND englishName like CONCAT('%',#{englishName},'%') OR sameName like CONCAT('%',#{englishName},'%')</if> <if test="host != null and host != ''">AND host like CONCAT('%',#{host},'%')</if> <if test="position != null and position != ''">AND position like CONCAT('%',#{position},'%')</if> <if test="location != null and location != ''">AND location like CONCAT('%',#{location},'%')</if> <if test="phylumName != null and phylumName != ''">AND phylumName = #{phylumName}</if> <if test="className != null and className != ''">AND className = #{className}</if> <if test="orderName != null and orderName != ''">AND orderName = #{orderName}</if> <if test="familyName != null and familyName != ''">AND familyName = #{familyName}</if> <if test="jenusName != null and jenusName != ''">AND jenusName = #{jenusName}</if> <if test="nameList != null and nameList != ''"> <foreach collection="nameList" item="name" >AND name like CONCAT('%',#{name},'%') OR sameName like CONCAT('%',#{name},'%')</foreach> </if> </where> </select>
Mapper文件中使用()放在語句中執(zhí)行情況
<select id="querySpeciesesListByMutilCondition" resultType="Species"> SELECT * FROM t_json_species <where> <if test="englisName != null and englisName != ''">AND englishName like CONCAT('%',#{englishName},'%') OR sameName like CONCAT('%',#{englishName},'%')</if> <if test="host != null and host != ''">AND host like CONCAT('%',#{host},'%')</if> <if test="position != null and position != ''">AND position like CONCAT('%',#{position},'%')</if> <if test="location != null and location != ''">AND location like CONCAT('%',#{location},'%')</if> <if test="phylumName != null and phylumName != ''">AND phylumName = #{phylumName}</if> <if test="className != null and className != ''">AND className = #{className}</if> <if test="orderName != null and orderName != ''">AND orderName = #{orderName}</if> <if test="familyName != null and familyName != ''">AND familyName = #{familyName}</if> <if test="jenusName != null and jenusName != ''">AND jenusName = #{jenusName}</if> <if test="nameList != null and nameList != ''"> <foreach collection="nameList" item="name" >AND (name like CONCAT('%',#{name},'%') OR sameName like CONCAT('%',#{name},'%'))</foreach> </if> </where> </select>
補充:
如果這里使用多個%來解決上述的含有多個OR和AND情況,那么所實現(xiàn)功能會有問題,因為多個關(guān)鍵詞有%來連接,會有一個次序問題。具體效果見下圖
以上這篇mybatis and,or復(fù)合查詢操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot2.6.7集成springfox3.0.0的示例代碼
這篇文章主要介紹了springboot2.6.7集成springfox3.0.0的示例代碼,本文通過示例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2024-04-04Maven profile實現(xiàn)不同環(huán)境的配置管理實踐
這篇文章主要介紹了Maven profile實現(xiàn)不同環(huán)境的配置管理實踐,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09java基礎(chǔ)詳解之數(shù)據(jù)類型知識點總結(jié)
這篇文章主要介紹了java基礎(chǔ)詳解之數(shù)據(jù)類型知識點總結(jié),文中有非常詳細的代碼示例,對正在學習java基礎(chǔ)的小伙伴們有很大的幫助,需要的朋友可以參考下2021-04-04Java程序順序結(jié)構(gòu)中邏輯控制語句詳解流程
在程序開發(fā)的過程之中一共會存在有三種程序邏輯:順序結(jié)構(gòu)、分支結(jié)構(gòu)、循環(huán)結(jié)構(gòu),對于之前所編寫的代碼大部分都是順序結(jié)構(gòu)的定義,即:所有的程序?qū)凑斩x的代碼順序依次執(zhí)行2021-10-10mybatis中點擊mapper接口快速定位到對應(yīng)xml中sql方式
這篇文章主要介紹了mybatis中點擊mapper接口快速定位到對應(yīng)xml中sql方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01java實現(xiàn)合并兩個已經(jīng)排序的列表實例代碼
這篇文章主要介紹了java實現(xiàn)合并兩個已經(jīng)排序的列表實例代碼,有需要的朋友可以參考一下2013-12-12WebClient拋UnsupportedMediaTypeException異常解決
這篇文章主要為大家介紹了WebClient拋UnsupportedMediaTypeException異常的解決方案,文中給大家介紹了六中方案,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02Java基礎(chǔ)教程之static五大應(yīng)用場景
這篇文章主要給大家介紹了關(guān)于Java基礎(chǔ)教程之static五大應(yīng)用場景的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-06-06