日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

MyBatis-动态SQL

發布時間:2025/3/20 数据库 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 MyBatis-动态SQL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目錄

一、概述

二、if

1.格式

2.實例

三、choose(when,otherwise)

1.格式

2.實例

四、trim(where,set)

1.where

2.trim

3.set

五、foreach

1.in(x,y,z...)條件查詢

2.批量保存

六、bind

七、可重用的sql片段

八、多數據庫支持


一、概述

  • 動態SQL是MyBatis的強大特性之一。極大的簡化了我們拼裝SQL的操作

  • 動態SQL元素和使用JSTL或其它類似的基于XML的文本處理器類似

  • MyBatis采用功能強大的基于OGNL的表達式來簡化操作

    • if

    • choose(when,otherwise)

    • trim(where,set)

      • foreach

二、if

1.格式

<if test=""></if>

2.實例

<!-- 查詢員工,要求,攜帶了哪個字段查詢條件就帶上這個字段的值 --> <!-- public List<Employee> getEmpsByConditionIf(Employee employee); --><select id="getEmpsByConditionIf" resultType="com.itheima.domain.Employee">select * from tbl_employee<!-- where --><where><!-- test:判斷表達式(OGNL)OGNL參照PPT或者官方文檔。c:if test從參數中取值進行判斷遇見特殊符號應該去寫轉義字符:&&:--><if test="id!=null">id=#{id}</if><!-- &amp為& &quot為'--><if test="lastName!=null &amp;&amp; lastName!=&quot;&quot;">and last_name like #{lastName}</if><if test="email!=null and email.trim()!=&quot;&quot;">and email=#{email}</if> <!-- ognl會進行字符串與數字的轉換判斷 "0"==0 --><if test="gender==0 or gender==1">and gender=#{gender}</if></where></select>

三、choose(when,otherwise)

1.格式

<choose><when test=""></when><when test=""></when><otherwise></otherwise> </choose>

2.實例

<!-- public List<Employee> getEmpsByConditionChoose(Employee employee); --><select id="getEmpsByConditionChoose" resultType="com..itheima.domain.Employee">select * from tbl_employee <where><!-- 如果帶了id就用id查,如果帶了lastName就用lastName查,都不帶就查詢gender=0的所有數據;只會進入其中一個 --><choose><when test="id!=null">id=#{id}</when><when test="lastName!=null">last_name like #{lastName}</when><otherwise>gender = 0</otherwise></choose></where></select>

四、trim(where,set)

1.where

? 在使用if表達式進行查詢時,有可能會發生一個問題。

<select id="findActiveBlogLike"resultType="Blog">SELECT * FROM BLOG WHERE <if test="state != null">state = #{state}</if> <if test="title != null">AND title like #{title}</if><if test="author != null and author.name != null">AND author_name like #{author.name}</if> </select>

如果這些條件都沒能匹配上,最終的SQL語句會變成:

SELECT * FROM BLOG ? WHERE

如果僅僅第二個條件匹配成功,最終的SQL語句會變成:

SELECT * FROM BLOG WHERE AND title like ‘someTitle’

這時,就可以使用where標簽來處理上述問題

<select id="findActiveBlogLike"resultType="Blog">SELECT * FROM BLOG <where> <if test="state != null">state = #{state}</if> <if test="title != null">AND title like #{title}</if><if test="author != null and author.name != null">AND author_name like #{author.name}</if></where> </select>

? where元素只會在至少有一個子元素的條件返回 SQL 子句的情況下才去插入“WHERE”子句。而且,若語句的開頭為“AND”或“OR”,where 元素也會將它們去除。

2.trim

? 我們也可以用自定義trim元素來定制where元素的功能。

<!-- prefix給拼串后的整個字符串加一個前綴 prefixOverrides="":前綴覆蓋: 去掉整個字符串前面多余的字符suffix="":后綴suffix給拼串后的整個字符串加一個后綴 suffixOverrides=""后綴覆蓋:去掉整個字符串后面多余的字符 --> <trim prefix="WHERE" prefixOverrides="AND |OR ">... </trim>

