1. if
創(chuàng)新互聯(lián)公司是一家專業(yè)從事成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、網(wǎng)頁設(shè)計的品牌網(wǎng)絡(luò)公司。如今是成都地區(qū)具影響力的網(wǎng)站設(shè)計公司,作為專業(yè)的成都網(wǎng)站建設(shè)公司,創(chuàng)新互聯(lián)公司依托強(qiáng)大的技術(shù)實力、以及多年的網(wǎng)站運(yùn)營經(jīng)驗,為您提供專業(yè)的成都網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)及網(wǎng)站設(shè)計開發(fā)服務(wù)!
This statement would provide an optional text search type of functionality.
<select id="findActiveBlogWithTitleLike" resultType="Blog">
SELECT * FROM BLOG
WHERE state = 'ACTIVE'
<if test="title != null">
AND title = #{title}
</if>
</select>
2. choose, when, otherwise
Sometimes we don’t want all of the conditionals to apply, instead we want to choose only one case among many options.
<select id="findActiveBlogLike" resultType="Blog">
SELECT * FROM BLOG WHERE state = 'ACTIVE'
<choose>
<when test="title != null">
AND title = #{title}
</when>
<when test="author != null and author.name != null">
AND author_name = #{author.name}
</when>
<otherwise>
AND featured = 1
</otherwise>
</choose>
</select>
3. set
The set element can be used to dynamically include columns to update,and leave out others.
A:
<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}
B:
</update>
<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>
where id=#{id}
</update>
Ifbiois null
A.update Author setusername='xx',password='xx',email ='xx'[not has, due to <set> tag will remove it] where id ='x'
B.update Author set username ='xx',password='xx',email ='xx', where id ='x'
4. foreach
The foreach element is very powerful, and allows you to specify a collection, declare item and index variables that can be used inside the body of the element. It also allows you to specify opening and closing strings, and add a separator to place in between iterations. The element is smart in that it won’t accidentally append extra separators.
<select id="selectPostIn" resultType="domain.blog.Post">
SELECT *
FROM POST P
WHERE ID in
<foreach item="item" index="index" collection="list"
open="(" separator="," close=")">
#{item}
</foreach>
</select>
1. trim
<trim prefix="WHERE" prefixOverrides="AND |OR ">
...</trim>
Sample:
select * from user
<trim prefix="WHERE"prefixoverride="AND |OR">
<if test="name != nullandname.length()>0"> AND name=#{name}</if>
<if test="gender != null and gender.length()>0"> AND gender=#{gender}</if>
</trim>
Ifname andgenderare not null, the SQL will be like this:select * from user where name = 'xx' and gender = 'xx'.
prefix:前綴
prefixoverride: remove 1st AND/OR
update user
<trim prefix="set"suffixoverride="," suffix=" where id = #{id} ">
<if test="name != null and name.length()>0"> name=#{name} , </if>
<if test="gender != null and gender.length()>0"> gender=#{gender}, </if>
</trim>
Ifname andgenderare not null, the SQL will be like this:update user set name='xx' , gender='xx' where id='x'.
suffix: 后綴
suffixoverride: remove last character “,”
where:search condition
Note
A)Escape character in MyBatis XML
Sample:
where id >= 1(wrong)
where id >= 1(correct)
B)mybatis中#{}和${}的區(qū)別
http://www.cnblogs.com/baizhanshi/p/5778692.html
By default, using the #{} syntax will cause MyBatis to generate PreparedStatement properties and set the values safely against the PreparedStatement parameters (e.g. ?). While this is safer, faster and almost always preferred, sometimes you just want to directly inject a string unmodified into the SQL Statement. For example, for ORDER BY, you might use something like this:
ORDER BY ${columnName}
Here MyBatis won't modify or escape the string.
NOTE It's not safe to accept input from a user and supply it to a statement unmodified in this way. This leads to potential SQL Injection attacks and therefore you should either disallow user input in these fields, or always perform your own escapes and checks.
分享名稱:MyBatis常用語法和注意事項
瀏覽地址:http://muchs.cn/article26/pjjpjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、定制開發(fā)、企業(yè)建站、App開發(fā)、建站公司、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)