赞
踩
application.yml文件里配置数据源,可多个
- # 数据库1
- spring.datasource.test1.url: jdbc:mysql://ip1:port1/test1?autoReconnect=true&characterEncoding=UTF-8&useTimezone=true&serverTimezone=GMT%2B8
- spring.datasource.test1.username: test1
- spring.datasource.test1.password: test1
- spring.datasource.test1.driver-class-name: com.mysql.cj.jdbc.Driver
-
- # 数据库2
- spring.datasource.test2.url: jdbc:mysql://ip2:port2/test2?autoReconnect=true&characterEncoding=UTF-8&useTimezone=true&serverTimezone=GMT%2B8
- spring.datasource.test2.username: test2
- spring.datasource.test2.password: test2
- spring.datasource.test2.driver-class-name: com.mysql.cj.jdbc.Driver
-
- # 通用连接池配置
- spring.datasource.type: com.alibaba.druid.pool.DruidDataSource
- spring.datasource.initialSize: 5
- spring.datasource.minIdle: 5
- spring.datasource.maxActive: 20
- spring.datasource.maxWait: 60000
- spring.datasource.timeBetweenEvictionRunsMillis: 60000
- spring.datasource.validationQuery: SELECT 1
- spring.datasource.testWhileIdle: true
- spring.datasource.testOnBorrow: false
- spring.datasource.testOnReturn: false
- spring.datasource.poolPreparedStatements: true
- spring.datasource.maxPoolPreparedStatementPerConnectionSize: 20
- spring.datasource.filters: stat
- spring.datasource.connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
根据配置的数据库个数,创建对应个数的连接配配置类:
1.常规读取mapper文件的配置
- @Configuration
- @MapperScan(basePackages = "com.xxx.datasource.test1.mapper", sqlSessionTemplateRef = "test1SqlSessionTemplate")
- public class Test1Datasource {
-
- @Bean(name = "test1DataSource")
- @Primary
- public DataSource getDataSource(Environment env) throws Exception {
- Properties props = new Properties();
- props.put("driverClassName", env.getProperty("spring.datasource.test1.driver-class-name"));
- props.put("url", env.getProperty("spring.datasource.test1.url"));
- props.put("username", env.getProperty("spring.datasource.test1.username"));
- props.put("password", env.getProperty("spring.datasource.test1.password"));
- return DruidDataSourceFactory.createDataSource(props);
- }
-
- @Bean(name = "test1SqlSessionFactory")
- public SqlSessionFactory getSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)
- throws Exception {
- SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
- bean.setDataSource(dataSource);
- // 设置mybatis的主配置文件 记住是主配置文件啊
- ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
- // 设置mapper.xml文件的路径 {resourceMapXML};
- Resource[] resource = resolver.getResources("classpath*:mapper/*.xml");
- bean.setMapperLocations(resource);
- return bean.getObject();
- }
-
- @Bean(name = "test1TransactionManager")
- @Primary
- public DataSourceTransactionManager getTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
-
- @Bean(name = "test1SqlSessionTemplate")
- @Primary
- public SqlSessionTemplate
- getSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
- return new SqlSessionTemplate(sqlSessionFactory);
- }
- }
2.使用mybatis-plus时的配置
- @Configuration
- @MapperScan(basePackages = "com.xxx.datasource.test2.mapper", sqlSessionFactoryRef = "test2SqlSessionFactory")
- public class Test2PlusDataSource {
-
- @Bean(name = "test2PlusDataSource")
- public DataSource getDataSource(Environment env) throws Exception {
- Properties props = new Properties();
- props.put("driverClassName", env.getProperty("spring.datasource.test2.driver-class-name"));
- props.put("url", env.getProperty("spring.datasource.test2.url"));
- props.put("username", env.getProperty("spring.datasource.test2.username"));
- props.put("password", env.getProperty("spring.datasource.test2.password"));
- return DruidDataSourceFactory.createDataSource(props);
- }
-
- /**
- * mybatis-plus分页插件
- */
- @Bean(name = "test2PaginationInterceptor")
- public Interceptor paginationInterceptor() {
- PaginationInterceptor page = new PaginationInterceptor();
- page.setDialectType("mysql");
- return page;
- }
-
- /**
- * 注入mybatis的 DatabaseIdProvider 作为一个bean,方便被引用
- *
- * @return bean
- */
- @Bean
- public DatabaseIdProvider getDatabaseIdProvider() {
- return new DefaultDatabaseIdProvider();
- }
-
-
-
- /**
- * 这里全部使用mybatis-autoconfigure 已经自动加载的资源。不手动指定 配置文件和mybatis-boot的配置文件同步 采用多参数注入方式,避免通过autowired注入导致的报错
- *
- * @param dataSource 注入数据源
- * @param databaseIdProvider 注入DatabaseIdProvider
- * @param properties 注入 MybatisPlusProperties
- * @param interceptors 注入 Interceptor 主要为分页信息
- * @return MybatisSqlSessionFactoryBean
- * @throws IOException IOException
- */
- @Bean(name = "test2SqlSessionFactory")
- public MybatisSqlSessionFactoryBean test2SqlSessionFactoryBean(
- @Qualifier("test2PlusDataSource") DataSource dataSource, DatabaseIdProvider databaseIdProvider,
- MybatisPlusProperties properties, Interceptor[] interceptors) throws IOException {
- MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
- try {
- mybatisPlus.setDataSource(dataSource);
- } catch (Exception e) {
- log.error(ExceptionUtils.getStackTrace(e));
- }
- //DefaultVFS在获取jar上存在问题,使用springboot
- //SpringBoot完整包部署,手动设置SpringBootVFS
- mybatisPlus.setVfs(SpringBootVFS.class);
- // 设置分页插件
- if (interceptors != null) {
- mybatisPlus.setPlugins(interceptors);
- }
-
- MybatisConfiguration mc = new MybatisConfiguration();
- mc.setDefaultScriptingLanguage(MybatisXMLLanguageDriver.class);
- // 数据库和java都是驼峰,就不需要
- mc.setMapUnderscoreToCamelCase(true);
- mybatisPlus.setConfiguration(mc);
-
- if (databaseIdProvider != null) {
- mybatisPlus.setDatabaseIdProvider(databaseIdProvider);
- }
-
- mybatisPlus.setTypeAliasesPackage("com.xxx.entity.test2");
- if (properties != null) {
- mybatisPlus.setTypeHandlersPackage(properties.getTypeHandlersPackage());
- mybatisPlus.setMapperLocations(properties.resolveMapperLocations());
- }
- // 设置mapper.xml文件的路径
- ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
- Resource[] resource = resolver.getResources("classpath:mapper/test2/**/*.xml");
- mybatisPlus.setMapperLocations(resource);
-
- return mybatisPlus;
- }
-
- @Bean(name = "test2MpTransactionManager")
- public DataSourceTransactionManager getTransactionManager(@Qualifier("test2PlusDataSource") DataSource dataSource) {
- return new DataSourceTransactionManager(dataSource);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。