springboot的mybatis多数据源

2022-04-23 23:37:39 浏览数 (1)

现在的业务都是一个服务一个数据库,绝大部分场景用不着一个服务多个数据库。

有些场景可能会用到

  1. 数据库的分库分表,可以使用shardingjdbc
  2. 管理后台之类的项目,可能因为历史原因需要查多个数据库

java的web服务比较流行的是springboot

在springboot的多数据源本质是什么?

  • 构建多个datasource
  • 注册不同的sqlSessionFactory
  • 使用不同的配置scan即可

遇到的坑

  • 多数据源一定要写jdbc-url不要写url
  • 注册xml路径如果无法注入,可以直接配置到代码里面
代码语言:txt复制
server.port=8181
debug=false



spring.datasource.db1.username=root
spring.datasource.db1.password=123456
spring.datasource.db1.jdbc-url=jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.db1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db1.pool-name=HikariPool-2
spring.datasource.db1.minimum-idle=1
spring.datasource.db1.maximum-pool-size=5
spring.datasource.db1.idle-timeout=600000
spring.datasource.db1.max-lifetime=1200000
spring.datasource.db1.connection-timeout=30000
spring.datasource.db1.connection-test-query=SELECT 1



spring.datasource.db2.username=root
spring.datasource.db2.password=123456
spring.datasource.db2.jdbc-url=jdbc:mysql://127.0.0.1:3306/db2?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.db2.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.db2.pool-name=HikariPool-1
spring.datasource.db2.minimum-idle=1
spring.datasource.db2.maximum-pool-size=5
spring.datasource.db2.idle-timeout=600000
spring.datasource.db2.max-lifetime=1200000
spring.datasource.db2.connection-timeout=30000
spring.datasource.db2.connection-test-query=SELECT 1


logging.level.org.apache.ibatis=debug
logging.level.com.mysql.cj = debug
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis.type-aliases-package=cn.beckbi.model
mybatis.configuration.mapUnderscoreToCamelCase = true
代码语言:java复制
@Configuration
@MapperScan(value = "cn.beckbi.dao.mapper1", sqlSessionFactoryRef = "sqlSessionFactoryBean1")
public class MybatisOne {

    @Autowired
    @Qualifier("DB1")
    DataSource db1;

    @Bean
    @Primary
    SqlSessionFactory sqlSessionFactoryBean1() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(db1);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/mapper1/*.xml"));

        return factoryBean.getObject();
    }

    @Bean(name = "db1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager() {
        return new DataSourceTransactionManager(db1);
    }
    @Bean
    @Primary
    SqlSessionTemplate sqlSessionTemplate1() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryBean1());
    }

}

@Configuration
@MapperScan(value = "cn.beckbi.dao.mapper2", sqlSessionFactoryRef = "sqlSessionFactoryBean2")
public class MybatisTwo {

    @Autowired
    @Qualifier("DB2")
    DataSource db2;

    @Bean
    SqlSessionFactory sqlSessionFactoryBean2() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(db2);
        factoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:/mapper/mapper2/*.xml"));

        return factoryBean.getObject();
    }


    @Bean(name = "db2TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager() {
        return new DataSourceTransactionManager(db2);
    }

    @Bean
    SqlSessionTemplate sqlSessionTemplate2() throws Exception {
        return new SqlSessionTemplate(sqlSessionFactoryBean2());
    }
}
代码语言:sql复制
create database db1;
CREATE TABLE `user1` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `name` varchar(30) DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

create database db2;
CREATE TABLE `user2` (
  `id` bigint NOT NULL COMMENT '主键ID',
  `name` varchar(30) DEFAULT NULL COMMENT '姓名',
  `age` int DEFAULT NULL COMMENT '年龄',
  `email` varchar(50) DEFAULT NULL COMMENT '邮箱',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

0 人点赞