最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2020年11月27日 15:03:42   作者:cuyun11  
這篇文章主要介紹了mybatis and,or復(fù)合查詢操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧

要查詢的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條件,除非使用()來將一個(gè)AND和OR括起來,這樣才能使得OR得以按照語句的順序執(zhí)行。

如下圖所示:

java測(cè)試代碼

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

補(bǔ)充:

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

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

相關(guān)文章

  • 快速解決idea @Autowired報(bào)紅線問題

    快速解決idea @Autowired報(bào)紅線問題

    這篇文章主要介紹了快速解決idea @Autowired報(bào)紅線問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • 一文帶你探究Spring中Bean的線程安全性問題

    一文帶你探究Spring中Bean的線程安全性問題

    很多人都想spring中的bean是線程安全的嗎?本文將帶你探究Spring中Bean的線程安全性問題,感興趣的同學(xué)可以參考閱讀下
    2023-05-05
  • Java類初始化執(zhí)行流程解析

    Java類初始化執(zhí)行流程解析

    這篇文章主要介紹了Java類初始化執(zhí)行流程,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-05-05
  • Mybatis傳遞多個(gè)參數(shù)的解決辦法(三種)

    Mybatis傳遞多個(gè)參數(shù)的解決辦法(三種)

    這篇文章主要介紹了Mybatis傳遞多個(gè)參數(shù)的解決辦法(三種),個(gè)人覺得第三種解決辦法比較好用,有需要的朋友一起學(xué)習(xí)吧
    2016-05-05
  • 如何給yml配置文件的密碼加密(SpringBoot)

    如何給yml配置文件的密碼加密(SpringBoot)

    這篇文章主要介紹了如何給yml配置文件的密碼加密(SpringBoot),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • java Hibernate 一對(duì)多自身關(guān)聯(lián)問題

    java Hibernate 一對(duì)多自身關(guān)聯(lián)問題

    formBean在提交表單的時(shí)候,域中數(shù)據(jù)庫在下一次中仍然保留引起的,struts formBean 默認(rèn)的scope為session,手動(dòng)設(shè)置為request,就好了
    2008-07-07
  • Java構(gòu)造函數(shù)通透理解篇

    Java構(gòu)造函數(shù)通透理解篇

    這篇文章主要介紹了Java構(gòu)造函數(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-09-09
  • SpringBoot動(dòng)態(tài)定時(shí)任務(wù)實(shí)現(xiàn)與應(yīng)用詳解

    SpringBoot動(dòng)態(tài)定時(shí)任務(wù)實(shí)現(xiàn)與應(yīng)用詳解

    定時(shí)任務(wù)在許多應(yīng)用場(chǎng)景中是必不可少的,特別是在自動(dòng)化任務(wù)執(zhí)行、定期數(shù)據(jù)處理等方面,定時(shí)任務(wù)能極大地提高系統(tǒng)的效率,然而,隨著業(yè)務(wù)需求的變化,定時(shí)任務(wù)的執(zhí)行頻率或時(shí)間點(diǎn)可能需要?jiǎng)討B(tài)調(diào)整,所以本文給大家介紹了SpringBoot動(dòng)態(tài)定時(shí)任務(wù)實(shí)現(xiàn)與應(yīng)用
    2024-08-08
  • Java 數(shù)組分析及簡(jiǎn)單實(shí)例

    Java 數(shù)組分析及簡(jiǎn)單實(shí)例

    這篇文章主要介紹了Java 數(shù)組分析及簡(jiǎn)單實(shí)例的相關(guān)資料,在Java中它就是對(duì)象,一個(gè)比較特殊的對(duì)象,需要的朋友可以參考下
    2017-03-03
  • springboot 運(yùn)行 jar 包讀取外部配置文件的問題

    springboot 運(yùn)行 jar 包讀取外部配置文件的問題

    這篇文章主要介紹了springboot 運(yùn)行 jar 包讀取外部配置文件,本文主要描述linux系統(tǒng)執(zhí)行jar包讀取jar包同級(jí)目錄的外部配置文件,主要分為兩種方法,每種方法通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2021-07-07

最新評(píng)論

上杭县| 赣州市| 金川县| 宜昌市| 天气| 秭归县| 伽师县| 南宫市| 旅游| 南涧| 左权县| 霸州市| 同心县| 航空| 象州县| 余江县| 大埔区| 阿勒泰市| 宝坻区| 工布江达县| 海原县| 霞浦县| 维西| 洛阳市| 宝山区| 高雄市| 张家界市| 桐城市| 鹿泉市| 慈溪市| 渭源县| 灵璧县| 自治县| 西平县| 香格里拉县| 大冶市| 长寿区| 繁昌县| 洛浦县| 布尔津县| 莆田市|