spring boot druid mybatis 多数据源(初学)

spring boot druid mybatis 多数据源(初学)

配置参数

#数据源one
spring.datasource.druid.jwt.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.jwt.url=jdbc:mysql://127.0.0.1:3306/jwttest?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.datasource.druid.jwt.username=root
spring.datasource.druid.jwt.password=qiquan
spring.datasource.druid.jwt.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.jwt.initial-size=5
spring.datasource.druid.jwt.min-idle=15
spring.datasource.druid.jwt.max-active=60
spring.datasource.druid.jwt.validation-query=SELECT 1
spring.datasource.druid.jwt.test-on-borrow=true
spring.datasource.druid.jwt.test-while-idle=true
spring.datasource.druid.jwt.time-between-eviction-runs-millis=60000
spring.datasource.druid.jwt.filters=stat,wall,log4j
#数据源 two
spring.datasource.druid.goods.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.druid.goods.url=jdbc:mysql://127.0.0.1:3306/goods?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
spring.datasource.druid.goods.username=root
spring.datasource.druid.goods.password=qiquan
spring.datasource.druid.goods.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.druid.goods.initial-size=5
spring.datasource.druid.goods.min-idle=15
spring.datasource.druid.goods.max-active=60
spring.datasource.druid.goods.validation-query=SELECT 1
spring.datasource.druid.goods.test-on-borrow=true
spring.datasource.druid.goods.test-while-idle=true
spring.datasource.druid.goods.time-between-eviction-runs-millis=60000
spring.datasource.druid.goods.filters=stat,wall,log4j
#不加filters在sql监控页没有数据

配置类

数据源一

@Configuration
@MapperScan(basePackages = "com.example.queshop.dao.goodsDao", sqlSessionFactoryRef = "goodsSqlSessionFactory")
public class goodsDataSourceConfig {
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(goodsDataSourceConfig.class);
    @Bean(value = "goodsDataSource")
    @ConfigurationProperties("spring.datasource.druid.goods")
    public DataSource goodsDataSource(){
        log.info("初始化数据源 二");
        return DruidDataSourceBuilder.create().build();
    }
    @Bean(name = "goodsTransactionManager")

    public DataSourceTransactionManager goodsTransactionManager() {
        return new DataSourceTransactionManager(goodsDataSource());
    }

    @Bean(name = "goodsSqlSessionFactory")

    public SqlSessionFactory goodssqlSessionFactory (@Qualifier("goodsDataSource") DataSource goodsDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(goodsDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:/mapper/productMapper.xml"));// 设置mapper文件路径
        return sessionFactory.getObject();
    }
    @Bean(name = "goodsSqlSessionTemplate")
    public SqlSessionTemplate goodssqlSessionTemplate(@Qualifier("goodsSqlSessionFactory") SqlSessionFactory sessionFactory) throws  Exception{
        return  new SqlSessionTemplate(sessionFactory);
    }}

数据源二

@Configuration
@MapperScan(basePackages = "com.example.queshop.dao", sqlSessionFactoryRef = "userSqlSessionFactory")
    public class userDataSourceConfig{
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(userDataSourceConfig.class);
    @Primary
    @Bean(value = "userDataSource")
    @ConfigurationProperties("spring.datasource.druid.jwt")
    public DataSource userDataSource(){
        log.info("初始化数据源 一");
        return DruidDataSourceBuilder.create().build();
    }
    @Bean(name = "userTransactionManager")
    @Primary
    public DataSourceTransactionManager userTransactionManager() {
        return new DataSourceTransactionManager(userDataSource());
    }

    @Bean(name = "userSqlSessionFactory")
    @Primary
    public SqlSessionFactory userSqlSessionFactory(@Qualifier("userDataSource") DataSource userDataSource)
            throws Exception {
        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(userDataSource);
        sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:/mapper/userMapper.xml"));
        return sessionFactory.getObject();
    }
    @Bean(name = "userSqlSessionTemplate")
    @Primary
    public SqlSessionTemplate usersqlSessionTemplate(@Qualifier("userSqlSessionFactory") SqlSessionFactory sessionFactory) throws  Exception{
        return  new SqlSessionTemplate(sessionFactory);
    }

配置详解(图片来源网络)

在这里插入图片描述

最后的druid监控页面


可以看见两个数据源以及配置filter类名

在测试一下是否成功

在这里插入图片描述
在这里插入图片描述
可以看见两个数据源都可以取得数据,配置成功

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值