MybatisPlus分页插件失效问题

今天重写控台,之前用的是github的pageHelper分页。准备换成mybatisPlus的分页插件。替换后发现分页并没有生效。
原来的配置为:

@Configuration
public class MybatisInterceptorConfig {

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }
}

sql语句为:

    @Select({"<script>" ,
            "select u.id,u.user_id,u.user_name,u.phone,u.case_state,u.state_up_time,(CASE WHEN log.count>0 then '已跟进' else '未跟进' end) follow_state,log.operate_time from ad_daichao_marketing_user u FORCE INDEX(idx_state)" +
                    "left join (SELECT  g.marketing_task_id id,COUNT(1) count,MAX(g.operate_time) operate_time from ad_daichao_marketing_operate_log g group by g.marketing_task_id ) log ",
                    "on u.id=log.id where 1=1 ",
            "<if test='state!=null'>",
            "and u.user_id in (SELECT DISTINCT user_id from ad_daichao_marketing_user_tag WHERE tag in",
            "<foreach item='item' index='index' collection='state' open='(' separator=',' close='))'>"
                    + "#{item}"
                    + "</foreach>",
            "</if>",
            "<if test='operatorId!=null'>",
            "and u.operator_id = #{operatorId} ",
            "</if>",
            "<if test='userStateUpStime!=null'>",
            "and u.state_up_time &gt;= #{userStateUpStime} ",
            "</if>",
            "<if test='userStateUpEtime!=null'>",
            "and u.state_up_time &lt;= #{.userStateUpEtime} ",
            "</if>",
            "<if test='followStateUpStime!=null'>",
            "and log.operate_time &gt;= #{followStateUpStime} ",
            "</if>",
            "<if test='followStateUpEtime!=null'>",
            "and log.operate_time &lt;= #{followStateUpEtime} ",
            "</if>",
            "<if test='appName !=null'>",
            "and u.app_name in ",
            "<foreach item='item' index='index' collection='appName' open ='(' separator=',' close=') '>"
                    +"#{item}"
                    +"</foreach>",
            "</if>",
            "<if test='userName!=null'>",
            "and u.user_name=#{userName} ",
            "</if>",
            "<if test='phone!=null'>",
            "and u.phone like concat('%',#{phone}) ",
            "</if>",
            "<if test='caseState!= null'>",
            "and u.case_state in ",
            "<foreach item='item' index='index' collection='caseState' open='(' separator=',' close=')'>"
                    + "#{item}"
                    + "</foreach>",
            "</if>",
            "<if test='followState!=null and followState==1'>",
            "HAVING follow_state='已跟进' ",
            "</if>",
            "<if test='followState!=null and followState==2'>",
            "HAVING follow_state='未跟进' ",
            "</if>",

            "order by u.state_up_time desc",
            "</script>"})
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "state", column = "user_id", javaType = List.class, many = @Many(select = "com.boutiquesf.service.admin.mapper.loan.AdDaichaoMarketingUserTagMapper.getTagByUserId"))
    })
    Page<DaichaoMarketingUserResp.DaichaoMarketingUser> getAll(Page<?> page, DaichaoMarketingUserReq daichaoMarketingUserReq);

查询报错找不到参数。mybatis映射出错。想了一下给对象参数决定加上@param注解,用 对象.属性 来映射 更改如下:

    @Select({"<script>" ,
            "select u.id,u.user_id,u.user_name,u.phone,u.case_state,u.state_up_time,(CASE WHEN log.count>0 then '已跟进' else '未跟进' end) follow_state,log.operate_time from ad_daichao_marketing_user u FORCE INDEX(idx_state)" +
                    "left join (SELECT  g.marketing_task_id id,COUNT(1) count,MAX(g.operate_time) operate_time from ad_daichao_marketing_operate_log g group by g.marketing_task_id ) log ",
                    "on u.id=log.id where 1=1 ",
            "<if test='req.state!=null'>",
            "and u.user_id in (SELECT DISTINCT user_id from ad_daichao_marketing_user_tag WHERE tag in",
            "<foreach item='item' index='index' collection='req.state' open='(' separator=',' close='))'>"
                    + "#{item}"
                    + "</foreach>",
            "</if>",
            "<if test='req.operatorId!=null'>",
            "and u.operator_id = #{req.operatorId} ",
            "</if>",
            "<if test='req.userStateUpStime!=null'>",
            "and u.state_up_time &gt;= #{req.userStateUpStime} ",
            "</if>",
            "<if test='req.userStateUpEtime!=null'>",
            "and u.state_up_time &lt;= #{req.userStateUpEtime} ",
            "</if>",
            "<if test='req.followStateUpStime!=null'>",
            "and log.operate_time &gt;= #{req.followStateUpStime} ",
            "</if>",
            "<if test='req.followStateUpEtime!=null'>",
            "and log.operate_time &lt;= #{req.followStateUpEtime} ",
            "</if>",
            "<if test='req.appName !=null'>",
            "and u.app_name in ",
            "<foreach item='item' index='index' collection='req.appName' open ='(' separator=',' close=') '>"
                    +"#{item}"
                    +"</foreach>",
            "</if>",
            "<if test='req.userName!=null'>",
            "and u.user_name=#{req.userName} ",
            "</if>",
            "<if test='req.phone!=null'>",
            "and u.phone like concat('%',#{req.phone}) ",
            "</if>",
            "<if test='req.caseState!= null'>",
            "and u.case_state in ",
            "<foreach item='item' index='index' collection='req.caseState' open='(' separator=',' close=')'>"
                    + "#{item}"
                    + "</foreach>",
            "</if>",
            "<if test='req.followState!=null and req.followState==1'>",
            "HAVING follow_state='已跟进' ",
            "</if>",
            "<if test='req.followState!=null and req.followState==2'>",
            "HAVING follow_state='未跟进' ",
            "</if>",

            "order by u.state_up_time desc",
            "</script>"})
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "state", column = "user_id", javaType = List.class, many = @Many(select = "com.boutiquesf.service.admin.mapper.loan.AdDaichaoMarketingUserTagMapper.getTagByUserId"))
    })
    Page<DaichaoMarketingUserResp.DaichaoMarketingUser> getAll(Page<?> page, @Param("req") DaichaoMarketingUserReq daichaoMarketingUserReq);

然后就不报映射错误了,查询到了数据但是分页一直没有生效。
各种尝试后,判断应该是多数据源的原因。直接声明一个分页插件不行。于是修改多数据源配置,代码如下

    @Bean("adminSqlSessionFactory")
    @Primary
    public SqlSessionFactory adminSqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
        factoryBean.setDataSource(adminDateSource);
        factoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        factoryBean.setPlugins(interceptor);
        return factoryBean.getObject();
    }

生成多数据源的SqlSessionFactory时将分页插件注入进入就好了。
结果完美运行。

总结:使用myabtisPlus分页插件进行分页时,如果参数是实体对象,必须使用@param注解,使用对象.属性进行映射。如果是多数据源下分页插件失效的,需要在生成对应的SqlSessionFactory时使用setPlugins方法注入分页插件。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值