idea springboot 配置MySQL SQL server双数据库
首先导入相关的Jar包(其他最基本的就省略不写了)
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.2.2.jre8</version>
</dependency>
然后配置 application.properties
在properties里面配置两个数据源 一个master 一个other并把相应的数据添加上
#注意classpath对应的位置 否则加载不到
mybatis.mapper-locations=classpath*:/*mapper/*.xml
spring.datasource.master.jdbc-url=jdbc:mysql://localhost:33051/plmss?useUnicode=true&characterEncoding=utf-8
spring.datasource.master.username=root
spring.datasource.master.password=root
spring.datasource.master.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.other.jdbc-url=jdbc:sqlserver://localhost:1433;DatabaseName=user
spring.datasource.other.username=user
spring.datasource.other.password=user
spring.datasource.other.driver-class-name=com.microsoft.sqlserver.jdbc.SQLServerDriver
主数据库配置:
新建一个datasource 里面专门放置 如图下
MySQL 数据库配置
package com.lenovo.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.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages ="com.lenovo.mapper", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MybatisDbMasterConfig {
@Primary
@Bean(name = "masterDataSource")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Primary
@Bean(name = "masterSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("masterDataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.lenovo.entity");
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
return factoryBean.getObject();
}
@Primary
@Bean(name = "masterTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("masterDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "masterSqlSessionTemplate")
@Primary
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("masterSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
SQL server数据库配置
package com.lenovo.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.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.lenovo.otherDB", sqlSessionFactoryRef = "otherSqlSessionFactory")
public class MybatisDbOtherConfig {
@Bean(name = "otherDataSource")
@ConfigurationProperties(prefix = "spring.datasource.other")
public DataSource dataSource() {
return DataSourceBuilder.create().build();
}
@Bean(name = "otherTransactionManager")
public DataSourceTransactionManager transactionManager(@Qualifier("otherDataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean(name = "otherSqlSessionFactory")
public SqlSessionFactory basicSqlSessionFactory(@Qualifier("otherDataSource") DataSource basicDataSource) throws Exception {
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(basicDataSource);
factoryBean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:othermapper/*.xml"));
return factoryBean.getObject();
}
@Bean(name = "otherSqlSessionTemplate")
public SqlSessionTemplate testSqlSessionTemplate(
@Qualifier("otherSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
最后在启动类上添加注解启动即可
package com.lenovo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.servlet.MultipartConfigElement;
@SpringBootApplication
@EnableConfigurationProperties
@EnableScheduling
public class PlmselfserviceApplication{
public static void main(String[] args) {
SpringApplication.run(PlmselfserviceApplication.class, args);
}
}
调用在接口实现类上
@Autowired
private OtherDBMapper otherDBMapper;
包结构总览
楼主亲测 并对其进行修改