好程序員Java分享Mybatis必會的動態SQL,前言:java
Mybatis可謂是java開發者必須會的一項技能。MyBatis 的強大特性之一即是它的動態 SQL。若是你有使用 JDBC 或其它相似框架的經驗,你就能體會到根據不一樣條件拼接 SQL 語句的痛苦。例如拼接時要確保不能忘記添加必要的空格,還要注意去掉列表最後一個列名的逗號。利用動態 SQL 這一特性能夠完全擺脫這種痛苦。程序員
Mybatis動態sql
mybatis 動態SQL,經過 if, choose, when, otherwise, trim, where, set, foreach等標籤,可組合成很是靈活的SQL語句,從而在提升 SQL 語句的準確性的同時,也大大提升了開發人員的效率。本文主要介紹這幾個動態SQL.sql
具體示例
if標籤 if就是用來對輸入映射的字段進行判斷 通常是非空判斷 null 和""。數組
- <!-- 案例1:動態sql之if -->
- <select id="selectUsersIf" parameterType="user" resultType="user">
- select * from users where 1=1
- <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>
- <if test="sex!=null and sex!=''">and sex = #{sex} </if>
- </select>
動態SQL <where /> 至關於 where關鍵字 <where />能夠自動處理第一個前and 或者or。 當條件都沒有的時候 where也不會加上 。mybatis
- <!-- 案例2:動態sql之where 能夠自動處理第一個前and 或者or。當條件都沒有的時候 where也不會加上 -->
- <select id="selectUsersWhere" parameterType="user" resultType="user">
- select * from users
- <where>
- <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>
- <if test="sex!=null and sex!=''">and sex = #{sex} </if>
- </where>
choose—when--when--otherwise when能夠多個 otherwise只能有一個 相似於 switch case。框架
需求:輸入用戶id 按照用戶id進行精確查找 其餘條件不看 沒有輸入 id 用戶名模糊查找 都沒有的話 查詢id=1的用戶ide
- <!-- 案例3:動態sql之choose—when when otherwise -->
-
- <select id="selectUsersChoose" parameterType="user" resultType="user">
- select * from users
- <where>
- <choose>
- <when test="uid!=null"> uid=#{uid}</when>
- <when test="uname!=null and uname!=''"> uname like "%"#{uname}"%"</when>
- <otherwise>uid=1</otherwise>
-
- </choose>
- </where>
- </select>
動態sql之set 代替set關鍵字 set標籤能夠幫助咱們去掉最後一個逗號ui
- <update id="updateSet" parameterType="user">
-
- update users
-
- <set>
- <if test="uname!=null and uname!=''"> uname =#{uname},</if>
- <if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>
- <if test="sex!=null and sex!=''"> sex =#{sex},</if>
- <if test="birthday!=null"> birthday =#{birthday},</if>
- </set>
- where uid=#{uid}
- </update>
Trim,trim代替wherespa
- <!-- 案例5:動態sql之trim 代替where -->
- <select id="selectUsersTrimWhere" parameterType="user" resultType="user">
- select * from users
- <!--
- prefix:指添加前綴修飾
- suffix:添加後綴修飾
- prefixOverrides:去掉前綴修飾
- suffixOverrides:去掉後綴修飾
- -->
- <trim prefix="where" prefixOverrides="and|or" >
- <if test="uname!=null and uname!=''"> and uname like "%"#{uname}"%" </if>
- <if test="sex!=null and sex!=''">and sex = #{sex} </if>
- </trim>
- </select>
Trim代替set:對象
- <!-- 案例6:動態sql之trim 代替set -->
- <update id="updateTrimSet" parameterType="user">
-
- update users
-
- <trim prefix="set" suffixOverrides="," suffix="where uid=#{uid}" >
- <if test="uname!=null and uname!=''"> uname =#{uname},</if>
- <if test="upwd!=null and upwd!=''"> upwd =#{upwd},</if>
- <if test="sex!=null and sex!=''"> sex =#{sex},</if>
- <if test="birthday!=null"> birthday =#{birthday},</if>
- </trim>
-
Foreach來遍歷集合
- <!--案例7:動態sql之foreach 遍歷數組 -->
- <select id="selectUsersForeachArray" resultType="user">
- select * from users where uid in
- <!--
- collection:要遍歷的集合
- item:當前正在遍歷的對象的變量名
- open:開始遍歷
- close:結束便利
- index:下標
- separator:分割
- -->
- <foreach collection="array" item="item" open="(" close=")" index="index" separator=",">
- #{item}
- </foreach>
-
- </select>
總結
熟練掌握以上Mysql的動態SQL,咱們能夠更駕輕就熟的完成個人功能,寫更少的代碼,實現更多的功能。