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

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

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

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

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

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

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

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

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

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

操作步驟如下:

1.在Mapper.xml中輸入要查詢的SQL語句

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

語句如下:

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

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

3.text文檔中輸出

語句如下:

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

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

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

方法一:修改SQL語句

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

給a_sid 取別名為sid

xml中語句修改如下:

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

原查詢語句:

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

修改為:

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

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

SQL語句為:

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

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

如下圖:

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

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條件查詢總結(jié)

1.if條件語句

<!--  if(判斷參數(shù)) - 將實(shí)體類不為空的屬性作為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í)體類 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>
//查詢condition類
Public OpenProductQueryCondition{
? ? private Integer productId; ?
? ? private List<Integer> list;
}

5.模糊查詢

<!-- ******************** 模糊查詢的常用的3種方式:********************* -->
? ? <select id="getUsersByFuzzyQuery" parameterType="User" resultType="User">
? ? ? ? select <include refid="columns"/> from users
? ? ? ? <where>
? ? ? ? ? ? <!--
? ? ? ? ? ? ? ? 方法一: 直接使用 % 拼接字符串?
? ? ? ? ? ? ? ? 注意:此處不能寫成 ?"%#{name}%" ,#{name}就成了字符串的一部分,
? ? ? ? ? ? ? ? 會(huì)發(fā)生這樣一個(gè)異常: The error occurred while setting parameters,
? ? ? ? ? ? ? ? 應(yīng)該寫成: "%"#{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)簽,對字符串進(jìn)行綁定,然后對綁定后的字符串使用 like 關(guān)鍵字進(jì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à)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Spring Boot集成Druid查看配置是否生效的方法

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

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

    這一次搞懂Spring的XML解析原理說明

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

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

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

    java多線程開發(fā)ScheduledExecutorService簡化方式

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

    SpringBoot 使用@WebMvcTest測試MVC Web Controller

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

    java使用swing繪制國際象棋棋盤

    這篇文章主要為大家詳細(xì)介紹了java使用swing繪制國際象棋棋盤,文中示例代碼介紹的非常詳細(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的開發(fā)學(xué)習(xí),感興趣的朋友可以了解下
    2021-01-01
  • java 字符串分割的三種方法(總結(jié))

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

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

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

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

最新評(píng)論

陆良县| 宁明县| 乌什县| 武清区| 玉溪市| 双牌县| 和平县| 郎溪县| 阜平县| 克山县| 红桥区| 若尔盖县| 洛南县| 资溪县| 昆明市| 保德县| 红原县| 新乐市| 秀山| 军事| 修水县| 米易县| 龙陵县| 昭平县| 汕头市| 岱山县| 内黄县| 黎城县| 额敏县| 崇仁县| 台中县| 封丘县| 美姑县| 肃宁县| 凤翔县| 吉首市| 上林县| 兴隆县| 赣州市| 新干县| 乐山市|