3.set

  • set元素可以用于動態包含需要更新的列,而舍去其它的

<update id="updateAuthorIfNecessary">update Author<set><if test="username != null">username=#{username},</if><if test="password != null">password=#{password},</if><if test="email != null">email=#{email},</if><if test="bio != null">bio=#{bio}</if></set>where id=#{id} </update>

? 這里,set 元素會動態前置 SET 關鍵字,同時也會刪掉無關的逗號,因為用了條件語句之后很可能就會在生成的 SQL 語句的后面留下這些逗號。

? set元素的底層為:

<trim prefix="SET" suffixOverrides=",">... </trim>

五、foreach

1.in(x,y,z...)條件查詢

<!--collection:指定要遍歷的集合:list類型的參數會特殊處理封裝在map中,map的key就叫listitem:將當前遍歷出的元素賦值給指定的變量separator:每個元素之間的分隔符open:遍歷出所有結果拼接一個開始的字符close:遍歷出所有結果拼接一個結束的字符index:索引。遍歷list的時候是index就是索引,item就是當前值遍歷map的時候index表示的就是map的key,item就是map的值#{變量名}就能取出變量的值也就是當前遍歷出的元素 --> <!--public List<Employee> getEmpsByConditionForeach(@Param("ids") List<Integer> ids); --><select id="getEmpsByConditionForeach" resultType="com.itheima.domain.Employee">SELECT *FROM tb1_employeeWHERE id IN<foreach collection="ids" close=")" ?separator="," item="items" open="(" >#{items}</foreach></select>

2.批量保存

<!-- 批量保存 --><!--public void addEmps(@Param("emps")List<Employee> emps); --><!--MySQL下批量保存:可以foreach遍歷 ? mysql支持values(),(),()語法--><insert id="addEmps">INSERT INTO tb1_employee(NAME,gender,email,did)VALUES<foreach collection="emps" item="items" separator="," >(#{items.name},#{items.gender},#{items.email},#{items.dept.id})</foreach></insert>

六、bind

  • bind元素可以從OGNL表達式中創建一個變量并將其綁定到上下文。

<!--public List<Employee> getEmpsTestInnerParameter(Employee employee); --> <select id="getEmpsTestInnerParameter" resultType="com.itheima.domain.Employee"><bind name="_name" value="'%' + name + '%'" />SELECT * FROM BLOGWHERE title LIKE #{_name} </select> <!--可以對傳過來的Employee的name參數直接進行模糊查詢而不需要自己加% -->

七、可重用的sql片段

!-- 抽取可重用的sql片段。方便后面引用 1、sql抽取:經常將要查詢的列名,或者插入用的列名抽取出來方便引用2、include來引用已經抽取的sql:3、include還可以自定義一些property,sql標簽內部就能使用自定義的屬性 --><sql id="insertColumn"><if test="_databaseId=='oracle'">employee_id,last_name,email</if><if test="_databaseId=='mysql'">last_name,email,gender,d_id</if></sql><insert id="addEmps" databaseId="mysql">insert into tbl_employee(<include refid="insertColumn"></include>) values<foreach collection="emps" item="emp" separator=",">(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})</foreach> </insert> <sql id="columns">#{profix}.id,#{profix}.name,#{profix}.age </sql><select id="getUser">select<include refid="columns"><property name="prefix" value="u" /></include>from user u where r.id=#{id} </select>

八、多數據庫支持

? 一個配置了“_databaseId”變量的 databaseIdProvider 可用于動態代碼中,這樣就可以根據不同的數據庫廠商構建特定的語句。

<insert id="insert"><selectKey keyProperty="id" resultType="int" order="BEFORE"><if test="_databaseId == 'oracle'">select seq_users.nextval from dual</if><if test="_databaseId == 'db2'">select nextval for seq_users from sysibm.sysdummy1"</if></selectKey>insert into users values (#{id}, #{name}) </insert>

?

總結

以上是生活随笔為你收集整理的MyBatis-动态SQL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。