赞
踩
@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() { // return DataSourceBuilder.create().type(DruidDataSource.class).build(); 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)); //添加PageHelper插件 Interceptor interceptor = new PageInterceptor(); Properties properties = new Properties(); //数据库 properties.setProperty("helperDialect", "mysql"); //是否将参数offset作为PageNum使用 properties.setProperty("offsetAsPageNum", "true"); //是否进行count查询 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属性 不同数据源 自动配置Dialect */ @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"); //这个属性非常重要 在oracle 和pg数据库中切换 properties.setProperty("autoRuntimeDialect","true"); pageHelper.setProperties(properties); return pageHelper; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。