欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

MyBatis XML去除多余AND|OR前缀或逗号等后缀的操作

时间:2022-03-17 10:55:05|栏目:JAVA代码|点击:

1.通过trim格式化标记set或where功能

2.对于set自动删除最后一个“,”,对于where自动删除最后一个“and|or”

使用示例如下:

1、

select * from user 
<trim prefix="WHERE" prefixOverride="AND |OR">
<if test="userName != null and userName.length()>0"> AND user_name=#{userName}</if>
<if test="loginName != null and loginName.length()>0"> AND login_name=#{loginName}</if>
</trim>

如果userName 为空则最终SQL为:

select * from user where login_name = 'xx'

prefix:前缀

prefixOverride:去掉第一个and或者是or

2、

update user
<trim prefix="set" suffixOverride="," suffix=" where user_id = #{userId} ">
<if test="userName != null and userName.length()>0"> user_name=#{userName} , </if>
<if test="loginName != null and loginName.length()>0"> login_name=#{loginName} , </if>
</trim>

如果userName 为空则最终SQL为:

update user set login_name='xx'  where user_id='xx'

suffixOverride:去掉最后一个逗号(也可以是其他的标记,就像是上面前缀中的and一样)

suffix:后缀

补充:mybatis去除多余的and或者or

啥也不多说了,大家还是直接看代码吧~

<select id="selectBySelective" resultType="xxx.UserInfo">
select
<include refid="Base_Column_List" />
from uc_user
<trim prefix="WHERE (" suffix=")" prefixOverrides="AND |OR ">
<if test="userName != null" >
user_name = #{userName}
</if>
<if test="email != null" >
or email = #{email}
</if>
<if test="phone != null" >
or phone = #{phone}
</if>
<if test="weiboId != null" >
or weibo_id = #{weiboId}
</if>
<if test="wxId != null" >
or wx_id = #{wxId}
</if> 
<if test="qqId != null" >
or qq_id = #{qqId}
</if>
</trim>
and status = 1
</select>

上一篇:SpringMVC 拦截器不拦截静态资源的三种处理方式方法

栏    目:JAVA代码

下一篇:Java泛型类型通配符和C#对比分析

本文标题:MyBatis XML去除多余AND|OR前缀或逗号等后缀的操作

本文地址:http://www.codeinn.net/misctech/196479.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有