国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

mybatis and,or復合查詢操作

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

要查詢的sql:

select * from user where name = ? and (age=? or city=?);

方法1:不使用Example查詢

直接在usermapper.xml中修改sql

方法2:使用Example查詢

sql可轉換成

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的優先級高于OR,所以在查詢時,會優先執行AND條件,除非使用()來將一個AND和OR括起來,這樣才能使得OR得以按照語句的順序執行。

如下圖所示:

mybatis and,or復合查詢操作

java測試代碼

mybatis and,or復合查詢操作

@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文件中沒有使用()放在語句中執行情況

mybatis and,or復合查詢操作

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

mybatis and,or復合查詢操作

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

補充:

如果這里使用多個%來解決上述的含有多個OR和AND情況,那么所實現功能會有問題,因為多個關鍵詞有%來連接,會有一個次序問題。具體效果見下圖

mybatis and,or復合查詢操作

以上這篇mybatis and,or復合查詢操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Mybatis 數據庫
主站蜘蛛池模板: 欧美一级毛片免费高清的 | 亚洲欧美另类视频 | 一级毛片日韩 | 成人男男黄网色视频免费 | avtt亚洲一区中文字幕 | 国产在线精品一区二区三区 | 国产精品免费一区二区三区四区 | 黄色在线视频网 | 日韩美女在线视频 | 亚洲三级在线播放 | 国产精品久久久久久一区二区 | 欧美日韩国产高清一区二区三区 | 青青草国产一区二区三区 | 毛片韩国 | 久久亚洲国产精品一区二区 | 偷柏自拍亚洲欧美综合在线图 | 一级毛片情侣 | 日韩精品中文字幕一区三区 | 亚洲欧美日韩在线一区二区三区 | 亚洲天堂视频在线观看 | 国产午夜精品不卡观看 | 色丁香久久 | 欧美日韩人成在线观看 | 中文字幕一区二区三区 精品 | 九草视频在线观看 | 在线视免费频观看韩国aaa | 久久影院一区二区三区 | 日本强不卡在线观看 | 国产精品系列在线一区 | 99精品国产成人一区二区 | 91香蕉国产在线观看免费永久 | 玖玖玖精品视频免费播放 | 国产精品亚洲一区二区在线观看 | 国内自拍在线 | 国产福利不卡一区二区三区 | 国产欧美在线一区二区三区 | 欧美在线视频二区 | 高清国产美女一级a毛片录 高清国产亚洲va精品 | 日本二级毛片免费 | 久草在线观看首页 | 日韩欧美国产精品第一页不卡 |