Skip to content

Mybatis multi database in spring boot 2

javahongxi edited this page Jan 13, 2019 · 1 revision
@Configuration
public class MybatisConfig {

    @MapperScan(basePackages = "org.hongxi.whatsmars.boot.sample.mybatis.dao.user", sqlSessionTemplateRef = "userSqlSessionTemplate")
    private class userMapperScan {}

    @MapperScan(basePackages = "org.hongxi.whatsmars.boot.sample.mybatis.dao.trade", sqlSessionTemplateRef = "tradeSqlSessionTemplate")
    private class tradeMapperScan {}

    @Bean("userSqlSessionTemplate")
    public SqlSessionTemplate userSqlSessionTemplate(
            @Qualifier("userSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean("tradeSqlSessionTemplate")
    public SqlSessionTemplate tradeSqlSessionTemplate(
            @Qualifier("tradeSqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }

    @Bean("userSqlSessionFactory")
    public SqlSessionFactory userSqlSessionFactory(
            @Qualifier("userDatasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTypeAliasesPackage("org.hongxi.whatsmars.boot.sample.mybatis.domain");
        String[] mapperLocations = {"classpath:mapper/*.xml"};
        factory.setMapperLocations(resolveMapperLocations(mapperLocations));
        return factory.getObject();
    }

    @Bean("tradeSqlSessionFactory")
    public SqlSessionFactory tradeSqlSessionFactory(
            @Qualifier("tradeDatasource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setTypeAliasesPackage("org.hongxi.whatsmars.boot.sample.mybatis.domain");
        String[] mapperLocations = {"classpath:mapper/*.xml"};
        factory.setMapperLocations(resolveMapperLocations(mapperLocations));
        return factory.getObject();
    }

    @Bean("userDatasource")
    @ConfigurationProperties(prefix = "user.datasource")
    public DataSource userDatasource() {
        return DataSourceBuilder.create().build();
    }

    @Bean("tradeDatasource")
    @ConfigurationProperties(prefix = "trade.datasource")
    public DataSource tradeDatasource() {
        return DataSourceBuilder.create().build();
    }

    private Resource[] resolveMapperLocations(String[] mapperLocations) {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        List<Resource> resources = new ArrayList<Resource>();
        if (mapperLocations != null) {
            for (String mapperLocation : mapperLocations) {
                try {
                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                } catch (IOException e) {
                    // ignore
                }
            }
        }
        return resources.toArray(new Resource[resources.size()]);
    }
}
user:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    jdbc-url: jdbc:mysql://db.hongxi.org:3306/user?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
    username: root
    password: n36bxXMnHina
    connectionTimeout: 30000
    idleTimeout: 60000
    maxLifetime: 1800000
    minimumIdle: 5
    maximumPoolSize: 10
    connection-test-query: SELECT 1

trade:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    jdbc-url: jdbc:mysql://db.hongxi.org:3306/trade?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
    username: root
    password: n36bxXMnHina
    connectionTimeout: 30000
    idleTimeout: 60000
    maxLifetime: 1800000
    minimumIdle: 5
    maximumPoolSize: 10
    connection-test-query: SELECT 1

首页

Java核心技术

Netty

RocketMQ深入研究

kafka深入研究

Pulsar深入研究

Dubbo源码导读

微服务架构

Redis

Elasticsearch

其他

杂谈

关于我

Clone this wiki locally