0
点赞
收藏
分享

微信扫一扫

Mybatis中 用括号括起来where中的部分条件

weipeng2k 2023-10-02 阅读 10

目标

SELECT id, `name`, icon, priority, pid, info, `state`, create_time, update_time
FROM tb_category
WHERE pid is null and state = 1  
 and (`name` like concat(concat('%', '电'), '%') or info like concat(concat('%', '电'), '%') ) 
 and create_time between 2021-01-03T14:02 and 2022-11-03T14:02 
order by update_time desc, priority desc;

原理

使用trim标签实现:

<trim prefix="(" suffix=")">
    
</trim>

示例

<select id="selectCategoryByCondition" resultMap="BaseResultMap" parameterType="com.wego.bean.query.CategoryQuery">
    select
    <include refid="Base_Column_List"/>
    from tb_category
    <where>
        <if test="pid0 == null">
            and pid is null
        </if>
        <if test="pid0 != null">
            and pid = #{pid0}
        </if>
        <if test="state0 != null and state0 != -1">
            and state = #{state0,jdbcType=INTEGER}
        </if>
        <trim prefix="and (" suffix=")">
            <if test="name0 != null">
                `name` like concat(concat('%', #{name0,jdbcType=VARCHAR}), '%')
            </if>
            <if test="info0 != null">
                or info like concat(concat('%', #{info0,jdbcType=VARCHAR}), '%')
            </if>
        </trim>
        <if test="datemin != null and datemax != null">
            and create_time between
            #{datemin,jdbcType=TIMESTAMP}
            and
            #{datemax,jdbcType=TIMESTAMP}
        </if>
    </where>
    order by update_time desc, priority desc
</select>
举报

相关推荐

0 条评论