mybatis+pageHelper实现1多对查询,查询条件一方和多方都有

问题

pageHelper的原理是默认执行分页中对应数量条数的sql,所以在mybatis的1对多查询中,如果直接书写,会查询出少于你主表条数的结果,产生问题,并且有些查询条件需要集成在多表这一方,,之前在博文上找了很久,没有啥具体答案,参考了mybatis的官方文档,整理了如下示例,直接上代码

主sql语句

<select id="findAllByCondition" resultMap="findAllTrafficEventMap">
        select
            event.*,
            maxHistory.id as trafficEventHistoryId,
            case when ('${judgeStartTime}' != null) then '${judgeStartTime}' else '' end as judgeStartTime,
            case when ('${judgeEndTime}' != null) then '${judgeEndTime}' else '' end as judgeEndTime
        from
            traffic_event event
        inner join
            (select max(id) as id, traffic_event_id from traffic_event_history group by traffic_event_id) maxHistory
        on event.traffic_event_id  = maxHistory.traffic_event_id
        <where>
            <if test="trafficEventType != null">
                event.traffic_event_type = #{trafficEventType}
            </if>
            <if test="isRelieved != null">
                and event.is_relieved = #{isRelieved}
            </if>
            <if test="checkStatus != null">
                and event.check_status = #{checkStatus}
            </if>
        </where>
    </select>

多方sql语句

<select id="selectJudgeTimes" resultMap="JudgeTimesMap" parameterType="java.util.Map">
        SELECT
            judgeTime.*
        FROM
            traffic_event_judge_time judgeTime
        <where>
            judgeTime.traffic_event_history_id = #{trafficEventHistoryId}
           <if test="judgeStartTime != null and judgeStartTime != ''">
                and judgeTime.judge_time :: timestamp >= #{judgeStartTime} :: timestamp
            </if>
            <if test="judgeEndTime != null and judgeEndTime != ''">
                and judgeTime.judge_time :: timestamp &lt;= #{judgeEndTime} :: timestamp
            </if>
        </where>
    </select>

两者的映射关系

 <resultMap id="findAllTrafficEventMap" type="com.bxt.event.po.TrafficEventPO">
        <result column="traffic_event_id"               property="trafficEventId"/>
        <result column="drc_event_report_time"          property="drcEventReportTime"/>
        <collection property="judgeTimes"               ofType="com.bxt.event.po.JudgeTimePO"
                    select="selectJudgeTimes" column="{trafficEventHistoryId=trafficEventHistoryId, judgeStartTime=judgeStartTime, judgeEndTime=judgeEndTime}">
        </collection>
    </resultMap>

其中trafficEventHistoryId,judgeStartTime,judgeEndTime三者属于多方的查询条件

参数含义讲解:

<!--property:对应实体类中的参数名称-->
<!--column:需要往子表传递的字段-->
<!--javaType:该参数类型-->
<!--ofType:该参数泛型-->
<!--select:子查询id名称-->
<collection property="" column="" ofType="" javaType="" select="" />
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在使用 MyBatisPageHelper 和达梦数据库时,确保以下几点可以帮助您避免查询结果出现问题: 1. 配置 MyBatis:确保正确配置 MyBatis 的连接信息和数据库驱动,包括数据库的 URL、用户名和密码等。 2. 引入 PageHelper 插件:在 MyBatis 的配置文件中,配置 PageHelper 插件以支持分页查询。例如,在 `<plugins>` 标签中添加 `<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>`。 3. 配置分页参数:在进行分页查询时,确保正确设置分页参数。可以通过在查询语句中直接传递 Page 对象,或者使用 PageHelper 的静态方法设置分页参数。 4. 注意数据库方言:达梦数据库可能有自己的方言特性,需要在 MyBatis 的配置文件中正确指定方言类型。例如,在 `<properties>` 标签中添加 `<property name="dialect" value="dm"></property>`。 5. 分页语句写法:MyBatisPageHelper 对于不同数据库的分页语句写法有所不同。针对达梦数据库,可以使用类似 `SELECT * FROM (SELECT ROW_NUMBER() OVER() AS rn, t.* FROM table t) t WHERE t.rn BETWEEN ? AND ?` 的语句进行分页查询。 6. 注意事务处理:如果涉及到事务操作,确保在需要的地方正确开启事务,并在适当的位置提交或回滚事务。 7. 版本兼容性:确保 MyBatisPageHelper 和达梦数据库的版本兼容性。查阅官方文档,了解各个组件之间的兼容性要求。 除了上述注意事项,还要根据具体的业务场景和需求,适当优化查询语句、索引等,以提高查询效率和避免潜在的问题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值