原因一:没有引入分页插件
以下为新版mybatisplus的插件配置
@Configuration
@MapperScan("com.xx.mapper")
@EnableTransactionManagement
public class MybatisPlusConfig {
@Bean
MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
原因二:没有在数据源配置中加入分页插件
在使用多数据源(非动态)时需要在数据源配置中手动加入分页
以下为配置方法(设置 MyBatis-Plus 分页插件注解以下部分)
@Configuration
@MapperScan(basePackages = {"com.xx.mapper.xx"},sqlSessionTemplateRef = "sqlTemplate1")
public class DataSourceConfig1 {
@Autowired
private MybatisPlusInterceptor mybatisPlusInterceptor;
@Primary
@Bean(name = "datasource1")
@ConfigurationProperties(prefix = "spring.datasource.xx")
public DruidDataSource druidDataSource1()
{
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "sqlFactory1")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("datasource1") DruidDataSource dataSource)
throws Exception
{
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
//设置 MyBatis-Plus 分页插件
Interceptor[] plugins = {mybatisPlusInterceptor};
factoryBean.setPlugins(plugins);
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factoryBean.setMapperLocations(resolver.getResources("classpath:mapper/invx/*.xml"));
return factoryBean.getObject();
}
@Bean(name = "sqlTemplate1")
@Primary
public SqlSessionTemplate sqlSessionTemplate(@Qualifier("sqlFactory1") SqlSessionFactory sqlSessionFactory)
{
return new SqlSessionTemplate(sqlSessionFactory);
}
}