zoukankan      html  css  js  c++  java
  • springboot多数据源配置

    application.yml

     1 spring:
     2   datasource:
     3     db1:
     4       type: com.alibaba.druid.pool.DruidDataSource
     5       driver-class-name: com.mysql.cj.jdbc.Driver
     6       jdbc-url: jdbc:mysql://192.168.188.10:3306/liuxn?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
     7       username: root
     8       password: root
     9     db2:
    10       type: com.alibaba.druid.pool.DruidDataSource
    11       driver-class-name: com.mysql.cj.jdbc.Driver
    12       jdbc-url: jdbc:mysql://192.168.188.10:3306/liuxn2?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai
    13       username: root
    14       password: root

    java代码配置多个数据源配置

    1、DataSource1Config

     1 @Configuration
     2 @MapperScan(basePackages ="com.xs.beebot.shandong.report.mapper.one",sqlSessionTemplateRef ="oneSqlSessionTemplate" )
     3 public class DataSource1Config {
     4 
     5     @Bean(name = "oneDataSource")
     6     @ConfigurationProperties(prefix = "spring.datasource.db1")
     7     @Primary
     8     public DataSource testDataSource() {
     9         return DataSourceBuilder.create().build();
    10     }
    11 
    12     @Bean(name = "oneSqlSessionFactory")
    13     @Primary
    14     public SqlSessionFactory testSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception {
    15         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    16         bean.setDataSource(dataSource);
    17         bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/one/*.xml"));
    18         return bean.getObject();
    19     }
    20 
    21     @Bean(name = "oneTransactionManager")
    22     @Primary
    23     public DataSourceTransactionManager testTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) {
    24         return new DataSourceTransactionManager(dataSource);
    25     }
    26 
    27     @Bean(name = "oneSqlSessionTemplate")
    28     @Primary
    29     public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    30         return new SqlSessionTemplate(sqlSessionFactory);
    31     }
    32 }

    2、DataSource2Config

     1 @Configuration
     2 @MapperScan(basePackages ="com.xs.beebot.shandong.report.mapper.two",sqlSessionTemplateRef ="twoSqlSessionTemplate" )
     3 public class DataSource2Config {
     4 
     5     @Bean(name = "twoDataSource")
     6     @ConfigurationProperties(prefix = "spring.datasource.db2")
     7     @Primary
     8     public DataSource testDataSource() {
     9         return DataSourceBuilder.create().build();
    10     }
    11 
    12     @Bean(name = "twoSqlSessionFactory")
    13     @Primary
    14     public SqlSessionFactory testSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource) throws Exception {
    15         SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
    16         bean.setDataSource(dataSource);
    17         bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/two/*.xml"));
    18         return bean.getObject();
    19     }
    20 
    21     @Bean(name = "twoTransactionManager")
    22     @Primary
    23     public DataSourceTransactionManager testTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) {
    24         return new DataSourceTransactionManager(dataSource);
    25     }
    26 
    27     @Bean(name = "twoSqlSessionTemplate")
    28     @Primary
    29     public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
    30         return new SqlSessionTemplate(sqlSessionFactory);
    31     }
    32 }

    代码使用:

     1 @Service
     2 public class UserService {
     3 
     4     @Resource
     5     private UserMapper userMapper;
     6 
     7     @Resource
     8     private LoginMapper loginMapper;
     9 
    10     public List<User> getAll(){
    11        return userMapper.selectByExample(new UserExample());
    12     }
    13 
    14 
    15     public List<Login> getAllLogin(){
    16         return loginMapper.selectByExample(new LoginExample());
    17     }
    18 }
  • 相关阅读:
    sqoop导出数据
    sqoop导入数据
    Hive学习(二)
    各个版本的集群安装包地址
    Hive学习(一)
    数据仓库
    HBase学习(二)
    HBase学习(一)
    MySQL中阻塞
    MySQL中锁问题
  • 原文地址:https://www.cnblogs.com/lxn0216/p/14384081.html
Copyright © 2011-2022 走看看