springboot mybatis多數據源

需求

項目有一個需求,須要從多數據源讀取數據。所以使用springboot+mybatis進行多數據源配置和操做。java

依賴引入

<!-- mybatis db -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.13</version>
        </dependency>

配置文件

# ds1數據源配置
spring.datasource.ds1.url=jdbc:mysql://ip1:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.ds1.username=xxxxxx
spring.datasource.ds1.password=xxxxxx
spring.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver

# ds2數據源配置
spring.datasource.ds2.url=jdbc:mysql://ip2:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.ds2.username=xxxxxx
spring.datasource.ds2.password=xxxxxx
spring.datasource.ds2.driver-class-name=com.mysql.cj.jdbc.Driver

# ds3數據源配置
spring.datasource.ds3.url=jdbc:mysql://ip3:port/database?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8
spring.datasource.ds3.username=xxxxxx
spring.datasource.ds3.password=xxxxxx
spring.datasource.ds3.driver-class-name=com.mysql.cj.jdbc.Driver

Java配置

package com.qishu.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
@MapperScan(basePackages = "com.qishu.demo.ds1.dao", sqlSessionFactoryRef = "ds1SqlSessionFactory")
public class Ds1DatasourceConfig {

    @Primary
    @Bean(name = "ds1DataSource")
    @ConfigurationProperties("spring.datasource.ds1")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean(name = "ds1SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("ds1DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:ds1-mapper/*.xml"));
        return sessionFactoryBean.getObject();
    }
}
package com.qishu.demo.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

@Configuration
// 掃描的包路徑
@MapperScan(basePackages = "com.qishu.demo.ds2.dao", sqlSessionFactoryRef = "ds2SqlSessionFactory")
public class Ds2DatasourceConfig {

    @Bean(name = "ds2DataSource")
    // 配置前綴
    @ConfigurationProperties("spring.datasource.ds2")
    public DataSource masterDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "ds2SqlSessionFactory")
    public SqlSessionFactory sqlSessionFactory(@Qualifier("ds2DataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
        sessionFactoryBean.setDataSource(dataSource);
        // mapper文件目錄
        sessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:ds2-mapper/*.xml"));
        return sessionFactoryBean.getObject();
    }
}

ds3就不寫了mysql

使用方式spring

@Autowired
    private AppDAO appDAO;

修改啓動類

// 排除自動註冊配置
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})

單元測試

// EnableAutoConfiguration註解,關閉springBoot關於mybatis的一些自動注入。
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})
public class AppDAOTest {

    @Autowired
    private AppDAO appDAO;


    @Test
    public void selectTest() {
        AppDO appDO = appDAO.selectByPrimaryKey(70);
        System.out.println(JSON.toJSONString(appDO));
    }
}

目錄結構

問題彙總

在ds1中使用 @Primary 對SqlSessionFactory和DataSource的Bean進行指定,不然會報sql

NoUniqueBeanDefinitionException: No qualifying bean of type SqlSessionFactoryapache

相關文章
相關標籤/搜索