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><!-- &為& "為'--><if test="lastName!=null && lastName!=""">and last_name like #{lastName}</if><if test="email!=null and email.trim()!=""">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元素可以用于動態包含需要更新的列,而舍去其它的
? 這里,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表達式中創建一個變量并將其綁定到上下文。
七、可重用的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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MyBatis Generator:代码
- 下一篇: Hibernate之检索方式(HQL/Q