-
Notifications
You must be signed in to change notification settings - Fork 614
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
wiki.hongxi.org
首页
Java核心技术
- JUC JMM与线程安全
- JUC 指令重排与内存屏障
- JUC Java内存模型FAQ
- JUC 同步和Java内存模型
- JUC volatile实现原理
- JUC AQS详解
- JUC AQS理解
- JUC synchronized优化
- JUC 线程和同步
- JUC 线程状态
- JUC 线程通信
- JUC ThreadLocal介绍及原理
- JUC 死锁及避免方案
- JUC 读写锁简单实现
- JUC 信号量
- JUC 阻塞队列
- NIO Overview
- NIO Channel
- NIO Buffer
- NIO Scatter与Gather
- NIO Channel to Channel Transfers
- NIO Selector
- NIO FileChannel
- NIO SocketChannel
- NIO ServerSocketChannel
- NIO Non-blocking Server
- NIO DatagramChannel
- NIO Pipe
- NIO NIO vs. IO
- NIO DirectBuffer
- NIO zero-copy
- NIO Source Code
- NIO HTTP Protocol
- NIO epoll bug
- Reflection 基础
- Reflection 动态代理
- JVM相关
- 设计模式典型案例
Netty
RocketMQ深入研究
kafka深入研究
Pulsar深入研究
Dubbo源码导读
- Dubbo SPI
- Dubbo 自适应拓展机制
- Dubbo 服务导出
- Dubbo 服务引用
- Dubbo 服务字典
- Dubbo 服务路由
- Dubbo 集群
- Dubbo 负载均衡
- Dubbo 服务调用过程
微服务架构
Redis
Elasticsearch
其他
- Dubbo 框架设计
- Dubbo 优雅停机
- dubbo-spring-boot-starter使用指南
- rocketmq-spring-boot-starter使用指南
- Mybatis multi-database in spring-boot 2
- RocketMQ 客户端简单封装
- Otter 入门
杂谈
关于我