Mybatis multi database in spring boot 2

版权声明:欢迎分享技术,转载请声明。更多技术见 github.com/javahongxi https://blog.csdn.net/javahongxi/article/details/86160179
@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

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试