016MyBatis其他动态SQL语法

本文深入探讨了MyBatis框架中的动态SQL语法,包括choose、when、otherwise、where、trim、set等标签的使用技巧,以及如何通过这些标签解决SQL语句中常见的冗余问题,如去除多余的AND、OR或逗号。

除了if与句之外,还有一些动态sql语法。

1、choose、when、otherwise

用xml配置:

<select id="queryNews" databaseId="mysql" resultMap="newsMapper">
        select * from news_inf where status = 'ACTIVE'
        <choose>
            <when test="title != null">
                and news_title regexp #{title}
            </when>
            <when test="content != null">
                and news_content regexp #{content}
            </when>
            <otherwise>
                and news_id in (select min(news_id) from news_inf where status = 'ACTIVE');
            </otherwise>
        </choose>
    </select>

用注解和java配置:

SELECT("n.news_id as id" +
",n.news_title as title," +
"n.news_content as content," +
"n.status");
FROM("news_inf n");
WHERE("status = 'ACTIVE'");
if (title != null)
{
	WHERE("news_title regexp #{title}");
}
else if (content != null)
{
	WHERE("news_content regexp #{content}");
}
else
{
	WHERE("news_id in (select min(news_id) from news_inf where status = 'ACTIVE')") ;
}

2、where、trim

sql中的where语句可以在MyBatis中的Mapper.xml中用where标签来完成,
这个标签最大的好处就是可以去除多余的and或者or。

<select id="queryNews" databaseId="mysql" resultMap="newsMapper">
select * from news_inf
<where>
	<if test="title != null">
		and news_title regexp #{title}
	</if>
	<if test="content != null">
		and news_content regexp #{content}
	</if>
</where>
</select>

而实际上where标签的功能是trim标签实现的.

<trim prefix="where" prefixOverrides="or|and">
    <if test="title != null">
         and news_title regexp #{title}
    </if>
    <if test="content != null">
        and news_content regexp #{content}
    </if>
</trim>

trim标签中有属性
prefix 和 suffix:其属性值分别对应于在这段sql语句(被trim标签包起来的sql语句)的前面或者后面添加的字符串值。
prefixOverrides 和 suffixOverrides:其属性值分别对应与要在这段sql语句中删除哪些不需要的内容。

3、set、trim

与where sql语句像似的是,在update中set语句也可能会多出",",为了方便去掉这个多余的逗号,可以用set标签代替直接写set到sql语句中

<update id="updateNews" databaseId="mysql" >
update news_inf
<set>
	<if test="title != null">
		news_title = #{title},
	</if>
	<if test="content != null">
		news_content = #{content}
	</if>
</set>
where news_id = #{oldId}
</update>

当然也可以用trim标签表示

<update id="updateNews" databaseId="mysql" >
 update news_inf
<trim prefix="set" suffixOverrides=",">
    <if test="title != null">
        news_title = #{title},
    </if>
    <if test="content != null">
        news_content = #{content}
    </if>
 </trim>
where news_id = #{oldId}
</update>

4、trim实现动态插入

当然trim还有一些其他灵活的用处,比如insert语句要插入出很多列,就可以去除可能多出来的“,”

<insert id="insertNews" databaseId="mysql" >
insert into news_inf (
<trim suffixOverrides=",">
    <if test="title != null">
        news_title,
    </if>
    <if test="content != null">
        news_content,
    </if>
    <if test="status != null">
        status
    </if>
</trim>
) values (
<trim suffixOverrides=",">
    <if test="title != null">
        #{title},
    </if>
    <if test="content != null">
        #{content},
    </if>
    <if test="status != null">
        #{status}
    </if>
</trim>
)
</insert>
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值