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

使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢(xún)

 更新時(shí)間:2022年03月11日 09:48:32   作者:black小黑黑  
這篇文章主要介紹了使用Mybatis如何實(shí)現(xiàn)多個(gè)控制條件查詢(xún),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

實(shí)現(xiàn)多個(gè)控制條件查詢(xún)

擴(kuò)展知識(shí)

1.給包起別名用<typeAliases>標(biāo)簽

< typeAliases>
< typeAlias type=“MybatiesAnimal.Animal” alias=“animal”/>
< !-- 指定實(shí)體類(lèi)在哪個(gè)包里 -->
< package name=“MybatiesAnimal”/>
< /typeAliases>

2.在<mappers>中通過(guò)<package>標(biāo)簽引Mapper.xml

如果想用package直接引入所有mapper/xml, 那么要求接口和xml在一個(gè)包里

實(shí)現(xiàn)多個(gè)條件簡(jiǎn)單查詢(xún)

操作步驟如下:

1.在Mapper.xml中輸入要查詢(xún)的SQL語(yǔ)句

2.接口文件中參數(shù)用@Param接

語(yǔ)句如下:

public List selAnimalBy(@Param(“NAME”) String a,@Param(“kind”) String b);

如果傳的是2個(gè)java簡(jiǎn)單類(lèi)型 那么需要用@Param(“name”) 指定參數(shù)名

3.text文檔中輸出

語(yǔ)句如下:

List< Animal> animal1=sqlsession.getMapper(AnimalMapper.class).selAnimalBy(“老虎”, “貓科”);
System.out.println(animal1);

數(shù)據(jù)庫(kù)的字段名和實(shí)體類(lèi)的屬性名不一致時(shí)

因?yàn)閿?shù)據(jù)庫(kù)命名多個(gè)單詞之間用下劃線連接,而Java命名規(guī)則為第二個(gè)字母大寫(xiě),命名規(guī)則不同時(shí),需要傳值。

方法一:修改SQL語(yǔ)句

SELECT a_sid sid,kind,numbers,address,NAME FROM animal

給a_sid 取別名為sid

xml中語(yǔ)句修改如下:

方法二:通過(guò)配置resultMap標(biāo)簽

原查詢(xún)語(yǔ)句:

< select id=“selAnimalBy” resultType=“animal” >
SELECT * FROM animal WHERE NAME=#{NAME} AND kind=#{kind}
< /select>

修改為:

實(shí)現(xiàn)多個(gè)條件復(fù)雜查詢(xún)

例如:查詢(xún)動(dòng)物列表中貓科中包含虎字段的數(shù)據(jù)

SQL語(yǔ)句為:

SELECT * FROM animal WHERE 1=1 AND kind=“貓科” AND NAME LIKE ‘%虎%'

1.配置xml中的select標(biāo)簽

如下圖:

2.添加接口中的執(zhí)行語(yǔ)句

public List< Animal> selCon(Animal animal);

3.text文檔檢驗(yàn)結(jié)果

Animal animal1=new Animal();
animal1.setKind(“貓科”);
animal1.setName(“虎”);
List< Animal>animal2=sqlsession.getMapper(AnimalMapper.class).selCon(animal1);
System.out.println(animal2);

輸出結(jié)果如下:

MyBatis條件查詢(xún)總結(jié)

1.if條件語(yǔ)句

