Mybatis的<where><trim><set>标签使用和排重

来源:互联网 发布:n卡负优化a卡战未来 编辑:程序博客网 时间:2024/05/08 07:38

trim标签可以很好地兼容where和set的功能

只要你在trim标签中的prefix中使用对应的关键字,就可以达到相应的作用

所以where和set可以不用啦


先对比一下mybatis中trim的几个参数的作用

prefixOverrides :是取消前面的多余内容,比如where语句中的and

suffixOverrides :是取消后缀的多余内容,比如set中的逗号

先说prefixOverrides

<sql id="whereExcludeIp"><if test="exd_id !=null and exd_id !=''">AND exi.exd_id = #{exd_id}</if><if test="ip_addr_outer !=null and ip_addr_outer !=''">AND exi.ip_addr_outer like CONCAT('%',#{ip_addr_outer},'%')</if><if test="port_outer !=null and port_outer !=''">AND exi.port_outer like CONCAT('%',#{port_outer},'%')</if><if test="ip_addr_inner !=null and ip_addr_inner !=''">AND exi.ip_addr_inner like CONCAT('%',#{ip_addr_inner},'%')</if></sql>

使用见下面

<!-- 查询受限ip信息 --><select id="findExcludeIpInfo"  parameterType="Map" resultType="Map"><include refid= "selExcludeIp" /><include refid= "fromExcludeIp" /><trim prefix="WHERE" prefixOverrides="AND|OR"><include refid= "whereExcludeIp" /> </where></select>

接下来是suffixOverrides

<update id="updateExcludeIpInfo" parameterType="Map">UPDATE tb_exclude_ip<trim prefix="SET" prefixOverrides=",">        <if test="used_status !=null and used_status !='' ">                   used_status = #{used_status},                </if>               <if test="used_status !=null and used_status !='' ">                   used_status = #{used_status},                </if>    </set>WHERE exd_id = #{exd_id}</update>
上面的代码中的第二个if语句中的逗号可能无用,所以这样可以将它失效。


0 0
原创粉丝点击