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
上一篇 使用MyBatis查询数据,按特定顺序排序
下一篇 利用Java反射机制获取实体对象所有属性及其对应值
目录
文章列表
1 Filebeat+ElasticSearch+Grafana实现Nginx日志监控
Filebeat+ElasticSearch+Grafana实现Nginx日志监控
2
MongoDB Limit与Skip方法
MongoDB Limit与Skip方法
3
Bigcommerce插件开发指南
Bigcommerce插件开发指南
4
Spring Cloud Gray 接入开发指南
Spring Cloud Gray 接入开发指南
5
Gradle设置apk文件输出命名
Gradle设置apk文件输出命名
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。