PaginationInnerInterceptor分页插件如何指定数据源名称
时间: 2024-06-12 10:05:49 浏览: 12
PaginationInnerInterceptor分页插件本身并不支持指定数据源名称,因为它是基于MyBatis的拦截器实现的,只能拦截MyBatis的执行过程,不能直接操作数据源。但是,我们可以通过配置多个SqlSessionFactory来实现在不同的数据源中使用PaginationInnerInterceptor分页插件。
具体的做法是,在配置多个SqlSessionFactory时,为每个SqlSessionFactory指定一个不同的数据源,然后在每个数据源的配置文件中分别配置PaginationInnerInterceptor分页插件。这样就可以在不同的数据源中使用PaginationInnerInterceptor分页插件了。
示例代码如下:
```
@Configuration
public class DataSourceConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.first")
public DataSource firstDataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.second")
public DataSource secondDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "firstSqlSessionFactory")
public SqlSessionFactory firstSqlSessionFactory(@Qualifier("firstDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/first/*.xml"));
bean.setPlugins(new PaginationInnerInterceptor());
return bean.getObject();
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/second/*.xml"));
bean.setPlugins(new PaginationInnerInterceptor());
return bean.getObject();
}
@Bean(name = "firstTransactionManager")
public DataSourceTransactionManager firstTransactionManager(@Qualifier("firstDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager secondTransactionManager(@Qualifier("secondDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
```
在这个示例中,我们配置了两个数据源,分别为firstDataSource和secondDataSource,然后分别配置了对应的SqlSessionFactory和TransactionManager。在每个SqlSessionFactory中,我们都配置了PaginationInnerInterceptor分页插件,这样就可以在不同的数据源中使用分页插件了。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)