日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

MyBatis動態語句

作者:W-琑 更新時間: 2024-03-07 編程語言

目錄

1.if和where標簽

2.set標簽

3.trim標簽

4.choose/when/otherwise標簽

5.foreach標簽

6.sql標簽

7.總結

1)where標簽的作用:

2)if標簽的作用:

3)set標簽的作用:

4)trim標簽的作用:

5)choose/when/otherwise標簽的作用:

6)foreach標簽的作用:

7)sql標簽的作用:


1.if和where標簽

<!-- List<Employee> selectEmployeeByCondition(Employee employee); -->
<select id="selectEmployeeByCondition" resultType="employee">
    select emp_id,emp_name,emp_salary from t_emp
    <!-- where標簽會自動去掉“標簽體內前面多余的and/or” -->
    <where>
        <!-- 使用if標簽,讓我們可以有選擇的加入SQL語句的片段。這個SQL語句片段是否要加入整個SQL語句,就看if標簽判斷的結果是否為true -->
        <!-- 在if標簽的test屬性中,可以訪問實體類的屬性,不可以訪問數據庫表的字段 -->
        <if test="empName != null">
            <!-- 在if標簽內部,需要訪問接口的參數時還是正常寫#{} -->
            or emp_name=#{empName}
        </if>
        <if test="empSalary &gt; 2000">
            or emp_salary>#{empSalary}
        </if>
        <!--
         第一種情況:所有條件都滿足 WHERE emp_name=? or emp_salary>?
         第二種情況:部分條件滿足 WHERE emp_salary>?
         第三種情況:所有條件都不滿足 沒有where子句
         -->
    </where>
</select>

2.set標簽

<!-- void updateEmployeeDynamic(Employee employee) -->
<update id="updateEmployeeDynamic">
    update t_emp
    <!-- set emp_name=#{empName},emp_salary=#{empSalary} -->
    <!-- 使用set標簽動態管理set子句,并且動態去掉兩端多余的逗號 -->
    <set>
        <if test="empName != null">
            emp_name=#{empName},
        </if>
        <if test="empSalary &lt; 3000">
            emp_salary=#{empSalary},
        </if>
    </set>
    where emp_id=#{empId}
    <!--
         第一種情況:所有條件都滿足 SET emp_name=?, emp_salary=?
         第二種情況:部分條件滿足 SET emp_salary=?
         第三種情況:所有條件都不滿足 update t_emp where emp_id=?
            沒有set子句的update語句會導致SQL語法錯誤
     -->
</update>

3.trim標簽

使用trim標簽控制條件部分兩端是否包含某些字符

  • prefix屬性:指定要動態添加的前綴
  • suffix屬性:指定要動態添加的后綴
  • prefixOverrides屬性:指定要動態去掉的前綴,使用“|”分隔有可能的多個值
  • suffixOverrides屬性:指定要動態去掉的后綴,使用“|”分隔有可能的多個值
<!-- List<Employee> selectEmployeeByConditionByTrim(Employee employee) -->
<select id="selectEmployeeByConditionByTrim" resultType="com.atguigu.mybatis.entity.Employee">
    select emp_id,emp_name,emp_age,emp_salary,emp_gender
    from t_emp
    
    <!-- prefix屬性指定要動態添加的前綴 -->
    <!-- suffix屬性指定要動態添加的后綴 -->
    <!-- prefixOverrides屬性指定要動態去掉的前綴,使用“|”分隔有可能的多個值 -->
    <!-- suffixOverrides屬性指定要動態去掉的后綴,使用“|”分隔有可能的多個值 -->
    <!-- 當前例子用where標簽實現更簡潔,但是trim標簽更靈活,可以用在任何有需要的地方 -->
    <trim prefix="where" suffixOverrides="and|or">
        <if test="empName != null">
            emp_name=#{empName} and
        </if>
        <if test="empSalary &gt; 3000">
            emp_salary>#{empSalary} and
        </if>
        <if test="empAge &lt;= 20">
            emp_age=#{empAge} or
        </if>
        <if test="empGender=='male'">
            emp_gender=#{empGender}
        </if>
    </trim>
