MyBatis复杂mapper,根据条件数组中的数据进一步判断、从子查询中进一步筛选数据

MyBatis复杂mapper,根据条件数组中的数据进一步判断、从子查询中进一步筛选数据

需要从子查询中进一步获取数据,数组中的数据进一步判断

<select id="queryByPage" resultMap="ThingModelVO">
        select
      *    
        from t_object_model
        <where>
            creator_number = #{queryDTO.loginName}
            <if test="queryDTO.categoryId != null and queryDTO.categoryId > 0">
                AND category_id = #{queryDTO.categoryId}
            </if>
            <if test="queryDTO.state != null and queryDTO.state.size > 1">
                AND state in
                <foreach collection="queryDTO.state" index="index" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="queryDTO.state == null or queryDTO.state.size == 0">
                or state in (1,20)</if>
            <if test="queryDTO.state != null and queryDTO.state.size == 1">
                <foreach collection="queryDTO.state" index="index" item="item">
                    <if test="item == 0">
                        and state = 0
                    </if>
                    <if test="item != 0">
                        or state = #{item}
                    </if>
                </foreach>
            </if>
            <if test="queryDTO.keyword != null and queryDTO.keyword != ''">
                AND INSTR(name,#{queryDTO.keyword})
                OR INSTR(creator_name,#{queryDTO.keyword})
                OR INSTR(creator_number,#{queryDTO.keyword})
            </if>
 
        </where>
    </select>

需要从子查询中进一步筛选数据

<select id="queryByPage" resultMap="ThingModelVO">
select * from (select
        *
        from t_object_model
        <where>
            creator_number = #{queryDTO.loginName}
            <if test="queryDTO.state == null or queryDTO.state.size == 0">
                or state in (1,20)
            </if>
            <if test="queryDTO.categoryId != null and queryDTO.categoryId > 0">
                AND category_id = #{queryDTO.categoryId}
            </if>
        </where>
        ) tempTable
        <where>
            1 = 1
            <if test="queryDTO.state != null and queryDTO.state.size > 1">
                AND state in
                <foreach collection="queryDTO.state" index="index" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="queryDTO.keyword != null and queryDTO.keyword != ''">
                AND (INSTR(name,#{queryDTO.keyword})
                OR INSTR(creator_name,#{queryDTO.keyword}))
        </where>
</select>

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现mysql + mybatis不定层数的树状结构数据条件查询,可以通过使用WITH RECURSIVE语句来实现。WITH RECURSIVE语句可以在SQL实现递归查询,适用于树状结构数据查询。 下面是实现不定层数的树状结构数据条件查询的步骤: 1. 创建Mapper接口,并定义一个方法来查询树状结构数据。 ``` public interface TreeMapper { List<Tree> selectTreeByCondition(Map<String, Object> params); } ``` 2. 在Mapper.xml文件,使用WITH RECURSIVE语句来实现不定层数的树状结构数据查询。 ``` <select id="selectTreeByCondition" resultMap="treeMap" parameterType="map"> with recursive cte as ( select * from tree where parent_id = #{parentId} union select t.* from tree t inner join cte on cte.id = t.parent_id ) select * from cte where name like CONCAT('%',#{name},'%'); </select> ``` 这个查询语句,使用了WITH RECURSIVE语句来进行递归查询。首先查询出指定parentId的树状结构数据,然后通过inner join和递归查询语句来查询其所有子节点的信息。最后,使用WHERE子句来添加查询条件。 3. 在Java代码调用Mapper接口的方法来查询树状结构数据。 ``` Map<String, Object> params = new HashMap<>(); params.put("parentId", 1); params.put("name", "test"); List<Tree> treeList = treeMapper.selectTreeByCondition(params); ``` 这个代码会查询根节点为1,并且节点名称包含“test”的所有子节点,并返回一个包含所有子节点信息的List集合。 通过这种方式,我们可以方便地实现不定层数的树状结构数据条件查询,而MyBatis的灵活性和易用性也为我们提供了很大的帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值