色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

mybatis and,or復合查詢操作

瀏覽:121日期: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 數據庫
主站蜘蛛池模板: 国产美女精品视频 | 草草影院www色欧美极品 | 一级aaaaaa毛片免费 | 欧美一级做一a做片性视频 欧美一级做一级爱a做片性 | 亚洲黄色免费在线观看 | 欧美高清视频手机在在线 | 男女无遮挡拍拍拍免费1000 | 亚洲久草在线 | 亚洲性网站| 久久精品店 | 国产欧美精品午夜在线播放 | 爽死你个放荡粗暴小淫货双女视频 | 欧美日韩生活片 | 亚洲国产日韩欧美一区二区三区 | 免费观看的毛片手机视频 | 国产成人女人在线视频观看 | 中文字幕有码在线观看 | 国产精品精品国产一区二区 | 亚洲精品第一国产综合野 | 成人性视频免费网站 | 欧美精品国产制服第一页 | 亚洲精品视频在线看 | 久久福利青草狠狠午夜 | 久久国产亚洲欧美日韩精品 | 欧美成人免费夜夜黄啪啪 | 美女啪啪网站又黄又免费 | 亚洲激情视频网站 | 亚洲国产精品久久久久666 | 欧美黄视频在线观看 | 成网站在线观看人免费 | 女人张开腿 让男人桶视频 女人张开腿等男人桶免费视频 | 成人 在线欧美亚洲 | 国产午夜毛片一区二区三区 | 久草免费手机视频 | 香蕉一区二区三区观 | 91亚洲综合 | 亚洲一区视频在线播放 | 国产视频99 | 久久久精品2018免费观看 | 看真人一一级毛片 | 亚洲理论在线观看 |