springboot+mybatis多数据源配置
来源:互联网 发布:ubuntu游客创建用户 编辑:程序博客网 时间:2024/06/13 18:55
1、数据源信息
spring.datasource.pg.driverClassName = org.postgresql.Driverspring.datasource.pg.url = jdbc:postgresql://x.x.x.x:xxxx/xxxspring.datasource.pg.username = xxxspring.datasource.pg.password = xxxspring.datasource.ms.driverClassName = com.mysql.jdbc.Driverspring.datasource.ms.url = jdbc:mysql://x.x.x.x:xxxx/xxxspring.datasource.ms.username = xxxspring.datasource.ms.password = xxx
2、mybatis相关配置
数据源1配置:(@Primary为设置主数据源标签,在多数据源情况下必须指定主数据源)
@Configuration@MapperScan(basePackages = "com.hysyn.mapper.pg", sqlSessionTemplateRef = "pgSqlSessionTemplate")public class DataSource1Config { @Bean(name = "pgDataSource") @ConfigurationProperties(prefix = "spring.datasource.pg") @Primary public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "pgSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("pgDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/pg/*.xml")); return bean.getObject(); } @Bean(name = "pgTransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("pgDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "pgSqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("pgSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }}数据源配置:
@Configuration@MapperScan(basePackages = "com.hysyn.mapper.ms", sqlSessionTemplateRef = "msSqlSessionTemplate")public class DataSource2Config { @Bean(name = "msDataSource") @ConfigurationProperties(prefix = "spring.datasource.ms") public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "msSqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("msDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mybatis/mapper/ms/*.xml")); return bean.getObject(); } @Bean(name = "msTransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("msDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "msSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("msSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }}
阅读全文
0 0
- SpringBoot、MyBatis配置多数据源
- springboot+mybatis配置多数据源
- Springboot配置Mybatis多数据源
- springBoot+mybatis多数据源的配置
- SpringBoot下配置Mybatis多数据源
- springboot+mybatis配置多数据源
- springboot+mybatis多数据源配置
- SpringBoot多数据源的配置(SpringBoot+MyBatis)
- springboot + mybatis + 多数据源
- SpringBoot+Mybatis多数据源
- springboot + mybatis + 多数据源
- SpringBoot、MyBatis配置多数据源XML方法
- SpringBoot多数据源及MyBatis配置详解
- SpringBoot之MyBatis多数据源配置实现
- springboot 配置多数据源
- springboot 配置多数据源
- springboot 多数据源配置
- Springboot 多数据源配置
- Linux基础操作(五)
- 我的Laravel学习之路之实现简单Blog展示
- 【大数据学习】数学基础及应用
- SQL 中 Count 和Distinct的使用
- 我回来了
- springboot+mybatis多数据源配置
- 二次采样
- 记录2--
- 给定一个二叉树,找到最大路径和。该路径可以在树中的任何节点处开始和结束。
- 2017第十七届成都建筑及装饰材料博览会会刊(参展商名录)
- servlet是单线程还是多线程?
- 3、源码级跨平台开发——选择运行时库2
- Unity优化杂谈1
- 使用CMake来进行Android NDK开发