springboot 2.1.3 + mybatis + druid配置多數據源
阿新 • • 發佈:2019-02-24
tab sys cal 1.9 path exc http bject cat
在一些大型的項目中,通常會選擇多數據庫來滿足一些業務需求,此處講解使用springboot、mybatis和druid來配置多數據源
1、依賴配置
pom文件引入相關依賴
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
application文件加入多數據源配置
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driverClassName: com.mysql.cj.jdbc.Driver druid: # 主庫數據源 master: url: jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false username: root password: root # 從庫數據源 slave: url: jdbc:mysql://ip:3306/springboot?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false username: root password: root # 初始連接數 initial-size: 10 # 最大連接池數量 max-active: 100 # 最小連接池數量 min-idle: 10 # 配置獲取連接等待超時的時間 max-wait: 60000 # 打開PSCache,並且指定每個連接上PSCache的大小 pool-prepared-statements: true max-pool-prepared-statement-per-connection-size: 20 # 配置間隔多久才進行一次檢測,檢測需要關閉的空閑連接,單位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一個連接在池中最小生存的時間,單位是毫秒 min-evictable-idle-time-millis: 300000 validation-query: SELECT 1 FROM DUAL test-while-idle: true test-on-borrow: false test-on-return: false stat-view-servlet: enabled: true url-pattern: /monitor/druid/* filter: stat: log-slow-sql: true slow-sql-millis: 1000 merge-sql: false wall: config: multi-statement-allow: true
2、添加主數據源和其他數據源配置
主數據源
@Configuration //master mapper目錄 @MapperScan(basePackages = {"com.example.springbootdruidmultsource.mapper.master"}, sqlSessionFactoryRef = "masterSqlSessionFactory") public class MasterDataSourcesConfig { private static final String MAPPER_LOCAL = "classpath:mybatis/master/*.xml"; @ConfigurationProperties("spring.datasource.druid.master") @Primary @Bean(name = "masterDataSource") public DruidDataSource druidDataSource() { return new DruidDataSource(); } @Bean(name = "masterTransactionManager") @Primary public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(druidDataSource()); } @Bean(name = "masterSqlSessionFactory") @Primary public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCAL)); return sessionFactoryBean.getObject(); } }
其中註解@Primary表示是主數據源
從數據源
@Configuration @MapperScan(basePackages = {"com.example.springbootdruidmultsource.mapper.slave"}, sqlSessionFactoryRef = "slaveSqlSessionFactory") public class SlaveDataSourcesConfig { private static final String MAPPER_LOCAL = "classpath:mybatis/slave/*.xml"; @Bean(name = "slaveDataSource") @ConfigurationProperties("spring.datasource.druid.slave") public DruidDataSource druidDataSource() { return new DruidDataSource(); }
//其他數據源的事務管理器 @Bean(name = "slaveTransactionManager") public DataSourceTransactionManager slaveTransactionManager() { return new DataSourceTransactionManager(druidDataSource()); } @Bean(name = "slaveSqlSessionFactory") public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DruidDataSource dataSource) throws Exception { final SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSource); sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCAL)); return sessionFactoryBean.getObject(); } }
其他的就是mybatis相關的一些內容,這裏不贅述
3、事務管理
對主數據原來說,直接在註解
@Override @Transactional public void update(SysUser sysUser) { sysUserMapper.update(sysUser); int i = 10 / 0; }
對其他數據源來說,需要加上配置的事務管理器
@Override @Transactional(value = "slaveTransactionManager") public void update(User user) { userMapper.update(user); int i = 10 / 0; }
源碼參照:Github
springboot 2.1.3 + mybatis + druid配置多數據源