mybatis中between...and...语句的写法和详解

本文介绍了MyBatis中XML配置文件的查询优化技巧,通过LEFT JOIN实现数据联查,并根据条件动态拼接SQL WHERE子句。示例展示了如何使用IF标签处理brandChannel、startDate和endDate参数,实现类似BETWEEN的效果,提高查询效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、mybatis中xml文件中写法

<select id="selectByBrandChannel"  resultMap="BaseResultMap">
    SELECT
    ct.*,
    cd.*
    FROM
    coupon_detail cd
    LEFT JOIN coupon_template ct ON cd.template_id = ct.template_id
    <if test="brandChannel!=null and brandChannel!=''">
        where cd.brand_channel=#{brandChannel}
    </if>
    <if test="startDate!=null and startDate.trim() neq ''">
        and date_format(cd.create_time,'%Y-%m-%d %H:%i:%s') &gt;= str_to_date(#{startDate},'%Y-%m-%d %H:%i:%s')
    </if>
    <if test="endDate!=null and endDate.trim() neq ''">
        and date_format(cd.create_time,'%Y-%m-%d %H:%i:%s') &lt;= str_to_date(#{endDate},'%Y-%m-%d %H:%i:%s')
    </if>
</select>

以上加红色部分等同between...and...

cd.create_time  between #{startDate} and #{endDate}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值