動態(tài)SQL
定義:根據(jù)不同條件拼接SQLy語句,實現(xiàn)對數(shù)據(jù)庫更準的操作。
實現(xiàn)方式:映射器配置文件或者注解
常用動態(tài)SQL元素:
- if元素:判斷語句,但條件分支判斷
- choose元素:when、otherwise;多條件分支判斷,等同于Java中的switch
- trim元素:where,set;輔助元素,用于處理一些SQL拼接的問題
- foreach元素:foreach元素用于遍歷集合或數(shù)組
- bind元素:自定義上下文變量,傳遞參數(shù)
實體類中定義int還是Integer:一般情況下應(yīng)該定義為Integer,選擇的條件是判斷該值能不能為0,因為在動態(tài)sql中判讀是否輸入的條件是0(引用數(shù)據(jù)類型為null)
if元素
語法:
<if test="條件"> 滿足條件的語句</if>
如果需要多重條件進行判斷在test中用or或and連接;該表達式也稱為OGNL表達式(對象圖導(dǎo)航語言),中的字符串比較不能用equals比較;屬性名直接寫
eg:
<select id="findStudnet" resultType="Student" parameterType="Student">
select * from studnet where
<if test="classId != 0">
classid = #{classId}
</if>
<if test="ssex != null">
and ssex = #{ssex}
</if>
</select>
解決辦法一:在where后面加 1=1;但是where會對表中的數(shù)據(jù)逐行進行判斷,因此效率低
解決辦法二:
上面的案例存在問題:當傳入一個參數(shù)時正常,當傳入0個參數(shù)時where多余,當傳入兩個參數(shù)缺少and,解決辦法是引入where標簽;第一個and會自動去掉
<select id="findStudnet" resultType="Student" parameterType="Student">
<!-- sql片段的使用 -->
<include refid="sql1"></include>
<where>
<if test="classId != 0">
and classid = #{classId}
</if>
<if test="ssex != null">
and ssex = #{ssex}
</if>
</where>
</select>
choose 、when 、otherwise 元素
語法:
<choose>
<when test="條件">滿足條件的語句</when>
<otherwise>滿足其他條件條件的語句</otherwise>
</choose>
eg:
<select id="findStudentChoose" resultType="Student" parameterType="Student">
select * from student
<where>
<choose>
<when test="sname != null">
sname = #{sname}
</when>
<when test="birthday != null">
birthday = #{birthday}
</when>
<when test="ssex != ssex">
ssex = #{ssex}
</when>
<!-- <otherwise>
sid = 10
</otherwise> -->
</choose>
</where>
</select>
注意:
- when中傳入?yún)?shù)不論是否有結(jié)果匹配到后面的when都不會執(zhí)行(等同于java中switch的break,前面的條件都為執(zhí)行otherwise中的內(nèi)容執(zhí)行,不是必須的)
trim 、where 、set 元素
where語法:
<where>
<if test="條件">滿足條件的語句</if>
</where>
eg:
<where>
<if test="title != null">
and title = #{title}
</if>
</where>
where注意:
where 元素只會在至少有一個子元素的條件返回 SQL 子句的情況下才去插入“WHERE”子句。而且,若語句的開頭為“AND”或“OR”,where 元素也會將它們?nèi)コ?/p>
set語法:
<set>
<if test = "條件">滿足條件的語句</if>
</set>
eg:
<set>
<if test = "title != null">
title = #{title}
</if>
</set>
注意:
set 標簽元素主要是用在更新操作的時候,它的主要功能和 where 標簽元素其實是差不 多的,主要是在包含的語句前輸出一個 set,然后如果包含的語句是以逗號結(jié)束的話將會把該逗號忽略,如果 set 包含的內(nèi)容為空的話則會出錯。有了 set 元素就可以動態(tài)的更新那些修改了的字段。
trim語法:
<trim prefix = “”suffixOverrides = “” prefixOverrides=“”suffix=“”></trim>
<!--
prefix:在開始添加一個
prefixOverrides:開始去掉一個
suffix:結(jié)束添加一個
suffixOverrides:結(jié)束去掉一個
-->
eg:
<update id="findStudentTrim" parameterType="Student">
update student
<trim prefix="set" suffixOverrides=",">
<if test="sname != null">
sname = #{sname},
</if>
<if test="birthday != null">
birthday = #{birthday},
</if>
<if test="ssex != null">
ssex = #{ssex},
</if>
</trim>
where sid = #{sid}
</update>
foreach 元素
語法:
<foreach item="" index="" collection="" open="" separator="" close=""></foreach>
<!--
item:循環(huán)中的當前元素
index:當前循環(huán)位置的下標
collection:方法傳遞的參數(shù),一個數(shù)組(array)或是一個集合(list,l為小寫)
open:以什么符號開頭
close:以什么符號結(jié)束
separator:元素間的間隔符號
-->
eg:
<select id="findStudentArray" resultType="Student">
select * from student
<where>
<foreach item="sid" collection="array" open="sid in (" separator="," close=")">
#{sid}
</foreach>
</where>
</select>
批量插入數(shù)據(jù):將對象存入集合中,再通過 對象.屬性 將值賦給數(shù)據(jù)庫中的字段
<insert id="insertStudentForeach">
insert into student (sname,birthday,ssex,classid)
<foreach collection="list" open=" values" separator="," item="stu">
(#{stu.sname},#{stu.birthday},#{stu.ssex},#{stu.classId})
</foreach>
</insert>
mybatis中的模糊查詢:
五種模糊查詢:文章來源:http://www.zghlxwxcb.cn/news/detail-786980.html
- 方式一:業(yè)務(wù)層處理傳入’張%’ 不推薦,耦合度高
- 方式二:通過MySQL的函數(shù)進行模糊處理,推薦
- ${}:純字符串替換,不防止sql注入;不能用不能防止sql注入
- 方式四:通過sql語法進行特殊字符串拼接:#{v}“%”
- 方式五:bind標簽 官方推薦;
<select id="selectStudnetLike" parameterType="String" resultMap="Student">
<!-- 方式一:業(yè)務(wù)層處理傳入'張%' 不推薦,耦合度高-->
<!-- select * from student where sname like #{v} -->
<!-- 方式二:通過MySQL的函數(shù)進行模糊處理 推薦 -->
select * from student where sname like concat(#{v},'%')
<!-- 方式三:不能用
#{}:參數(shù)位置用?進項占位(sql預(yù)處理),然后用PreparedStatement進行傳參處理
${}:純字符串替換,不防止sql注入
-->
select * from student where sname like '${v}%'
<!-- 方式四:通過sql語法進行特殊字符串拼接 -->
select * from student where sname like #{v}"%"
<!-- 方式五:bind標簽 官方推薦 -->
<bind name="x" value="_parameter+'%'"/>
select * from student where sname like #{x}%
</select>
bind 元素
語法:文章來源地址http://www.zghlxwxcb.cn/news/detail-786980.html
<bind name=“” value=“_parameter”>
</bind>
<!--
name:自定義變量的變量名
value:自定義變量的變量值
_parameter:傳遞進來的參數(shù)
-->
eg:
<bind name = “name1” value = “ '%' + _parameter+ '%' ”></bind>
到了這里,關(guān)于mybatis之動態(tài)sql、if\choose\when\otherwise\trim\where\set\foreach\bind有案例的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!