Mybatis使用<where>标签管理条件筛选标签或替换where 1=1的作用

注:xxx为某某某的意思

基础代码

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.xxx.xxxMapper">
    <select id="findByx" parameterType="int"
            resultType="com.xx.xxx.xx">

        select * from xxx

    </select>
</mapper>

要对select * from xxx;附加条件筛选,

1,,用where 1=1加筛选标签:select * from xxx 加 where 1=1 加<if><choose><when>等筛选标签。

select * from where 1=1
<if test="xxx!=xx">
    and SQL1
</if>

2,,用where标签内嵌筛选标签:select * from xxx 加<where><if><choose><when></where>。

select * from xxx
<where>
    <if test="xxx!=xx">
        and SQL1
    </if>
    <if test="xxx==x">
        and SQL2
    </if>
</where>

如果where内有筛选标签成立,比如xxx!=xx

那么最终SQL为 select * from xxx SQL1;

where标签会对其中的内容检查,让最终SQL不以and和or这些关键字直接作开头接上。

比如 select * from xxx and SQL1;------> select * from xxx SQL1;

如果where内没有筛选标签成立,则最终SQL直接没有where部分,

那么最终SQL为 Lselect * from xxx;

另外<trim>也可以实现上述功能,见下一篇。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值