亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis中的動態(tài)sql問題

 更新時間:2023年02月27日 14:13:28   作者:一入Java深似海丶  
這篇文章主要介紹了mybatis中的動態(tài)sql問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Mybatis框架的動態(tài)SQL技術是一種根據特定條件動態(tài)拼裝SQL語句的功能,它存在的意義是通過標簽解決拼接SQL語句字符串時的問題

1、if(常用)

if:根據標簽中test屬性所對應的表達式決定標簽中的內容是否需要拼接到SQL中

/**
     * 多條件查詢
     */
    List<Emp> getEmpByCondition(Emp emp);

當empName為null和“ ”時,會拼接and age,此時會報錯,可以1=1恒成立解決。

  • 用and表示&&(并且)
  • emp_name是字段名
  • if只有test標簽且必須使用
<!--List<Emp> getEmpListByMoreTJ(Emp emp);-->
    <select id="getEmpListByMoreTJ" resultType="Emp">
select * from t_emp where 1=1
<if test="empName!= '' and empName!= null">
    and emp_name = #{empName}
</if>
<if test="age != '' and age != null">
    and age = #{age}
</if>
<if test="sex != '' and sex != null">
    and sex = #{sex}
</if>
</select>
@Test
    public void testGetEmpByCondition(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        List<Emp> list = mapper.getEmpByCondition(new Emp(null, "陳智", 33, "女", null));
        System.out.println(list);
    }
select eid,emp_name,age,sex,email from t_emp where emp_name = ? and age = ? or sex = ?

2、where

當where標簽中有內容時,會自動生成where關鍵字,并且將內容前多余的and或or去掉(在程序執(zhí)行后生成的sql會將內容前多余的and或or去掉)

and寫在第二句是為了和上面拼接,寫在第一句是和下面拼接(即固定有elect * from t_emp emp_name= ?)

<select id="getEmpListByMoreTJ2" resultType="Emp">
    select * from t_emp
<where>
<if test="empName != '' and empName != null">
    emp_name = #{empName }
</if>
<if test="age != '' and age != null">
    and age = #{age}
</if>
<if test="sex != '' and sex != null">
    and sex = #{sex}
</if>
</where>
</select>

當where標簽中沒有內容時(或者使用getEmpByCondition傳入的值全為null或“ ”),此時where標簽沒有任何效果。則直接SQL語句為select * from t_emp

--------------------分割--------------------

where標簽不能將其中內容后面多余的and或or去掉:(會報錯)

語句為select * from t_emp emp_name= ? and

<select id="getEmpListByMoreTJ2" resultType="Emp">
    select * from t_emp
<where>
<if test="empName != '' and empName != null">
    emp_name = #{empName } and
</if>
<if test="age != '' and age != null">
    age = #{age} and
</if>
<if test="sex != '' and sex != null">
    sex = #{sex}
</if>
</where>
</select>

3、trim

where的增強

若標簽中有內容時:

  • prefix|suffix:將trim標簽中內容前面或后面添加指定內容
  • suffixOverrides|prefixOverrides:將trim標簽中內容前面或后面去掉指定內容

若標簽中沒有內容時,trim標簽也沒有任何效果(跟上面的where一樣)

<!--List<Emp> getEmpByCondition(Emp emp);-->
    <select id="getEmpByCondition" resultType="Emp">
        select <include refid="empColumns"></include> from t_emp
        <trim prefix="where" suffixOverrides="and|or">
            <if test="empName != null and empName != ''">
                emp_name = #{empName} and
            </if>
            <if test="age != null and age != ''">
                age = #{age} or
            </if>
            <if test="sex != null and sex != ''">
                sex = #{sex} and
            </if>
            <if test="email != null and email != ''">
                email = #{email}
            </if>
        </trim>
    </select>

4.choose、when、otherwise

相當于if...else if...else

/**
     * 測試choose、when、otherwise
     */
    List<Emp> getEmpByChoose(Emp emp);

when至少要有一個,otherwise最多只能有一個

與第一點if的區(qū)別:choose只會滿足一個條件便退出,一個不滿足則執(zhí)行otherwise

<!--List<Emp> getEmpByChoose(Emp emp);-->
    <select id="getEmpByChoose" resultType="Emp">
        select * from t_emp
        <where>
            <choose>
                <when test="empName != null and empName != ''">
                    emp_name = #{empName}
                </when>
                <when test="age != null and age != ''">
                    age = #{age}
                </when>
                <when test="sex != null and sex != ''">
                    sex = #{sex}
                </when>
                <when test="email != null and email != ''">
                    email = #{email}
                </when>
                <otherwise>
                    did = 1
                </otherwise>
            </choose>
        </where>
    </select>
@Test
    public void testGetEmpByChoose(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        List<Emp> list = mapper.getEmpByChoose(new Emp(null, "", null, "", ""));
        System.out.println(list);
    }

