【MyBatis】学习纪要六:动态SQL
2019獨角獸企業(yè)重金招聘Python工程師標準>>>
引言
動態(tài)SQL:Dynamic SQL。
本節(jié)我們來通過 MyBatis 的官方文檔進行學習。
Description(描述)
官方描述如下:
One of the most powerful features of MyBatis has always been its Dynamic SQL capabilities. If you have any experience with JDBC or any similar framework, you understand how painful it is to conditionally concatenate strings of SQL together, making sure not to forget spaces or to omit a comma at the end of a list of columns. Dynamic SQL can be downright painful to deal with.
While working with Dynamic SQL will never be a party, MyBatis certainly improves the situation with a powerful Dynamic SQL language that can be used within any mapped SQL statement.
The Dynamic SQL elements should be familiar to anyone who has used JSTL or any similar XML based text processors. In previous versions of MyBatis, there were a lot of elements to know and understand. MyBatis 3 greatly improves upon this, and now there are less than half of those elements to work with. MyBatis employs powerful OGNL based expressions to eliminate most of the other elements:
● if ● choose (when, otherwise) ● trim (where, set) ● foreach
My View
上面這段話,總結起來就三點:
-
MyBatis 在動態(tài)SQL這塊很強大。
-
你需要會 OGNL 表達式
-
if/choose/trim/foreach
OGNL
用法看這里:OGNL指南
另外,支持轉義符,所以,看一下轉義:
if/where/trim/choose/foreach
- if 表示 判斷
- where
我們總會在if標簽里寫AND,那么當?shù)谝粋€不存在時就會出問題: sql: ...WHERE AND ...
因此 where 是解決這個問題的
<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>- trim
有人會說了,既然寫在前面有問題,那可不可以寫在后面呢?當然可以。
來看些 trim 的用法:
<trim prefix="WHERE" prefixOverrides="AND |OR ">... </trim>-----<trim prefix="SET" suffixOverrides=",">... </trim>總結一下: prefix:前面加什么 prefixOverrides:前面忽略掉什么 suffix:后綴 suffixOverrides:后面忽略掉什么
- choose
- foreach
遍歷一個集合。
collection:指定要遍歷的集合: list類型的參數(shù)會特殊處理封裝在map中,map的key就叫l(wèi)ist item:將當前遍歷出的元素賦值給指定的變量 separator:每個元素之間的分隔符 open:遍歷出所有結果拼接一個開始的字符 close:遍歷出所有結果拼接一個結束的字符 index:索引。遍歷list的時候是index就是索引,item就是當前值
WHERE ID in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}</foreach>set
<update id="testUpdate">UPDATEuser<set><if test="user.name != null">name = #{user.name},</if><if test="user.age != null and user.age >= 0">age = #{user.age}</if></set><where><if test="user.id != null and user.id > 0">id = #{user.id}</if></where></update>批量插入
- 方法一:
- 方法二:
注意
1、數(shù)據(jù)庫的url應該如下配置:
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis_demo?allowMultiQueries=true2、此方法返回的影響行數(shù)不正確。
==> Preparing: INSERT INTO user(name, age) VALUES( ? , ? ) ; INSERT INTO user(name, age) VALUES( ? , ? ) ==> Parameters: n3(String), 11(Integer), n4(String), 12(Integer) <== Updates: 1 Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@64fc097e] rs num : 1
_parameter and _databaseId
_parameter,參數(shù) _databaseId,當前數(shù)據(jù)庫的Id
bind
關于數(shù)據(jù)綁定,MyBatis官網(wǎng)提供的例子如下:
<select id="selectBlogsLike" resultType="Blog"><bind name="pattern" value="'%' + _parameter.getTitle() + '%'" />SELECT * FROM BLOGWHERE title LIKE #{pattern} </select>引用sql
有些重復的,可以抽取出來
后記
測試代碼:DynamicSQL-Demo
需要說明的是:為了保持測試風格,有些返回值類型不正確,導致測試報錯,但這并不影響著一節(jié)的學習,因為你能夠看到sql和結果。
轉載于:https://my.oschina.net/fengwenyi/blog/1801918
總結
以上是生活随笔為你收集整理的【MyBatis】学习纪要六:动态SQL的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring cloud config
- 下一篇: 洛谷P2866 [USACO06NOV]