引入依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
注入分页插件类
/**
* 分页插件
*/
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
return interceptor;
}
Mapper文件继承BaseMapper
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
单数据源使用默认的配置的话,到这里就结束啦~不过多数据源的话就得多加一步,因为多数据源使用的不是默认的配置,得手动把分页插件加入到SqlSessionFactory中。
多数据源最后一步
@Bean("curtainSqlSessionFactory")
@Primary
public SqlSessionFactory curtainSqlSessionFactory(@Qualifier("curtainDataSource") DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/curtain/*.xml"));
Interceptor[] plugins = new Interceptor[1];
plugins[0] = mybatisPlusInterceptor();
bean.setPlugins(plugins);
return bean.getObject();
}