5、foreach

  • collection:設置需要循環(huán)的數組或集合
  • item:表示數組或集合中的每一個數據
  • separator:循環(huán)體之間的分割符
  • open:foreach標簽所循環(huán)的所有內容的開始符
  • close:foreach標簽所循環(huán)的所有內容的結束符

5.1批量刪除

/**
     * 通過數組實現批量刪除
     */
    int deleteMoreByArray(@Param("eids") Integer[] eids);
<!--int deleteMoreByArray(@Param("eids") Integer[] eids);-->
    <delete id="deleteMoreByArray">
        delete from t_emp where
        <foreach collection="eids" item="eid" separator="or">
            eid = #{eid}
        </foreach>
        <!--
            delete from t_emp where eid in
            <foreach collection="eids" item="eid" separator="," open="(" close=")">
                #{eid}
            </foreach>
        -->
    </delete>
@Test
    public void testDeleteMoreByArray(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        int result = mapper.deleteMoreByArray(new Integer[]{12,13,14,15});
        System.out.println(result);
    }

5.2批量添加

/**
     * 通過list集合實現批量添加
     */
    int insertMoreByList(@Param("emps") List<Emp> emps);
<!--int insertMoreByList(@Param("emps") List<Emp> emps);-->
    <insert id="insertMoreByList">
        insert into t_emp values
        <foreach collection="emps" item="emp" separator=",">
            (null,#{emp.empName},#{emp.age},#{emp.sex},#{emp.email},null)
        </foreach>
    </insert>

Arrays.asList該方法是將數組轉化成List集合的方法 

如果你的List只是用來遍歷,就用Arrays.asList()。

如果你的List還要添加或刪除元素,還是乖乖地new一個java.util.ArrayList,然后一個一個的添加元素。

@Test
    public void testInsertMoreByList(){
        SqlSession sqlSession = SqlSessionUtils.getSqlSession();
        DynamicSQLMapper mapper = sqlSession.getMapper(DynamicSQLMapper.class);
        Emp emp1 = new Emp(null,"a1",23,"男","123@qq.com");
        Emp emp2 = new Emp(null,"a2",23,"男","123@qq.com");
        Emp emp3 = new Emp(null,"a3",23,"男","123@qq.com");
        List<Emp> emps = Arrays.asList(emp1, emp2, emp3);
        System.out.println(mapper.insertMoreByList(emps));
    }

6、sql標簽

設置SQL片段:<sql id="empColumns">eid,emp_name,age,sex,email</sql>

引用SQL片段:<include refid="empColumns"></include>

總結

以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • java中javamail收發(fā)郵件實現方法

    java中javamail收發(fā)郵件實現方法

    這篇文章主要為大家詳細介紹了java中javamail收發(fā)郵件實現方法,實例分析了javamail的使用方法與相關注意事項,非常具有實用價值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Maven中dependency和plugins的繼承與約束

    Maven中dependency和plugins的繼承與約束

    這篇文章主要介紹了Maven中dependency和plugins的繼承與約束,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • IDEA生成patch和使用patch的方法實現

    IDEA生成patch和使用patch的方法實現

    比如你本地修復的 bug,需要把增量文件發(fā)給客戶,很多場景下大家都需要手工整理修改的文件,并整理好目錄,這個很麻煩,那有沒有簡單的技巧呢?本文主要介紹了IDEA生成patch和使用patch的方法實現,感興趣的可以了解一下
    2023-08-08
  • springboot如何讀取自定義屬性

    springboot如何讀取自定義屬性

    大家好,本篇文章主要講的是springboot如何讀取自定義屬性,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-02-02
  • Java源碼解析HashMap簡介

    Java源碼解析HashMap簡介

    今天小編就為大家分享一篇關于Java源碼解析HashMap簡介,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • 基于java線程池讀取單個SQL數據庫表

    基于java線程池讀取單個SQL數據庫表

    這篇文章主要為大家詳細介紹了基于java線程池讀取單個SQL數據庫表,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • SpringBoot使用WebJars統一管理靜態(tài)資源的方法

    SpringBoot使用WebJars統一管理靜態(tài)資源的方法

    這篇文章主要介紹了SpringBoot使用WebJars統一管理靜態(tài)資源的方法,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Java編程線程同步工具Exchanger的使用實例解析

    Java編程線程同步工具Exchanger的使用實例解析

    這篇文章主要介紹了Java編程線程同步工具Exchanger的使用實例解析,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下
    2018-02-02
  • solr 配置中文分析器/定義業(yè)務域/配置DataImport功能方法(測試用)

    solr 配置中文分析器/定義業(yè)務域/配置DataImport功能方法(測試用)

    下面小編就為大家?guī)硪黄猻olr 配置中文分析器/定義業(yè)務域/配置DataImport功能方法(測試用)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09
  • 使用@CachePut?更新數據庫和更新緩存

    使用@CachePut?更新數據庫和更新緩存

    這篇文章主要介紹了使用@CachePut?更新數據庫和更新緩存方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12

最新評論