MyBatis根据List批量查询List结果排序问题

MyBatis配置

public interface RouteServiceMapper {
    List<RouteService> selectRouteServiceList(@Param("routeServiceList") List<Map<String, String>> routeServiceList);
}

查询出来的RouteServiceList顺序与传入的List信息顺序不一致:

<select id="selectRouteServiceList" parameterType="java.util.List" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List"/>
    from route_service rs
    where
    rs.route_code in
    <foreach item="routeService" index="index" collection="routeServiceList"
             open="(" separator="," close=")">
        #{routeService.route_code}
    </foreach>
    and rs.service_code in
    <foreach item="routeService" index="index" collection="routeServiceList"
            open="(" separator="," close=")">
        #{routeService.service_code}
    </foreach>
    and rs.status = 'ENABLE'
</select>

解决方式:

<select id="selectRouteServiceList" parameterType="java.util.List" resultMap="BaseResultMap">
    select * from
    <foreach item="routeService" index="index" collection="routeServiceList"
             open="(" separator="union all" close=")">
        select <include refid="Base_Column_List"/>
        from route_service rs where
        rs.route_code=#{routeService.route_code,jdbcType=VARCHAR} and
        rs.service_code=#{routeService.service_code,jdbcType=VARCHAR} and
        rs.status = 'ENABLE'
    </foreach> as rs
</select>

SQL测试

  • 待查询数组顺序
[{"service_code":"ALIPAY_QR","route_code":"alipay"},
 {"service_code":"WECHATPAY_QR","route_code":"wechatpay"}]
  • 顺序不一致查询转义
select id, route_code, service_code, service_app_name, pay_outer, status, creation_time, modification_time 
from tp_channel_service cs 
where cs.route_code in ( 'alipay','wechatpay' ) 
  and cs.service_code in ( 'ALIPAY_QR','WECHATPAY_QR' ) 
  and cs.status = 'ENABLE'
  • 顺序一致查询转义
select id, route_code, service_code, service_app_name, pay_outer, status, creation_time, modification_time from
    (
    select id, route_code, service_code, service_app_name, pay_outer, status, creation_time, modification_time from tp_channel_service cs where
        cs.route_code='alipay' and cs.service_code='ALIPAY_QR' and cs.status = 'ENABLE'
    UNION ALL
    select id, route_code, service_code, service_app_name, pay_outer, status, creation_time, modification_time from tp_channel_service cs where
        cs.route_code='wechatpay' and cs.service_code='WECHATPAY_QR' and cs.status = 'ENABLE'
    )
as cs

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/02/26/mybatis-batch-query-list-results-sorting-problem-based-on-list/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
MyBatis根据List批量查询List结果排序问题
MyBatis配置 public interface RouteServiceMapper { List<RouteService> selectRouteServiceList(@Param("routeServiceList") List<M……
<<上一篇
下一篇>>
文章目录
关闭
目 录