动态sql
测试所用的数据表结构
- if语句
<select id="select" parameterType="com.alibaba.fastjson.JSONObject" resultType="com.alibaba.fastjson.JSONObject">
select * from facere_bases where
<if test="libId !=null ">
libId=#{libId}
</if>
<if test="libName !=null">
and libName=#{libName}
</if>
</select>
if语句为判断语句,如果test中返回为true,则执行 if 中的语句,否则不执行。
-
where语句
但如果只有 if 语句的话,就会出现很多问题,
如:libId = null and libName != null
这样就会出现
select * from tables where and libName="..."
显然,这样的代码肯定不正确。
又如: libId = null and libName =null
就会出现
select * from tables where
显然,这样的代码也会有问题
所以就引出了where语句
<select id="select" parameterType="com.alibaba.fastjson.JSONObject" resultType="com.alibaba.fastjson.JSONObject"> select * from facere_bases <where> <if test="libId !=null "> and libId=#{libId} </if> <if test="libName !=null"> and libName=#{libName} </if> </where> </select>
当出先所有的条件均为null的情况下;where会自动消失
即出现:libId=null and libName=null 时
最终sql语句为
select * from tables
同时where语句对于多条件的并联也很友好
<if test="libId !=null "> and libId=#{libId} //此处的and可以省去(因为是第一个if ,但建议还是加入,这样之后编写代码就不会考虑要不要加and的问题了) </if> <if test="libName !=null"> and libName=#{libName} </if>
在第一个if中,也加入了and,但where语句会自动的将开口的第一个 and/or 字符去除。
关于这一点,也可以理解为:
<where> == where 1=1
-
set 语句
set语句与where语句很相似,也是为了使得多条set有更好的连接性
<update id="update" parameterType="com.alibaba.fastjson.JSONObject"> update facere_bases set <if test="libName !=null"> libName=#{libName}, </if> <if test="introduce !=null"> introduce=#{introduce} </if> <where> <if test="libId !=null "> libId=#{libId} </if> </where> </update>
如上图,如果没使用<set>标签,就会出现很多问题
如:libName != null and introduce = null
这样最终的sql语句就会变成
update tables set libName=? , where .....
显然,因为where之前不应当出现‘,’所以这句话就报错了。
而<set>解决了这一问题,他会将出现在最后一个的’,'给删除掉,如果where语句将第一个and消除掉一样
<update id="update" parameterType="com.alibaba.fastjson.JSONObject"> update facere_bases <set> <if test="libName !=null"> libName=#{libName}, </if> <if test="introduce !=null"> introduce=#{introduce}, </if> </set> <where> <if test="libId !=null "> and libId=#{libId} </if> </where> </update>
同样,笔者建议所有的后面都加‘,’
-
choose(when,otherwise)语句
choose(when,otherwise)与java中的switch语句相同,是通过<when>来进行筛选,从上而下,选择第一个满足条件的一个,如果满足,则直接跳出<choose>,如果没有满足的<when>则会进入到<otherwise>中,相当于java-switch语句中的default一样。
<delete id="delete" parameterType="com.alibaba.fastjson.JSONObject"> delete from facere_bases <where> <choose> <when test="libName != null "> and libName=#{libName} </when> <when test="introduce != null "> <!-- 模糊查找 --> and introduce like "%${introduce}%" </when> <otherwise> 1=0 </otherwise> </choose> </where> </delete>
-
trim语句
trim语句,是用于对边界进行处理的
- prefix:前缀
- prefixoverride:去掉第一个指定的符号
- suffix:后缀
- suffixoverride:去掉最后一个指定的符号
实际上,<where>标签与<set>标签本质上是trim语句
<where>相当于:
<trim prefix="WHERE" prefixOverrides="AND |OR "> ... </trim>
<set>相当于:
<trim prefix="SET" suffixOverride="," > ... </trim>
使用trim执行insert语句
<insert id="insert" parameterType="com.alibaba.fastjson.JSONObject"> insert into facere_bases <trim prefix="(" suffix=")" suffixOverrides=","> <if test="libId !=null"> libId, </if> <if test="libName !=null"> libName, </if> <if test="introduce !=null"> introduce, </if> <if test="createTime !=null"> createTime, </if> <if test="updateTime !=null"> updateTime, </if> </trim> <trim prefix="values(" suffix=")" suffixOverrides=","> <if test="libId !=null"> #{libId}, </if> <if test="libName !=null"> #{libName}, </if> <if test="introduce !=null"> #{libintroduce}, </if> <if test="createTime !=null"> #{createTime}, </if> <if test="updateTime !=null"> #{updateTime}, </if> </trim> </insert>
-