動态sql:
以前的sql如果你有3列資料,你要修改資料、那你就要寫修改的第一列的,一二列的,一二三列的,這樣操作很麻煩。
但是如果你用sql預處理的話他就會根據你輸入你對象,來判斷你要修改的參數,讓sql變得很靈活。
注意事項!!
封裝屬性的時候 有一定不能用基本資料類型 要用封裝資料類型
基本資料類型沒有指派的時候是零
封裝資料類型 沒有指派的時候是null
if:
test 條件,可以取屬性值或者參數 可以用and加入多個判斷
set:
update生成的,可以生成 set 關鍵字 還有就是去除最後逗号
<update id="updKc" parameterType="com.dao.kecheng">
-- 動态sql好處讓sql變得靈活,修改他可以進行判斷,根據你傳入的對象的參數來判斷你要修改的值
update kecheng
<set>
-- 這裡用《set》可以幫助你自動去除 逗号多參數是就有好處
<if test="kname!=null">
kname=#{kname},
</if>
</set>
where kid=#{kid}
-- 這裡要注意的是動态sql的值都是鍵值對,對象參數對應資料庫的的的參數
</update>
trim:
去除或添加
suffixOverrides 末尾去除符号(如果你聲明的話,他就會你最後的值後面加個,例如(name,)無法識别sql語句)
suffix 末尾添加符号
prefix 前面添加符号
prefixOverrides 前面去除符号
<insert id="inster" parameterType="com.dao.Student">
insert into student(
<trim suffixOverrides=",">
-- 這裡要注意取出末尾逗号,不取出的話如果你在插入時,如果你插入一個值不去除逗号他就會預設你後面還要插入會報sql文法錯誤
<if test="address!=null">
address,
</if>
<if test="sname!=null">
sname,
</if>
<if test="cid">
cid,
</if>
</trim>
)values(
-- 還要注意這些都是在括号中執行的
<trim suffixOverrides=",">
<if test="address!=null">
#{address},
</if>
<if test="sname!=null">
#{sname},
</if>
<if test="cid">
#{cid},
</if>
</trim>
)
</insert>
查詢動态sql查詢可以根據你選擇的條件進行查詢
List<Student> seleStu(String sname,String address,Integer cid,Integer sid); //這裡可以根據你輸入的值來判斷你要查詢的值,
不選擇輸入null就可以這裡要注意要用封裝資料類型,
因為基本資料類型不為空,為0
<select id="seleStu" resultType="com.dao.Student">
select * from student
<where>
<trim suffixOverrides="and">
<if test="param1!=null">
-- 根據你所輸入的參數來決定你要查詢的條件
sname=#{param1}and
</if>
<if test="param2!=null">
address=#{param2}and
</if>
<if test="param3!=null">
cid=#{param3}and
</if>
<if test="param4!=null">
sid=#{param4}
</if>
</trim>
</where>
-- 這裡是查詢玩排序按降序
ORDER BY sid DESC
</select>
如果上面動态查詢中發生: Parameter 'uid' not found. Available parameters are [arg3, arg2, param5, arg5, arg1, arg0, param3, ] 錯誤
可以試試下面寫法
and寫前面<where>标記會自動去除and
<select id="sleuse" resultType="com.sun.sm.Dao.Urers">
SELECT*FROM urers
<where>
<if test="uid !=null">
uid=#{arg0}
</if>
<if test="arg1 !=null">
and username=#{arg1}
</if>
<if test="arg2 !=null">
and password=#{arg2}
</if>
<if test="arg3 !=null">
and jine=#{arg3}
</if>
<if test="arg4 !=null">
and logerror=#{arg4}
</if>
<if test="arg5 !=null">
and utype=#{arg5}
</if>
</where>
</select>
查詢語句 根據你的輸出選擇要輸出的條件
<select id="selectStudentRowSumByLike" parameterType="java.lang.String" resultType="java.lang.Integer">
select count(*) from student where
<choose>
<when test="param1 == 'sname'">
sname
</when>
<when test="param1 == 'address'">
address
</when>
<when test="param1 == 'email'">
email
</when>
<when test="param1 == 'phone'">
phone
</when>
</choose>
like #{param2}
</select>
動态查詢
<select id="selectOrderAllByUseridLimit" resultMap="BaseResultMap">
SELECT * FROM os_order
WHERE userid=#{param1}
<choose>
<when test="param5==null"> 判斷這個條件成立就進這裡
AND createtime >= #{param2}
AND createtime <= #{param3}
<if test="param4!='ALL'"> 判斷這個條件成立就進這裡
AND orderstate = #{param4}
</if>
</when>
<otherwise> 條件不成立就進這裡 用這個條件
AND orderid=#{param5}
</otherwise>
</choose>
ORDER BY createtime DESC LIMIT #{param6},#{param7}
</select>