av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

mybatis and,or復(fù)合查詢操作

瀏覽:157日期:2023-10-21 11:17:12

要查詢的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);

補(bǔ)充知識(shí):MySQL/Mybatis多個(gè)AND和OR混用注意事項(xiàng)

mysql中AND的優(yōu)先級(jí)高于OR,所以在查詢時(shí),會(huì)優(yōu)先執(zhí)行AND條件,除非使用()來(lái)將一個(gè)AND和OR括起來(lái),這樣才能使得OR得以按照語(yǔ)句的順序執(zhí)行。

如下圖所示:

mybatis and,or復(fù)合查詢操作

java測(cè)試代碼

mybatis and,or復(fù)合查詢操作

@Test public void TestMutil(){ Species species = new Species(); ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add('長(zhǎng)喙蚤'); 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文件中沒(méi)有使用()放在語(yǔ)句中執(zhí)行情況

mybatis and,or復(fù)合查詢操作

<select 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文件中使用()放在語(yǔ)句中執(zhí)行情況

mybatis and,or復(fù)合查詢操作

<select 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>

補(bǔ)充:

如果這里使用多個(gè)%來(lái)解決上述的含有多個(gè)OR和AND情況,那么所實(shí)現(xiàn)功能會(huì)有問(wèn)題,因?yàn)槎鄠€(gè)關(guān)鍵詞有%來(lái)連接,會(huì)有一個(gè)次序問(wèn)題。具體效果見(jiàn)下圖

mybatis and,or復(fù)合查詢操作

以上這篇mybatis and,or復(fù)合查詢操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

主站蜘蛛池模板: 日韩黄色影院 | av少妇| 免费国产| 一区二区三区久久久 | 日韩av手机在线 | 国产成人影视 | 成人国产精品免费观看 | 国产欧美日韩综合 | 青草在线视频 | 欧美日韩免费在线 | 亚洲精品一| 国产精品亚洲综合 | 久久久久网 | 福利影院在线观看 | 国产第四页 | 91白浆| av不卡在线播放 | 亚洲综合日韩 | 欧美激情综合 | 爱搞逼综合网 | 中文字幕永久 | 欧美日本在线观看 | 日韩精品一区在线 | 视频一二区 | 亚洲精品乱码久久久久 | 亚洲h视频 | 欧美一级淫片免费视频黄 | 日日日干干干 | 日韩av资源| 中文字幕久久久 | 在线看av网址 | 一区二区三区久久久 | 国产欧美一区二区三区视频在线观看 | 久草网站 | 国产精品毛片va一区二区三区 | 欧美性受xxxx黑人xyx性爽 | 天堂在线免费观看 | 能看的av网站 | 国产一区二区在线观看视频 | 亚洲视频中文字幕 | 99热精品在线 |