<!--  if(判斷參數(shù)) - 將實(shí)體類(lèi)不為空的屬性作為where條件 -->  
    <select id="getStudentList_if" resultMap="resultMap_studentEntity" parameterType="liming.student.manager.data.model.StudentEntity">  
        SELECT ST.STUDENT_ID,  
               ST.STUDENT_NAME,  
               ST.STUDENT_SEX,  
               ST.STUDENT_BIRTHDAY,  
               ST.STUDENT_PHOTO,  
               ST.CLASS_ID,  
               ST.PLACE_ID  
          FROM STUDENT_TBL ST   
         WHERE  
        <if test="studentName !=null ">  
            ST.STUDENT_NAME LIKE CONCAT(CONCAT('%', #{studentName, jdbcType=VARCHAR}),'%')  
        </if>  
        <if test="studentSex != null and studentSex != '' ">  
            AND ST.STUDENT_SEX = #{studentSex, jdbcType=INTEGER}  
        </if>  
        <if test="studentBirthday != null ">  
            AND ST.STUDENT_BIRTHDAY = #{studentBirthday, jdbcType=DATE}  
        </if>  
        <if test="classId != null and classId!= '' ">  
            AND ST.CLASS_ID = #{classId, jdbcType=VARCHAR}  
        </if>  
        <if test="classEntity != null and classEntity.classId !=null and classEntity.classId !=' ' ">  
            AND ST.CLASS_ID = #{classEntity.classId, jdbcType=VARCHAR}  
        </if>  
        <if test="placeId != null and placeId != '' ">  
            AND ST.PLACE_ID = #{placeId, jdbcType=VARCHAR}  
        </if>  
        <if test="placeEntity != null and placeEntity.placeId != null and placeEntity.placeId != '' ">  
            AND ST.PLACE_ID = #{placeEntity.placeId, jdbcType=VARCHAR}  
        </if>  
        <if test="studentId != null and studentId != '' ">  
            AND ST.STUDENT_ID = #{studentId, jdbcType=VARCHAR}  
        </if>   
    </select> 

2.choose (when otherwise)

<!--  choose(判斷參數(shù)) - 按順序?qū)?shí)體類(lèi) User 第一個(gè)不為空的屬性作為:where條件 -->  
<select id="getUserList_choose" resultMap="resultMap_user" parameterType="com.yiibai.pojo.User">  
    SELECT *  
      FROM User u   
    <where>  
        <choose>  
            <when test="username !=null ">  
                u.username LIKE CONCAT(CONCAT('%', #{username, jdbcType=VARCHAR}),'%')  
            </when >  
            <when test="sex != null and sex != '' ">  
                AND u.sex = #{sex, jdbcType=INTEGER}  
            </when >  
            <when test="birthday != null ">  
                AND u.birthday = #{birthday, jdbcType=DATE}  
            </when >  
            <otherwise>  
            </otherwise>  
        </choose>  
    </where>    
</select>  
<select id="dynamicChooseTest" parameterType="Blog" resultType="Blog">
        select * from t_blog where 1 = 1 
        <choose>
            <when test="title != null">
                and title = #{title}
            </when>
            <when test="content != null">
                and content = #{content}
            </when>
            <otherwise>
                and owner = "owner1"
            </otherwise>
        </choose>
</select>

4.in的用法

? ? <select id="getNewListByLabelID" resultMap="BaseResultMap" parameterType="arraylist">
? ? ? ? SELECT
? ? ? ? <include refid="Base_Column_List"/>
? ? ? ? FROM tb_problem
? ? ? ? WHERE id IN
? ? ? ? <foreach collection="ids" index="index" item="id" separator="," close=")" open="(">
? ? ? ? ? ? #{id}
? ? ? ? </foreach>
? ? </select>

還可以這樣

<select id="queryAllOpenProduct" parameterType="com.tims.open.domain.OpenProductQueryCondition"
? ? resultType="com.tims.open.domain.OpenProduct">
? ? SELECT
? ? ? ? ?*
? ? FROM
? ? ? ? product_db.product p
? ? WHERE
? ? ? ? p.isvalid = 1
? ? <if test="list != null">
? ? ? ? <foreach collection="list" index="index" item="item" separator="," open="AND p.id IN (" close=")">
? ? ? ? ? ? ? ?#{item}
? ? ? ? </foreach>
? ? </if>
</select>
//查詢(xún)condition類(lèi)
Public OpenProductQueryCondition{
? ? private Integer productId; ?
? ? private List<Integer> list;
}

5.模糊查詢(xún)

<!-- ******************** 模糊查詢(xún)的常用的3種方式:********************* -->
? ? <select id="getUsersByFuzzyQuery" parameterType="User" resultType="User">
? ? ? ? select <include refid="columns"/> from users
? ? ? ? <where>
? ? ? ? ? ? <!--
? ? ? ? ? ? ? ? 方法一: 直接使用 % 拼接字符串?
? ? ? ? ? ? ? ? 注意:此處不能寫(xiě)成 ?"%#{name}%" ,#{name}就成了字符串的一部分,
? ? ? ? ? ? ? ? 會(huì)發(fā)生這樣一個(gè)異常: The error occurred while setting parameters,
? ? ? ? ? ? ? ? 應(yīng)該寫(xiě)成: "%"#{name}"%",即#{name}是一個(gè)整體,前后加上%
? ? ? ? ? ? -->
? ? ? ? ? ? <if test="name != null">
? ? ? ? ? ? ? ? name like "%"#{name}"%"
? ? ? ? ? ? </if>
? ? ? ? ? ? <!--方法二: 使用concat(str1,str2)函數(shù)將兩個(gè)參數(shù)連接 -->
? ? ? ? ? ? <if test="phone != null">
? ? ? ? ? ? ? ? and phone like concat(concat("%",#{phone}),"%")
? ? ? ? ? ? </if>
? ? ? ? ? ? <!--方法三: 使用 bind 標(biāo)簽,對(duì)字符串進(jìn)行綁定,然后對(duì)綁定后的字符串使用 like 關(guān)鍵字進(jìn)行模糊查詢(xún) -->
? ? ? ? ? ? <if test="email != null">
? ? ? ? ? ? ? ? <bind name="pattern" value="'%'+email+'%'"/>
? ? ? ? ? ? ? ? and email like #{pattern}
? ? ? ? ? ? </if>
? ? ? ? </where>
? ? </select>

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • springboot jackson自定義序列化和反序列化實(shí)例

    springboot jackson自定義序列化和反序列化實(shí)例

    這篇文章主要介紹了spring boot jackson自定義序列化和反序列化實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Spring Boot集成Druid查看配置是否生效的方法

    Spring Boot集成Druid查看配置是否生效的方法

    本文主要介紹了Spring Boot集成Druid查看配置是否生效的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • 這一次搞懂Spring的XML解析原理說(shuō)明

    這一次搞懂Spring的XML解析原理說(shuō)明

    這篇文章主要介紹了這一次搞懂Spring的XML解析原理說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-08-08
  • Java的四種常見(jiàn)線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解

    Java的四種常見(jiàn)線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解

    這篇文章主要介紹了Java的四種常見(jiàn)線程池及Scheduled定時(shí)線程池實(shí)現(xiàn)詳解,在Java中,我們可以通過(guò)Executors類(lèi)來(lái)創(chuàng)建ScheduledThreadPool,Executors類(lèi)提供了幾個(gè)靜態(tài)方法來(lái)創(chuàng)建不同類(lèi)型的線程池,包括ScheduledThreadPool,需要的朋友可以參考下
    2023-09-09
  • java多線程開(kāi)發(fā)ScheduledExecutorService簡(jiǎn)化方式

    java多線程開(kāi)發(fā)ScheduledExecutorService簡(jiǎn)化方式

    這篇文章主要為大家介紹了java多線程開(kāi)發(fā)ScheduledExecutorService的簡(jiǎn)化方式,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2022-03-03
  • SpringBoot 使用@WebMvcTest測(cè)試MVC Web Controller

    SpringBoot 使用@WebMvcTest測(cè)試MVC Web Controller

    這篇文章主要介紹了SpringBoot 使用@WebMvcTest測(cè)試MVC Web Controller,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • java使用swing繪制國(guó)際象棋棋盤(pán)

    java使用swing繪制國(guó)際象棋棋盤(pán)

    這篇文章主要為大家詳細(xì)介紹了java使用swing繪制國(guó)際象棋棋盤(pán),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法

    IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法

    這篇文章主要介紹了IDEA將Maven項(xiàng)目中指定文件夾下的xml等文件編譯進(jìn)classes的方法,幫助大家更好的利用IDEA進(jìn)行Java的開(kāi)發(fā)學(xué)習(xí),感興趣的朋友可以了解下
    2021-01-01
  • java 字符串分割的三種方法(總結(jié))

    java 字符串分割的三種方法(總結(jié))

    下面小編就為大家?guī)?lái)一篇java 字符串分割的三種方法(總結(jié))。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-11-11
  • 如何在spring boot項(xiàng)目中使用Spring Security的BCryptPasswordEncoder類(lèi)進(jìn)行相同密碼不同密文的加密和驗(yàn)證

    如何在spring boot項(xiàng)目中使用Spring Security的BCryptPasswordE

    本文介紹如何在Spring Boot項(xiàng)目中通過(guò)修改pom.xml引入安全依賴(lài),添加配置類(lèi)以解除默認(rèn)的HTTP請(qǐng)求攔截,以及如何創(chuàng)建BCryptPasswordEncoder對(duì)象進(jìn)行密碼的加密和匹配,通過(guò)這些步驟,可以有效地增強(qiáng)應(yīng)用的安全性
    2023-08-08

最新評(píng)論

广州市| 济源市| 广平县| 溆浦县| 宣化县| 阿拉善右旗| 东阿县| 商都县| 紫金县| 宁都县| 班戈县| 高平市| 宁明县| 天门市| 姜堰市| 冕宁县| 隆德县| 新兴县| 南涧| 丹巴县| 河东区| 无为县| 黄梅县| 保定市| 昌乐县| 康乐县| 新兴县| 逊克县| 鄄城县| 尚义县| 从江县| 永胜县| 阿瓦提县| 满洲里市| 桂平市| 济南市| 遵义县| 武定县| 西乌珠穆沁旗| 杨浦区| 武邑县|