</select>

4.choose/when/otherwise標簽

在多個分支條件中,僅執行一個。

  • 從上到下依次執行條件判斷
  • 遇到的第一個滿足條件的分支會被采納
  • 被采納分支后面的分支都將不被考慮
  • 如果所有的when分支都不滿足,那么就執行otherwise分支
<!-- List<Employee> selectEmployeeByConditionByChoose(Employee employee) -->
<select id="selectEmployeeByConditionByChoose" resultType="com.atguigu.mybatis.entity.Employee">
    select emp_id,emp_name,emp_salary from t_emp
    where
    <choose>
        <when test="empName != null">emp_name=#{empName}</when>
        <when test="empSalary &lt; 3000">emp_salary &lt; 3000</when>
        <otherwise>1=1</otherwise>
    </choose>
    
    <!--
     第一種情況:第一個when滿足條件 where emp_name=?
     第二種情況:第二個when滿足條件 where emp_salary < 3000
     第三種情況:兩個when都不滿足 where 1=1 執行了otherwise
     -->
</select>

5.foreach標簽

例如批量插入:

<!--
    collection屬性:要遍歷的集合
    item屬性:遍歷集合的過程中能得到每一個具體對象,在item屬性中設置一個名字,將來通過這個名字引用遍歷出來的對象
    separator屬性:指定當foreach標簽的標簽體重復拼接字符串時,各個標簽體字符串之間的分隔符
    open屬性:指定整個循環把字符串拼好后,字符串整體的前面要添加的字符串
    close屬性:指定整個循環把字符串拼好后,字符串整體的后面要添加的字符串
    index屬性:這里起一個名字,便于后面引用
        遍歷List集合,這里能夠得到List集合的索引值
        遍歷Map集合,這里能夠得到Map集合的key
 -->
<foreach collection="empList" item="emp" separator="," open="values" index="myIndex">
    <!-- 在foreach標簽內部如果需要引用遍歷得到的具體的一個對象,需要使用item屬性聲明的名稱 -->
    (#{emp.empName},#{myIndex},#{emp.empSalary},#{emp.empGender})
</foreach>

批量更新時需要注意

上面批量插入的例子本質上是一條SQL語句,而實現批量更新則需要多條SQL語句拼起來,用分號分開。也就是一次性發送多條SQL語句讓數據庫執行。此時需要在數據庫連接信息的URL地址中設置:

atguigu.dev.url=jdbc:mysql:///mybatis-example?allowMultiQueries=true
<!-- int updateEmployeeBatch(@Param("empList") List<Employee> empList) -->
<update id="updateEmployeeBatch">
    <foreach collection="empList" item="emp" separator=";">
        update t_emp set emp_name=#{emp.empName} where emp_id=#{emp.empId}
    </foreach>
</update>

6.sql標簽

抽取重復的sql片段

<!-- 使用sql標簽抽取重復出現的SQL片段 -->
<sql id="mySelectSql">
    select emp_id,emp_name,emp_age,emp_salary,emp_gender from t_emp
</sql>

引用已抽取的sql片段

<!-- 使用include標簽引用聲明的SQL片段 -->
<include refid="mySelectSql"/>

7.總結

1)where標簽的作用:

1.自動 添加 where關鍵字
2.自動 去掉 標簽體前面多余的and和or關鍵字

2)if標簽的作用:

1.根據if標簽判斷的結果,選擇是否加入sql語句的片段

3)set標簽的作用:

1.自動 添加 set關鍵字
2.自動 去掉 兩端多余的逗號

4)trim標簽的作用:

1.動態添加或去掉前后綴

5)choose/when/otherwise標簽的作用:

1.在多個分支條件中,僅執行一個

6)foreach標簽的作用:

1.遍歷

7)sql標簽的作用:

1.抽取重復的sql片段

原文鏈接:https://blog.csdn.net/weixin_69134926/article/details/136487399

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新