您现在的位置是:首页 > 个人博客日记 个人博客日记

springBoot配置多数据源连接数据库

2019-07-10 19:04:20

springBoot配置多数据源连接数据库

修改配置文件 application.yml

  1. server:
  2. port: 8001
  3. spring:
  4. #定义多数据源
  5. datasource-test1:
  6. driver-class-name: com.mysql.jdbc.Driver
  7. jdbc-url: jdbc:mysql://127.0.0.1:3306/test1?useUnicode=true&characterEncoding=utf-8&useSSL=false
  8. username: root
  9. password: root
  10. dbcp2:
  11. #验证连接的有效性
  12. test-while-idle: true
  13. #验证语句
  14. validation-query: SELECT 1
  15. #空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟
  16. time-between-eviction-runs-millis: 300000
  17. #连接池空闲连接的有效时间 ,设置30分钟
  18. soft-min-evictable-idle-time-millis: 1800000
  19. hikari:
  20. #一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired)
  21. # max-lifetime: 60000
  22. #连接池中允许的最大连接数
  23. maximum-pool-size: 15
  24. #<!-- 生效超时 -->
  25. validation-timeout: 3000
  26. #连接只读数据库时配置为true, 保证安全
  27. read-only: false
  28. #等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException
  29. connection-timeout: 60000
  30. #一个连接idle状态的最大时长(毫秒),超时则被释放(retired)
  31. idle-timeout: 60000
  32. # 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQLwait_timeout参数(show variables like '%timeout%';)
  33. max-lifetime: 120000
  34. datasource-test2:
  35. driver-class-name: com.mysql.jdbc.Driver
  36. jdbc-url: jdbc:mysql://127.0.0.1:3306/test2?useUnicode=true&characterEncoding=utf-8&useSSL=false
  37. username: root
  38. password: root
  39. dbcp2:
  40. #验证连接的有效性
  41. test-while-idle: true
  42. #验证语句
  43. validation-query: SELECT 1
  44. #空闲连接回收的时间间隔,与test-while-idle一起使用,设置5分钟
  45. time-between-eviction-runs-millis: 300000
  46. #连接池空闲连接的有效时间 ,设置30分钟
  47. soft-min-evictable-idle-time-millis: 1800000
  48. hikari:
  49. #一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired)
  50. # max-lifetime: 60000
  51. #连接池中允许的最大连接数
  52. maximum-pool-size: 15
  53. #<!-- 生效超时 -->
  54. validation-timeout: 3000
  55. #连接只读数据库时配置为true, 保证安全
  56. read-only: false
  57. #等待连接池分配连接的最大时长(毫秒),超过这个时长还没可用的连接则发生SQLException
  58. connection-timeout: 60000
  59. #一个连接idle状态的最大时长(毫秒),超时则被释放(retired)
  60. idle-timeout: 60000
  61. # 一个连接的生命时长(毫秒),超时而且没被使用则被释放(retired),缺省:30分钟,建议设置比数据库超时时长少30秒,参考MySQLwait_timeout参数(show variables like '%timeout%';)
  62. max-lifetime: 120000
  63. #mybatis配置
  64. mybatis:
  65. # mapper-locations: classpath:mybatis/mapper/company/*.xml,mybatis/mapper/tinymeng/*.xml #配置多数据源以后在config里边定义了mapper的位置,此行失效
  66. config-location: classpath:mybatis/mybatis-config.xml
创建config文件夹

1.创建 DataSourceConfig

  1. package com.majiameng.config;
  2. import org.springframework.boot.context.properties.ConfigurationProperties;
  3. import org.springframework.boot.jdbc.DataSourceBuilder;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import javax.sql.DataSource;
  7. /**
  8. * 数据源配置
  9. */
  10. @Configuration
  11. public class DataSourceConfig {
  12. @Bean(name="datasourceTest1")
  13. @ConfigurationProperties(prefix = "spring.datasource-test1")
  14. public DataSource datasourceTest1() {
  15. return DataSourceBuilder.create().build();
  16. }
  17. @Bean(name="datasourceTest2")
  18. @ConfigurationProperties(prefix = "spring.datasource-test2")
  19. public DataSource datasourceTest2() {
  20. return DataSourceBuilder.create().build();
  21. }
  22. }

2.创建 MybatisTest1Config

  1. package com.majiameng.config;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  11. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  12. import org.springframework.transaction.PlatformTransactionManager;
  13. import javax.sql.DataSource;
  14. /**
  15. * 定义test1数据源
  16. */
  17. @Configuration
  18. @MapperScan(basePackages = {"com.majiameng.mapper.test1"},sqlSessionFactoryRef = "sqlSessionFactoryTest1")
  19. public class MybatisTest1Config {
  20. @Autowired
  21. @Qualifier(value="datasourceTest1")
  22. private DataSource test1;
  23. @Bean
  24. public SqlSessionFactory sqlSessionFactoryTest1() throws Exception {
  25. SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
  26. factory.setDataSource(test1);
  27. //指定mapper位置
  28. factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
  29. return factory.getObject();
  30. }
  31. @Bean
  32. public SqlSessionTemplate sqlSessionTemplateTest1() throws Exception {
  33. SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryTest1());
  34. return template;
  35. }
  36. @Bean(name = "transactionManagerTest1")
  37. public PlatformTransactionManager transactionManagerTest1() {
  38. return new DataSourceTransactionManager(test1);
  39. }
  40. }

1.创建 MybatisTest2Config

  1. package com.majiameng.config;
  2. import org.apache.ibatis.session.SqlSessionFactory;
  3. import org.mybatis.spring.SqlSessionFactoryBean;
  4. import org.mybatis.spring.SqlSessionTemplate;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Qualifier;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
  11. import org.springframework.jdbc.datasource.DataSourceTransactionManager;
  12. import org.springframework.transaction.PlatformTransactionManager;
  13. import javax.sql.DataSource;
  14. /**
  15. * 定义test2数据源
  16. */
  17. @Configuration
  18. @MapperScan(basePackages = {"com.majiameng.mapper.test2"},sqlSessionFactoryRef = "sqlSessionFactoryTest2")
  19. public class MybatisTest2Config {
  20. @Autowired
  21. @Qualifier(value="datasourceTest2")
  22. private DataSource test2;
  23. @Bean
  24. public SqlSessionFactory sqlSessionFactoryTest2() throws Exception {
  25. SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
  26. factory.setDataSource(test2);
  27. //指定mapper位置
  28. factory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
  29. return factory.getObject();
  30. }
  31. @Bean
  32. public SqlSessionTemplate sqlSessionTemplateTest2() throws Exception {
  33. SqlSessionTemplate template = new SqlSessionTemplate(sqlSessionFactoryTest2());
  34. return template;
  35. }
  36. @Bean(name = "transactionManagerTest2")
  37. public PlatformTransactionManager transactionManagerTest2() {
  38. return new DataSourceTransactionManager(test2);
  39. }
  40. }

两个数据源的配置创建好了

然后对应创建包

  1. com.majiameng.mapper.test1;
  2. com.majiameng.mapper.test2;
  3. resources.mybatis.mapper.test1
  4. resources.mybatis.mapper.test2

在启动类上加注解,启动事务

  1. //@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册
  2. @ServletComponentScan
  3. //事务管理注解
  4. @EnableTransactionManagement


关注TinyMeng博客,更多精彩分享,敬请期待!
 

站点信息