欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用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異常處理之異常顯示的頁(yè)面問(wèn)題

    SpringBoot異常處理之異常顯示的頁(yè)面問(wèn)題

    這篇文章主要介紹了SpringBoot異常處理異常顯示的頁(yè)面的問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-09-09
  • 一文了解Java中record和lombok的使用對(duì)比

    一文了解Java中record和lombok的使用對(duì)比

    Java的 record 關(guān)鍵字是Java 14中引入的一個(gè)新的語(yǔ)義特性。Lombok 是一個(gè)Java庫(kù),可以自動(dòng)生成一些已知的模式為Java字節(jié)碼。本文我們將探討各種使用情況,包括java record 的一些限制。對(duì)于每個(gè)例子,我們將看到Lombok如何派上用場(chǎng),并比較這兩種解決方案
    2022-07-07
  • Java通過(guò)MyBatis框架對(duì)MySQL數(shù)據(jù)進(jìn)行增刪查改的基本方法

    Java通過(guò)MyBatis框架對(duì)MySQL數(shù)據(jù)進(jìn)行增刪查改的基本方法

    MyBatis框架由Java的JDBC API進(jìn)一步封裝而來(lái),在操作數(shù)據(jù)庫(kù)方面效果拔群,接下來(lái)我們就一起來(lái)看看Java通過(guò)MyBatis框架對(duì)MySQL數(shù)據(jù)進(jìn)行增刪查改的基本方法:
    2016-06-06
  • java實(shí)現(xiàn)通用分頁(yè)(后端)

    java實(shí)現(xiàn)通用分頁(yè)(后端)

    這篇文章主要介紹了java實(shí)現(xiàn)通用分頁(yè)(后端)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-12-12
  • java如何實(shí)現(xiàn)抽取json文件指定字段值

    java如何實(shí)現(xiàn)抽取json文件指定字段值

    這篇文章主要介紹了java如何實(shí)現(xiàn)抽取json文件指定字段值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。
    2022-06-06
  • SpringCloud微服務(wù)熔斷器Hystrix使用詳解

    SpringCloud微服務(wù)熔斷器Hystrix使用詳解

    這篇文章主要介紹了Spring Cloud Hyxtrix的基本使用,它是Spring Cloud中集成的一個(gè)組件,在整個(gè)生態(tài)中主要為我們提供服務(wù)隔離,服務(wù)熔斷,服務(wù)降級(jí)功能,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • eclipse創(chuàng)建項(xiàng)目沒(méi)有dynamic web的解決方法

    eclipse創(chuàng)建項(xiàng)目沒(méi)有dynamic web的解決方法

    最近上課要用到eclipse,要用到Dynamic web project.但是我下載的版本上沒(méi)有.接下來(lái)就帶大家了解 eclipse創(chuàng)建項(xiàng)目沒(méi)有dynamic web的解決方法,文中有非常詳細(xì)的圖文示例,需要的朋友可以參考下
    2021-06-06
  • scala 匿名函數(shù)案例詳解

    scala 匿名函數(shù)案例詳解

    Scala支持一級(jí)函數(shù),函數(shù)可以用函數(shù)文字語(yǔ)法表達(dá),即(x:Int)=> x + 1,該函數(shù)可以由一個(gè)叫作函數(shù)值的對(duì)象來(lái)表示,這篇文章主要介紹了scala 匿名函數(shù)詳解,需要的朋友可以參考下
    2023-03-03
  • Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例

    Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例

    本文主要介紹了Springboot整合阿里巴巴SMS的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • SpringBoot設(shè)置首頁(yè)(默認(rèn)頁(yè))跳轉(zhuǎn)功能的實(shí)現(xiàn)方案

    SpringBoot設(shè)置首頁(yè)(默認(rèn)頁(yè))跳轉(zhuǎn)功能的實(shí)現(xiàn)方案

    這篇文章主要介紹了SpringBoot設(shè)置首頁(yè)(默認(rèn)頁(yè))跳轉(zhuǎn)功能,本文通過(guò)兩種方案,給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07

最新評(píng)論