程序员的公众号:源1024,获取更多资料,无加密无套路!
最近整理了一波电子书籍资料,包含《Effective Java中文版 第2版》《深入JAVA虚拟机》,《重构改善既有代码设计》,《MySQL高性能-第3版》,《Java并发编程实战》等等
获取方式: 关注公众号并回复 电子书 领取,更多内容持续奉上
单数据源的配置:
mybatis-plus:mapper-locations: classpath:mapper/*.xmlconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
但是多数据源就不适用了,那么多数据源应该怎么配置呢?
先看看多数据源配置实现:
application.yml:
spring:#数据源配置(可配置多个数据库)datasource:#主数据库master:type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/hello1?useUnicode=true&characterEncoding=UTF-8&useSSL=falseusername: rootpassword: 1234slave:type: com.alibaba.druid.pool.DruidDataSourcedriverClassName: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/hello2?useUnicode=true&characterEncoding=UTF-8&useSSL=falseusername: rootpassword: 1234
配置数据源:
MasterDataSourceConfig.java
@Configuration
@MapperScan(basePackages = "com.master.*.Mapper", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterDataSourceConfig {static String MAPPER_LOCATION = "classpath*:masterMapper/*Mapper.xml";/*** 配置数据源* @return*/@Bean(name = "masterDataSource")@ConfigurationProperties(prefix = "spring.datasource.master")public DataSource masterDataSource() {return new DruidDataSource();}/*** 事务管理器*/@Bean(name = "masterTransactionManager")public DataSourceTransactionManager masterTransactionManager() {return new DataSourceTransactionManager(masterDataSource());}/*** 会话工厂*/@Bean(name = "masterSqlSessionFactory")public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(masterDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}
}
SlaveDataSourceConfig.java
@Configuration
@MapperScan(basePackages = "com.slave.*.Mapper", sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveDataSourceConfig {static String MAPPER_LOCATION = "classpath*:slaveMapper/*Mapper.xml";/*** 配置数据源* @return*/@Bean(name = "slaveDataSource")@ConfigurationProperties(prefix = "spring.datasource.slave")public DataSource slaveDataSource() {return new DruidDataSource();}/*** 事务管理器*/@Bean(name = "slaveTransactionManager")public DataSourceTransactionManager slaveTransactionManager() {return new DataSourceTransactionManager(slaveDataSource());}/*** 会话工厂*/@Bean(name = "slaveSqlSessionFactory")public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource slaveDataSource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(slaveDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(SlaveDataSourceConfig.MAPPER_LOCATION));return sessionFactory.getObject();}
}
如果想打印Master数据源的sql日志,修改SqlSessionFactory,添加sql打印配置,slave同理。
/*** 会话工厂*/@Bean(name = "slaveSqlSessionFactory")public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource slaveDataSource) throws Exception {final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();sessionFactory.setDataSource(slaveDataSource);sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(SlaveDataSourceConfig.MAPPER_LOCATION));MybatisConfiguration configuration = new MybatisConfiguration();configuration.setMapUnderscoreToCamelCase(true);// 配置打印sql语句configuration.setLogImpl(StdOutImpl.class);sessionFactory.setConfiguration(configuration);return sessionFactory.getObject();}