Springboot Mybatis实现多数据源

准备

依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.5.4.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>1.3.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>6.0.4</version>
</dependency>

结构

这里写图片描述

数据源

单个数据源

Spring Boot默认已经封装好了JDBC(或SqlSession等),使用时只需要配置相关属性就行了。

application.properties

# mysql
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false
spring.datasource.username=username
spring.datasource.password=password

# mybatis config
mybatis.type-aliases-package=com.git.lee.spring.boot.example.model
mybatis.mapper-locations=classpath*:com/git/lee/spring/boot/example/mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

Application

@SpringBootApplication
@MapperScan("com.git.lee.spring.boot.example.mapper")
public class ExApplication {

    public static void main(String[] args) {
        SpringApplication.run(ExApplication.class, args);
    }
}

多个数据源

不同数据源创建不同的DataSource(SqlSessionFactory),Mapper Scan Package也不一样。

application.properties

# mysql
first.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
first.datasource.url=jdbc:mysql://localhost:3306/first?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false
first.datasource.username=username
first.datasource.password=password

second.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
second.datasource.url=jdbc:mysql://localhost:3306/second?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&serverTimezone=UTC&useSSL=false
second.datasource.username=username
second.datasource.password=password

# mybatis config
mybatis.type-aliases-package=com.git.lee.spring.boot.example.model
mybatis.mapper-locations=classpath*:com/git/lee/spring/boot/example/mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case=true

DataSource Configuration

@Configuration
@MapperScan(value = "com.git.lee.spring.boot.example.mapper.first", sqlSessionFactoryRef = "firstSqlSessionFactory")
@EnableConfigurationProperties(MybatisProperties.class)
public class FirstDataSourceConfig {

    private MybatisProperties mybatisProperties;

    public FirstDataSourceConfig(MybatisProperties properties) {
        this.mybatisProperties = properties;
    }

    @Bean(name = "firstDataSource")
    @Primary
    @ConfigurationProperties(prefix = "first.datasource")
    public DataSource getDataSource() {
        return new org.apache.tomcat.jdbc.pool.DataSource();
    }


    @Bean(name = "firstSqlSessionFactory")
    @Primary
    public SqlSessionFactory getSqlSessionFactory() throws Exception {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(getDataSource());
        sessionFactory.setMapperLocations(mybatisProperties.resolveMapperLocations());
        sessionFactory.setTypeAliasesPackage(mybatisProperties.getTypeAliasesPackage());
        sessionFactory.setConfiguration(SqlSessionFactoryConfigUtil.clone(mybatisProperties.getConfiguration()));
        return sessionFactory.getObject();
    }

}

其他数据源同理。只需要把bean的name改下,MapperScan的value改下;还有把@Primary去掉(只能有一个);

这样,不同的mapper就可以操作不同的数据库了。

源码地址:https://github.com/shuaiweili/spring-boot-example

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值