赞
踩
java项目同时访问两个数据库的配置
(1)在application.yml配置文件中对两个数据库进行访问配置(任何两种数据库都可以用如下的配置)
# ----------------------------------------------------------------> 数据库
datasource:
one:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/anji_tea?useSSL = false&useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
username: root
password: 123456
two:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/postgis_24_sample
username: postgres
password: 123456
(2)分别创建两个类进行两个数据库访问的配置
①数据库1
package com.sunvua.config; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; 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 org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * 数据库1配置 * @author */ @Configuration @MapperScan(basePackages = "com.sunvua.sys.**.mapper.**", sqlSessionTemplateRef = "oneSqlSessionTemplate") public class OneDataSourceConfig { @Bean(name = "oneDataSource") @ConfigurationProperties(prefix = "spring.datasource.one") @Primary public DataSource oneDataSource(){ return DataSourceBuilder.create().build(); } @Bean(name = "oneSqlSessionFactory") @Primary public SqlSessionFactory testSqlSessionFactory(@Qualifier("oneDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/**/*.xml")); return bean.getObject(); } @Bean(name = "oneTransactionManager") @Primary public DataSourceTransactionManager testTransactionManager(@Qualifier("oneDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "oneSqlSessionTemplate") @Primary public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("oneSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); } }
②数据库2
package com.sunvua.config; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.SqlSessionTemplate; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /** * 数据库2配置 * @author */ @Configuration @MapperScan(basePackages = "com.sunvua.twokinds.**.mapper.**",sqlSessionTemplateRef = "twoSqlSessionTemplate") public class TwoDataSourceConfig { @Bean(name = "twoDataSource") @ConfigurationProperties(prefix = "spring.datasource.two") public DataSource testDataSource() { return DataSourceBuilder.create().build(); } @Bean(name = "twoSqlSessionFactory") public SqlSessionFactory testSqlSessionFactory(@Qualifier("twoDataSource") DataSource dataSource) throws Exception { SqlSessionFactoryBean bean = new SqlSessionFactoryBean(); bean.setDataSource(dataSource); bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mappers/**/*.xml")); return bean.getObject(); } @Bean(name = "twoTransactionManager") public DataSourceTransactionManager testTransactionManager(@Qualifier("twoDataSource") DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean(name = "twoSqlSessionTemplate") public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("twoSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception { return new SqlSessionTemplate(sqlSessionFactory); }
.xml的mybatis分布:

方法类中的结构如下:数据库1的方法mapper路径

数据库2的方法类mapper路径

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。