多数据源:
1、数据源配置类 DB***Config.java(几个数据源写几个此类)
2、数据源对应数据库连接pom
3、启动类加注解
//关闭springBoot关于mybatis的一些自动注入(多数据源),DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class防止报错NoUniqueBeanDefinitionException
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class})
//@MapperScan(basePackages ="com.example.demo.dao")//mapper映射接口所在包路径(单数据源用到)
4、数据库地址账号 配置
注意事项:
spring boot应用,通常我们在进行数据管理时,只操作一个数据源的表,需要开启事务管理,只需要在服务启动类增加@EnableTransactionManagement注解,在需要事务控制的方法增加@Transactional注解即可。
数据源配置类:
@ConfigurationProperties("spring.datasource.wkAutoNotice")
此注解不支持驼峰命名法和下划线,修改为全部小写
多数据源,写声明式事务注解时,需要加value属性 如:@Transactional(value = "dbwkautonoticeTransactionManager")
@Primary//在配置两个数据源的时候,在其中一个数据源加上注解@Primary即可,多个transactionManager @Configuration @MapperScan(basePackages = "com.example.demo.dao.daobase",sqlSessionFactoryRef = "wkAutoNoticeSqlSessionFactory") public class DBWkguideAutoNoticeConfig { @Primary @Bean(name = "wkAutoNoticeDataSource") @ConfigurationProperties("spring.datasource.wkautonotice")//配置中jdbcUrl、driverClassName、username、password等配置前缀 public DataSource masterDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "wkAutoNoticeSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("wkAutoNoticeDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mapper/*.xml"));//mapper.xml文件路径。 return sessionFactoryBean.getObject(); } @Bean//方法名就是事务value如:@Transactional(value = "dbwkautonoticeTransactionManager") public PlatformTransactionManager dbwkautonoticeTransactionManager(@Qualifier("wkAutoNoticeDataSource") DataSource prodDataSource) { return new DataSourceTransactionManager(prodDataSource); } }
MySQL数据源配置类:
@Configuration @MapperScan(basePackages = "com.example.demo.dao.daoprintorder",sqlSessionFactoryRef = "printorderSqlSessionFactory") public class DBMySqlPrintorder { @Primary @Bean(name = "printorderDataSource") @ConfigurationProperties("spring.datasource.printorder")//配置中jdbcUrl、driverClassName、username、password等配置前缀 public DataSource masterDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "printorderSqlSessionFactory") public SqlSessionFactory sqlSessionFactory(@Qualifier("printorderDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver() .getResources("classpath:mapper/printorder/*.xml"));//mapper.xml文件路径。 return sessionFactoryBean.getObject(); } @Bean//方法名就是事务value如:@Transactional(value = "printorderTransactionManager") public PlatformTransactionManager printorderTransactionManager(@Qualifier("printorderDataSource") DataSource prodDataSource) { return new DataSourceTransactionManager(prodDataSource); } }
注意:上边不同数据库对应mapper.xml 路径不一样
数据库配置:
spring:
datasource: wkautonotice: ConnectionTestQuery: SELECT 1 jdbcUrl: jdbc:sqlserver://qn.yu****ji.com:30005;databasename=W*****st driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver username: ***** password: **** # sql-script-encoding: utf-8 printorder: driver-class-name: com.mysql.cj.jdbc.Driver jdbcUrl: jdbc:mysql://rm-bp17a******o.mysql.rds.aliyuncs.com:3306/********* username: **** password: ****
# pageHelper 分页插件配置
pagehelper:
reasonable: false
supportMethodsArguments: true
params: count=countSql
#默认false,当为true时,自动检验适合的数据库
auto-dialect: true
#pagehelper多数据源这个一定要加上,不然mysql和oracle分页两个只能用一个,另一个会报错,加上后,两中数据库分页都可以用了,自动识别方言
auto-runtime-dialect: true
启动类加注解:
@SpringBootApplication @EnableTransactionManagement//开启事务管理 @EnableCaching//开启缓存功能redis需要配置 @EnableScheduling //开启定时任务 //关闭springBoot关于mybatis的一些自动注入(多数据源),DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class防止报错NoUniqueBeanDefinitionException @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class}) //@MapperScan(basePackages ="com.example.demo.dao")//mapper映射接口所在包路径(单数据源) public class DemoApplication {