spring-boot配置双数据源mysql+sqlserver
说来惭愧,从业程序这么多年,还没有弄过双数据库源,这次公司指派了一个任务,在已有的项目里和已有的一个数据库里,还需要在其它服务器的数据里拿数据。于是这篇文章横空出世。首先,我要感谢这位博主。我只看了他这个文章,不出一小时,全配置完+程序跑起顺溜。https://blog.csdn.net/qq_41076797/article/details/83215696?utm_medium=distrib
说来惭愧,从业程序这么多年,还没有弄过双数据库源,这次公司指派了一个任务,在已有的项目里和已有的一个数据库里,还需要在其它服务器的数据里拿数据。于是这篇文章横空出世。
首先,我要感谢这位博主。我只看了他这个文章,不出一小时,全配置完+程序跑起顺溜。
这位博主知识贡献不错,在我看来好像也没有漏洞,我估且把它写得再充满一点。
1.首先我的程序结构长这样
2.动手第一步,配置好application.properties
spring.datasource.test1.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.test1.jdbc-url=jdbc:mysql://localhost:3306/ext_upload_files?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false
spring.datasource.test1.minPoolSize=3
spring.datasource.test1.maxPoolSize=100
spring.datasource.test1.initialPoolSize=3
spring.datasource.test2.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.test2.jdbc-url=jdbc:sqlserver://localhost:80;DatabaseName=AIS20190910163600
spring.datasource.test2.minPoolSize=3
spring.datasource.test2.maxPoolSize=100
spring.datasource.test2.initialPoolSize=3
spring.datasource.test1.username=root
spring.datasource.test1.password=root
spring.datasource.test2.username=cosun
spring.datasource.test2.password=cosun&2004
3.配置pom.xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.22</version>
</dependency>
版本建议就按照这上面的,不然可能会出现版本不兼容问题。
4.config配置
新建两个文件夹,我的是
databymysql为
package com.cosun.cosunp.datasource;
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.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
* @author:homey Wong
* @Date: 2020/5/11 上午 11:10
* @Description:
* @Modified By:
* @Modified-date:
*/
@Configuration
@MapperScan(basePackages = {"com.cosun.cosunp.service.impl","com.cosun.cosunp.mapper"},sqlSessionFactoryRef = "test1SqlSessionFactory")
//这里一定要注意,这个basePackages是你的mapper接口及service所在的包名
public class DataByMySQL {
@Bean(name="test1DataSource")
@ConfigurationProperties(prefix ="spring.datasource.test1")
public DataSource testDataSource(){
return DataSourceBuilder.create().build();
}
@Bean(name="test1SqlSessionFactory")
public SqlSessionFactoryBean testSqlSessionFactory(@Qualifier("test1DataSource") DataSource dataSource)throws Exception{
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
try {
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage("com.cosun.cosunp.entity");
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:com/cosun/cosunp/mapper/*.java");
sessionFactory.setMapperLocations(resources);
}catch (Exception e) {
e.printStackTrace();
throw e;
}
return sessionFactory;
}
@Bean(name = "test1TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test1DataSource")DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
}
@Bean(name="test1SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test1SqlSessionFactory")SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
databysqlserver内容为
package com.cosun.cosunp.datasource;
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.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
/**
* @author:homey Wong
* @Date: 2020/5/11 0011 上午 11:11
* @Description:
* @Modified By:
* @Modified-date:
*/
@Configuration
@MapperScan(basePackages = {"com.cosun.cosunp.service2","com.cosun.cosunp.mapper2"},
//这里一定要注意,这个basePackages是你的mapper接口及service所在的包名
sqlSessionFactoryRef = "test2SqlSessionFactory")
public class DataBySqlServer {
@Bean(name = "test2DataSource")
@ConfigurationProperties(prefix = "spring.datasource.test2")
public DataSource testDataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "test2SqlSessionFactory")
public SqlSessionFactoryBean testSqlSessionFactory(@Qualifier("test2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
try {
sessionFactory.setDataSource(dataSource);
sessionFactory.setTypeAliasesPackage("com.cosun.cosunp.entity");
Resource[] resources = new PathMatchingResourcePatternResolver().getResources("classpath*:com/cosun/cosunp/mapper2/*.java");
sessionFactory.setMapperLocations(resources);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return sessionFactory;
}
@Bean(name = "test2TransactionManager")
public DataSourceTransactionManager testTransactionManager(@Qualifier("test2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "test2SqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("test2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
我的项目是用mybaties的注解开发,所以这是我的配置,和给我帮助的那个博主是不一样的,如果你是xml文件开发,可以参考上面我给的那个博主链接。
5.到此就完成了
但还有一个地方需要配置,就是service里的事物。
@Service
@Transactional(value="test1TransactionManager",rollbackFor = Exception.class)
public class DesignServiceImpl implements IDesignServ {
即 @Transactional(value="test1TransactionManager",rollbackFor = Exception.class) 这里填上所需要的某个事物。
这是我的方法,当然一定有更简单的方法,就是直接在config里配置,不过我时间有限,就不研究了。
更多推荐
所有评论(0)