Mybatis的动态sql语句的查询
if
<select id="findActiveBlogWithTitleLike"resultType="Blog">SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null">AND title like #{title}</if> </select>?如果想可選地通過(guò)"title"和"author"兩個(gè)條件搜索,首先,改變語(yǔ)句的名稱讓它更具實(shí)際意義;然后只要加入另一個(gè)條件即可。
<select id="findActiveBlogLike"resultType="Blog">SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <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>?choose, when, otherwise
MyBatis 提供了 choose 元素,它有點(diǎn)像 Java 中的 switch 語(yǔ)句。提供了"title"就按"title"查找,提供了"author"就按"author"查找,若兩者都沒(méi)有提供,就返回所有符合條件的BLOG
<select id="findActiveBlogLike"resultType="Blog">SELECT * FROM BLOG WHERE state = ‘ACTIVE’<choose><when test="title != null">AND title like #{title}</when><when test="author != null and author.name != null">AND author_name like #{author.name}</when><otherwise>AND featured = 1</otherwise></choose> </select>?trim, where, set
MyBatis 有一個(gè)簡(jiǎn)單的處理,這在90%的情況下都會(huì)有用。
<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 元素知道只有在一個(gè)以上的if條件有值的情況下才去插入"WHERE"子句。而且,若最后的內(nèi)容是"AND"或"OR"開(kāi)頭的,where 元素也知道如何將他們?nèi)コ?/p>
如果 where 元素沒(méi)有按正常套路出牌,我們還是可以通過(guò)自定義 trim 元素來(lái)定制我們想要的功能。比如,和 where 元素等價(jià)的自定義 trim 元素為:
<trim prefix="WHERE" prefixOverrides="AND |OR ">... </trim>?
<select id="selectEmps" resultType="Emp"><!-- SELECT * FROM emp e, dept d WHERE e.deptno=d.deptno -->select * from emp e<!-- <if test="ename != null"> where e.ename like #{ename} </if> --><trim prefix="where" prefixOverrides="and|or"><if test="ename != null">and e.ename like #{ename}</if><if test="sal != null">and e.sal > #{sal}</if><if test="empnoList != null">and e.empno in<foreach collection="empnoList" item="empno" open="(" close=")"separator=", " index="a">#{empno}</foreach></if></trim> </select>?prefixOverrides 屬性會(huì)忽略通過(guò)管道分隔的文本序列(注意此例中的空格也是必要的)。它帶來(lái)的結(jié)果就是所有在 prefixOverrides 屬性中指定的內(nèi)容將被移除,并且插入 prefix 屬性中指定的內(nèi)容。
類似的用于動(dòng)態(tài)更新語(yǔ)句的解決方案叫做 set。set 元素可以被用于動(dòng)態(tài)包含需要更新的列,而舍去其他的。
?
<update id="updateEmp" parameterType="Emp">update emp e <set><if test="ename != null">e.ename=#{ename},</if><if test="job != null">e.job=#{job},</if><if test="mgr != null">e.mgr=#{mgr},</if><if test="hiredate != null">e.hiredate=#{hiredate},</if><if test="sal != null">e.sal=#{sal},</if><if test="comm != null">e.comm=#{comm},</if></set><where>e.empno=#{empno}</where></update>?這里,set 元素會(huì)動(dòng)態(tài)前置 SET 關(guān)鍵字,同時(shí)也會(huì)消除無(wú)關(guān)的逗號(hào),因?yàn)橛昧藯l件語(yǔ)句之后很可能就會(huì)在生成的賦值語(yǔ)句的后面留下這些逗號(hào)。
foreach
動(dòng)態(tài) SQL 的另外一個(gè)常用的必要操作是需要對(duì)一個(gè)集合進(jìn)行遍歷,通常是在構(gòu)建 IN 條件語(yǔ)句的時(shí)候。比如:
<select id="selectPostIn" resultType="domain.blog.Post">SELECT *FROM POST PWHERE ID in<foreach item="item" index="index" collection="list"open="(" separator="," close=")">#{item}</foreach> </select>?foreach 元素的功能是非常強(qiáng)大的,它允許你指定一個(gè)集合,聲明可以用在元素體內(nèi)的集合項(xiàng)和索引變量。它也允許你指定開(kāi)閉匹配的字符串以及在迭代中間放置分隔符。這個(gè)元素是很智能的,因此它不會(huì)偶然地附加多余的分隔符。
注意 你可以將一個(gè) List 實(shí)例或者數(shù)組作為參數(shù)對(duì)象傳給 MyBatis,當(dāng)你這么做的時(shí)候,MyBatis 會(huì)自動(dòng)將它包裝在一個(gè) Map 中并以名稱為鍵。List 實(shí)例將會(huì)以"list"作為鍵,而數(shù)組實(shí)例的鍵將是"array"。
<select id="selectEmpsByList" resultType="Emp">select e.*,sq_test.nextval from emp e<trim prefix="where" prefixOverrides="and|or">and e.empno in<foreach collection="list" item="empno" open="(" close=")"separator=", " index="a">#{empno}</foreach></trim> </select>?
轉(zhuǎn)載于:https://www.cnblogs.com/zuo72/p/8408609.html
總結(jié)
以上是生活随笔為你收集整理的Mybatis的动态sql语句的查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: [转]Eclipse插件开发之基础篇(2
- 下一篇: python连接redis sentin