今天重写控台,之前用的是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 >= #{userStateUpStime} ",
"</if>",
"<if test='userStateUpEtime!=null'>",
"and u.state_up_time <= #{.userStateUpEtime} ",
"</if>",
"<if test='followStateUpStime!=null'>",
"and log.operate_time >= #{followStateUpStime} ",
"</if>",
"<if test='followStateUpEtime!=null'>",
"and log.operate_time <= #{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 >= #{req.userStateUpStime} ",
"</if>",
"<if test='req.userStateUpEtime!=null'>",
"and u.state_up_time <= #{req.userStateUpEtime} ",
"</if>",
"<if test='req.followStateUpStime!=null'>",
"and log.operate_time >= #{req.followStateUpStime} ",
"</if>",
"<if test='req.followStateUpEtime!=null'>",
"and log.operate_time <= #{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方法注入分页插件。