多数据源使用pagehelper
在不同数据源中,使用不同的dialect配置 ,如果不配置的话,使用会报错Error querying database. Cause: java.lang.NullPointerException: 。因为使用了mabatis-plus,在mapper上添加@DS注解来区分数据源,没有数据源的配置文件,如果使用配置文件,也可以使用下面的配置来完成数据源的配置,类似下面的文件,
@Configuration
@MapperScan(basePackages = DataSource1Config.BASE_PACKAGE, sqlSessionFactoryRef = "datasource1SqlSessionFactory")
public class DataSource1Config {
private static final Logger log = LogManager.getLogger(DataSource1Config.class);
static final String BASE_PACKAGE = "com.transcend.demo.dao.datasource1";
private static final String MAPPER_LOCATION = "classpath:mapper/datasource1/*.xml";
@Primary
@Bean(name = "datasource1")
@ConfigurationProperties(prefix = "spring.datasource.mysql")
public DataSource datasource1() {
log.info("datasource1 开始初始化。");
return new DruidDataSource();
}
@Primary
@Bean(name = "datasource1TransactionManager")
public DataSourceTransactionManager datasource1Transaction(@Qualifier("datasource1") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Primary
@Bean(name = "datasource1SqlSessionFactory")
public SqlSessionFactory datasource1SqlSessionFactory(@Qualifier("datasource1") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(DataSource1Config.MAPPER_LOCATION));
Interceptor interceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("helperDialect", "mysql");
properties.setProperty("offsetAsPageNum", "true");
properties.setProperty("rowBoundsWithCount", "true");
properties.setProperty("reasonable", "false");
interceptor.setProperties(properties);
factoryBean.setPlugins(new Interceptor[] {interceptor});
return factoryBean.getObject();
}
@Primary
@Bean(name = "datasource1SqlSessionTemplate")
public SqlSessionTemplate datasource1SqlSessionTemplate(@Qualifier("datasource1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
直接配置PageHelper 的bean来完成不同数据源的PageHelper 配置
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","false");
properties.setProperty("rowBoundsWithCount","false");
properties.setProperty("pageSizeZero","true");
properties.setProperty("reasonable","false");
properties.setProperty("supportMethodsArguments","false");
properties.setProperty("returnPageInfo","none");
properties.setProperty("autoRuntimeDialect","true");
pageHelper.setProperties(properties);
return pageHelper;
}