springboot 多数据源添加-Mybatis版

springboot 多数据源添加-Mybatis版

文章目录

  • springboot 多数据源添加-Mybatis版
    • Spring配置文件配置数据源连接信息
    • Spring代码配置

很久之前同学问起过springboot 如何添加数据源,一直没机会搞,这次抽空搞了下。本地准备好两个数据库,wst、my_test,具体教程参考 SpringBoot官网

Spring配置文件配置数据源连接信息

这里我整合了p6spy,可以忽略,和正常的数据库驱动一样。这里需要注意一点的是,spring.datasource.p0.url得改为spring.datasource.p0.jdbc-url,具体原因可以看下上面官网的回答。

spring.datasource.driver-class-name=com.p6spy.engine.spy.P6SpyDriver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
db.conn.info=useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&autoReconnect=true&serverTimezone=UTC

spring.datasource.p0.jdbc-url=jdbc:p6spy:mysql://127.0.0.1:3306/wst?${db.conn.info}
spring.datasource.p0.username=root
spring.datasource.p0.password=123456
spring.datasource.p0.driver-class-name=com.p6spy.engine.spy.P6SpyDriver

spring.datasource.p1.jdbc-url=jdbc:p6spy:mysql://127.0.0.1:3306/my_test?${db.conn.info}
spring.datasource.p1.username=root
spring.datasource.p1.password=123456
spring.datasource.p1.driver-class-name=com.p6spy.engine.spy.P6SpyDriver

Spring代码配置

因为SpringBoot会自动读取application.properties中的数据源属性,所以需要手动在写下配置

配置数据源

/**
 * @program: springCloudZoo
 * @author: chuanyun.yang
 * @create: 2020-10-09 15:17
 **/
@Configuration
public class DataSourceConfig {

    @Bean(name = "firstDb")
    @ConfigurationProperties(prefix = "spring.datasource.p0")
    public DataSource firstDataSource(){
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "secondDb")
    @ConfigurationProperties(prefix = "spring.datasource.p1")
    public DataSource secondDataSource(){
        return DataSourceBuilder.create().build();
    }
}

整合mybatis,扫不同的mapper包,不同的包下放置对应的mapper,访问对应的数据库

/**
 * @program: springCloudZoo
 * @author: chuanyun.yang
 * @create: 2020-10-09 15:17
 **/
@MapperScan(basePackages = "com.lemon.springcloudmyeureka.mapper.firstdb", sqlSessionFactoryRef = "firstDbSqlSessionFactory")
@Configuration
public class FirstDbConfig {

    @Qualifier("firstDb")
    @Autowired
    private DataSource firstDataSource;

    @Bean
    public SqlSessionFactory firstDbSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(firstDataSource);
        sqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/firstdb/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate firstSqlSessionTemplate(SqlSessionFactory firstDbSqlSessionFactory) {
        return new SqlSessionTemplate(firstDbSqlSessionFactory);
    }
}
/**
 * @program: springCloudZoo
 * @author: chuanyun.yang
 * @create: 2020-10-09 15:17
 **/
@MapperScan(basePackages = "com.lemon.springcloudmyeureka.mapper.firstdb", sqlSessionFactoryRef = "firstDbSqlSessionFactory")
@Configuration
public class FirstDbConfig {

    @Qualifier("firstDb")
    @Autowired
    private DataSource firstDataSource;

    @Bean
    public SqlSessionFactory firstDbSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(firstDataSource);
        sqlSessionFactoryBean.setMapperLocations(
                new PathMatchingResourcePatternResolver().getResources("classpath:mapper/firstdb/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }

    @Bean
    public SqlSessionTemplate firstSqlSessionTemplate(SqlSessionFactory firstDbSqlSessionFactory) {
        return new SqlSessionTemplate(firstDbSqlSessionFactory);
    }
}

你可能感兴趣的:(mybatis,spring,多数据源,mybatis,spring,boot)