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

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

MyBatis動(dòng)態(tài)語(yǔ)句

作者:W-琑 更新時(shí)間: 2024-03-07 編程語(yǔ)言

目錄

1.if和where標(biāo)簽

2.set標(biāo)簽

3.trim標(biāo)簽

4.choose/when/otherwise標(biāo)簽

5.foreach標(biāo)簽

6.sql標(biāo)簽

7.總結(jié)

1)where標(biāo)簽的作用:

2)if標(biāo)簽的作用:

3)set標(biāo)簽的作用:

4)trim標(biāo)簽的作用:

5)choose/when/otherwise標(biāo)簽的作用:

6)foreach標(biāo)簽的作用:

7)sql標(biāo)簽的作用:


1.if和where標(biāo)簽

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

2.set標(biāo)簽

<!-- void updateEmployeeDynamic(Employee employee) -->
<update id="updateEmployeeDynamic">
    update t_emp
    <!-- set emp_name=#{empName},emp_salary=#{empSalary} -->
    <!-- 使用set標(biāo)簽動(dòng)態(tài)管理set子句,并且動(dòng)態(tài)去掉兩端多余的逗號(hào) -->
    <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=?
            沒(méi)有set子句的update語(yǔ)句會(huì)導(dǎo)致SQL語(yǔ)法錯(cuò)誤
     -->
</update>

3.trim標(biāo)簽

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

  • prefix屬性:指定要?jiǎng)討B(tài)添加的前綴
  • suffix屬性:指定要?jiǎng)討B(tài)添加的后綴
  • prefixOverrides屬性:指定要?jiǎng)討B(tài)去掉的前綴,使用“|”分隔有可能的多個(gè)值
  • suffixOverrides屬性:指定要?jiǎng)討B(tài)去掉的后綴,使用“|”分隔有可能的多個(gè)值
<!-- 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屬性指定要?jiǎng)討B(tài)添加的前綴 -->
    <!-- suffix屬性指定要?jiǎng)討B(tài)添加的后綴 -->
    <!-- prefixOverrides屬性指定要?jiǎng)討B(tài)去掉的前綴,使用“|”分隔有可能的多個(gè)值 -->
    <!-- suffixOverrides屬性指定要?jiǎng)討B(tài)去掉的后綴,使用“|”分隔有可能的多個(gè)值 -->
    <!-- 當(dāng)前例子用where標(biāo)簽實(shí)現(xiàn)更簡(jiǎn)潔,但是trim標(biāo)簽更靈活,可以用在任何有需要的地方 -->
    <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標(biāo)簽

在多個(gè)分支條件中,僅執(zhí)行一個(gè)。

  • 從上到下依次執(zhí)行條件判斷
  • 遇到的第一個(gè)滿足條件的分支會(huì)被采納
  • 被采納分支后面的分支都將不被考慮
  • 如果所有的when分支都不滿足,那么就執(zhí)行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>
    
    <!--
     第一種情況:第一個(gè)when滿足條件 where emp_name=?
     第二種情況:第二個(gè)when滿足條件 where emp_salary < 3000
     第三種情況:兩個(gè)when都不滿足 where 1=1 執(zhí)行了otherwise
     -->
</select>

5.foreach標(biāo)簽

例如批量插入:

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

批量更新時(shí)需要注意

上面批量插入的例子本質(zhì)上是一條SQL語(yǔ)句,而實(shí)現(xiàn)批量更新則需要多條SQL語(yǔ)句拼起來(lái),用分號(hào)分開(kāi)。也就是一次性發(fā)送多條SQL語(yǔ)句讓數(shù)據(jù)庫(kù)執(zhí)行。此時(shí)需要在數(shù)據(jù)庫(kù)連接信息的URL地址中設(shè)置:

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標(biāo)簽

抽取重復(fù)的sql片段

<!-- 使用sql標(biāo)簽抽取重復(fù)出現(xiàn)的SQL片段 -->
<sql id="mySelectSql">
    select emp_id,emp_name,emp_age,emp_salary,emp_gender from t_emp
</sql>

引用已抽取的sql片段

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

7.總結(jié)

1)where標(biāo)簽的作用:

1.自動(dòng) 添加 where關(guān)鍵字
2.自動(dòng) 去掉 標(biāo)簽體前面多余的and和or關(guān)鍵字

2)if標(biāo)簽的作用:

1.根據(jù)if標(biāo)簽判斷的結(jié)果,選擇是否加入sql語(yǔ)句的片段

3)set標(biāo)簽的作用:

1.自動(dòng) 添加 set關(guān)鍵字
2.自動(dòng) 去掉 兩端多余的逗號(hào)

4)trim標(biāo)簽的作用:

1.動(dòng)態(tài)添加或去掉前后綴

5)choose/when/otherwise標(biāo)簽的作用:

1.在多個(gè)分支條件中,僅執(zhí)行一個(gè)

6)foreach標(biāo)簽的作用:

1.遍歷

7)sql標(biāo)簽的作用:

1.抽取重復(fù)的sql片段

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

  • 上一篇:沒(méi)有了
  • 下一篇:沒(méi)有了
欄目分類
最近更新