springboot集成mybatis 配置多数据源

springboot集成mybatis 配置多数据源

创建一个springboot工程就不赘述了

直接上代码:

  1. 每一个数据源对应建立一个对应的包,如图:
    每一个数据源对应建立包
  2. 数据源配置如下:
spring.datasource.basic.jdbc-url=jdbc:mysql://127.0.0.1:3306/XXX?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.basic.username=root
spring.datasource.basic.password=root
#DB Configuration:
spring.datasource.mail.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.mail.jdbc-url=jdbc:mysql://127.0.0.1:3306/XXX_mail_reply?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.mail.username=root
spring.datasource.mail.password=root
#DB Configuration:
spring.datasource.order.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.order.jdbc-url=jdbc:mysql://127.0.0.1:3306/XXX_XXX_order?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
spring.datasource.order.username=root
spring.datasource.order.password=root
  1. 给每一个数据源配置包扫描,注意主数据源的要添加**@Primary**注解
// 主配置类,创建对应数据源
@Configuration
public class DataSourceConfig {
    @Bean("basic")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource.basic")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("mail")
    @ConfigurationProperties(prefix = "spring.datasource.mail")
    public DataSource mailDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("order")
    @ConfigurationProperties(prefix = "spring.datasource.order")
    public DataSource orderDataSource() {
        return DataSourceBuilder.create().build();
    }
}
/**
basic主数据源配置包扫描
**/
@Configuration
@MapperScan(basePackages = {"com.XXX.base.mapper"}, sqlSessionFactoryRef = "basicSqlSessionFactory")
public class BasicMybatisDataSourceConfig {

    // 精确到master目录和其他数据源隔离
    static final String MAPPER_LOCATION = "classpath:com/XXX/base/mapper/*.xml";

    @Bean(name = "basicSqlSessionFactory")
    @Primary
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("basic") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(BasicMybatisDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}
/**
mail数据源配置包扫描
**/
@Configuration
@MapperScan(basePackages = {"com.XXX.mail.mapper"}, sqlSessionFactoryRef = "mailSqlSessionFactory")
public class MailMybatisDataSourceConfig {

    // 精确到master目录和其他数据源隔离
    static final String MAPPER_LOCATION = "classpath:com/XXX/mail/mapper/*.xml";

    @Bean(name = "mailSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("mail") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(MailMybatisDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}
/**
order数据源配置包扫描
**/
@Configuration
@MapperScan(basePackages = {"com.XXX.order.mapper"}, sqlSessionFactoryRef = "orderSqlSessionFactory")
public class OrderMybatisDataSourceConfig {

    // 精确到master目录和其他数据源隔离
    static final String MAPPER_LOCATION = "classpath:com/XXX/order/mapper/*.xml";

    @Bean(name = "orderSqlSessionFactory")
    public SqlSessionFactory masterSqlSessionFactory(@Qualifier("order") DataSource masterDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(masterDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources(OrderMybatisDataSourceConfig.MAPPER_LOCATION));
        return sessionFactory.getObject();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值