springboot+mybatis多数据源配置

Posted killer-leon

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot+mybatis多数据源配置相关的知识,希望对你有一定的参考价值。

配置文件

pom包依赖配置:

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>$mybatis-spring-boot-starter.version</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>$druid.version</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>$mysql-connector-java.version</version>
</dependency>
<properties>
    <mysql-connector-java.version>5.1.34</mysql-connector-java.version>
    <druid.version>1.0.18</druid.version>
    <mybatis-spring-boot-starter.version>1.1.1</mybatis-spring-boot-starter.version>
</properties>
数据库配置:
druid.url=jdbc:mysql://localhost:3306/test1?useSSL=false&useUnicode=true&characterEncoding=utf-8
druid.driver-class=com.mysql.jdbc.Driver
druid.username=root
druid.password=root

druid2.url=jdbc:mysql://localhost:3306/test2?useSSL=false&useUnicode=true&characterEncoding=utf-8
druid2.driver-class=com.mysql.jdbc.Driver
druid2.username=root
druid2.password=root

一个test1库和一个test2库,其中test1位主库,在使用的过程中必须指定主库,不然会报错。

数据源配置@Configuration


 
@MapperScan(basePackages = "com.neo.mapper.test1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")
public class DataSource1Config 

    @Bean(name = "test1DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test1")
    @Primary
    public DataSource testDataSource() 
        return DataSourceBuilder.create().build();
    

    @Bean(name = "test1SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource) throws Exception 
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test1/*.xml"));
        return bean.getObject();
    

    @Bean(name = "test1TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource") DataSource dataSource) 
        return new DataSourceTransactionManager(dataSource);
    

    @Bean(name = "test1SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception 
        return new SqlSessionTemplate(sqlSessionFactory);
    

@Configuration
@MapperScan(basePackages = "com.neo.mapper.test2", sqlSessionTemplateRef  = "test2SqlSessionTemplate")
public class DataSource1Config 


    @Bean(name = "test2DataSource")
    @ConfigurationProperties(prefix = "spring.datasource.test2")
    @Primary
    public DataSource testDataSource() 
        return DataSourceBuilder.create().build();
    


    @Bean(name = "test2SqlSessionFactory")
    @Primary
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception 
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/test2/*.xml"));
        return bean.getObject();
    


    @Bean(name = "test2TransactionManager")
    @Primary
    public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) 
        return new DataSourceTransactionManager(dataSource);
    


    @Bean(name = "test2SqlSessionTemplate")
    @Primary
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception 
        return new SqlSessionTemplate(sqlSessionFactory);
    




最关键的地方就是这块了,一层一层注入,先创建DataSource,在创建SqlSessionFactory在创建事务,最后包装到SqlSessionTemplate中。其中需要制定分库的mapper文件地址,以及分库到层代码。

@MapperScan(basePackages = "com.neo.mapper.test1", sqlSessionTemplateRef  = "test1SqlSessionTemplate")
@MapperScan(basePackages = "com.neo.mapper.test2", sqlSessionTemplateRef  = "test2SqlSessionTemplate")

这块的注解就是指明了扫描dao层,并且给dao层注入指定的SqlSessionTemplate。所有@Bean都需要按照命名指定正确。

启动测试

@SpringBootApplication
public class Application 
    /**
     * 方法描述:启动程序
     * 
     * @author leon 2018年3月23日 下午8:07:50
     * @param args
     */
    public static void main(String[] args) 
        SpringApplication.run(Application.class, args);
    

以上是关于springboot+mybatis多数据源配置的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot入门之基于Druid配置Mybatis多数据源

springboot+mybatis多数据源配置

springBoot+mybatis多数据源配置

SpringBoot2 + Druid + Mybatis 多数据源配置

springboot:mybatis配置多数据源

springboot 整合 mybatis 多数据源配置