springboot连接两个或多个数据库

一、实现

        一般我们的SpringBoot项目只连一个数据库,下面是springboot连接2个以上的数据库方式

这里连得相同数据库,数据库名不一样做区分

1、配置application.yml,添加2个数据库连接

server:
  port: 8080

spring:
  datasource:
  
    one:
      driver-class-name: org.postgresql.Driver
      jdbc-url: jdbc:postgresql://127.0.0.1:5432/test001  #测试数据库
      username: postgres
      password: root
 
    two:
      driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
      jdbc-url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=test002  #测试数据库
      username: sa
      password: sa

注意:

(1)test001为主数据库,test002为从

(2)默认url更改为:jdbc-url

2、application启动类增加注解

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

//@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})
@SpringBootApplication(scanBasePackages = "com")
public class AcceptApplication {

	public static void main(String[] args) {
		SpringApplication.run(AcceptApplication.class, args);
	}

}

@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class})意为:去掉springboot服务启动时对于数据源配置的自动导入。后面我们会手动配置多个数据源。
如果有需要,也可把事务管理与mybatis的自动导入去掉,注解为:
@EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class,DataSourceTransactionManagerAutoConfiguration.class, MybatisAutoConfiguration.class})

3、使用代码进行数据源注入,和扫描dao层路径

 PostgresConfig.java 为主数据库,项目启动默认连接此数据库

注意:主数据库PostgresConfig需要 @Primary注解

import javax.sql.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;

@Configuration
@MapperScan(basePackages = "com.dao.test1",sqlSessionFactoryRef = "test1SqlSessionFactory")
public class PostgresConfig {

	@Primary
	@Bean(name = "DataSource1")
    @ConfigurationProperties(prefix = "spring.datasource.one")
    public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
 
	@Primary
    @Bean(name = "SqlSessionFactory1")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("DataSource1") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/dao/test1/*.xml"));
        return bean.getObject();
    }
    
    @Primary
    @Bean(name = "TransactionManager1")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("DataSource1") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
 
    @Primary
    @Bean(name = "SqlSessionTemplate1")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("SqlSessionFactory1") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
	
	
}

SqlServerConfig.java 为从数据库,不需要@Primary注解

import javax.sql.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;

@Configuration
@MapperScan(basePackages = "com.dao.test2",sqlSessionFactoryRef = "test2SqlSessionFactory")
public class SqlServerConfig {

	@Bean(name = "DataSource2")
    @ConfigurationProperties(prefix = "spring.datasource.two")
	public DataSource testDataSource() {
        return DataSourceBuilder.create().build();
    }
 
    @Bean(name = "SqlSessionFactory2")
    public SqlSessionFactory testSqlSessionFactory(@Qualifier("DataSource2") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:com/dao/test2/*.xml"));
        return bean.getObject();
    }
    
    @Bean(name = "TransactionManager2")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("DataSource2") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
 
    @Bean(name = "SqlSessionTemplate2")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("SqlSessionFactory2") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
	
}

4、在dao文件夹下,新建test1和test2两个包,分别放两个不同数据库的dao层文件

(1)StudentMapper.java

@Component
public interface StudentMapper {
    Student selectByPrimaryKey(Integer id);
}

(2)StuMapper.java

@Component
public interface StuMapper {
    Student selectByPrimaryKey(Integer id);
}

5、dao层的xml文件,路径看自己安排

        注意:dao文件名和xml文件名要一致

(1)StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.test1.StudentMapper">
 
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select * from student
    where id = #{id,jdbcType=INTEGER}
  </select>
 
</mapper>

(2)StuMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.test1.StudentMapper">
 
  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select * from stu
    where id = #{id,jdbcType=INTEGER}
  </select>
 
</mapper>

6、测试,controller控制层,注入两个数据库的dao,分别查询

@RestController
@RequestMapping(value = "/stu")
public class JDBCController {

	@Autowired
	private StudentMapper studentMapper;
	
	@Autowired
	private StuMapper stuMapper;
	
	@GetMapping("/getStudent")
    public Student getStudentByPostgres(){
        return studentMapper.selectByPrimaryKey(2);
    }
	
	@GetMapping("/getStu")
    public Stu getStuByPostgres(){
        return stuMapper.selectByPrimaryKey(2);
    }
	
}

7、启动项目,浏览器访问

http://localhost:8080/stu/getStudent

{"id":2,"name":"王五","age":24,"sex":"男"}

http://localhost:8080/stu/getStu

{"id":2,"name":"李四","age":21,"sex":"女"}

8、pom

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.0.1</version>
</dependency>

<!-- 连接数据库postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.7</version>
</dependency>

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
</dependency>

附:配置类内容

1、SqlSessionFactory

SqlSessionFactory是MyBatis的关键对象,它是个单个数据库映射关系经过编译后的内存镜像,SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象类获得,而SqlSessionFactoryBuilder则可以从XML配置文件或一个预先定制的Configuration的实例构建出SqlSessionFactory的实例。每一个MyBatis的应用程序都以一个SqlSessionFactory对象的实例为核心,同时SqlSessionFactory也是线程安全的,SqlSessionFactory一旦被创建,应该在应用执行期间都存在,在应用运行期间不要重复创建多次,建议使用单例模式,SqlSessionFactory是创建SqlSession的工厂。

2、SqlSession

SqlSession是MyBatis的关键对象,是执行持久化操作的独享,类似于JDBC中的Connection。它是应用程序与持久层之间执行交互操作的一个单线程对象,也是MyBatis执行持久化操作的关键对象。SqlSession对象完全包含以数据库为背景的所有执行SQL操作的方法,它的底层封装了JDBC连接,可以用SqlSession实例来直接执行被映射的SQL语句。每个线程都应该有它自己的SqlSession实例。SqlSession的实例不能被共享,同时SqlSession也是线程不安全的,绝对不能讲SqlSeesion实例的引用放在一个类的静态字段甚至是实例字段中。也绝不能将SqlSession实例的引用放在任何类型的管理范围中,比如Servlet当中的HttpSession对象中,使用完SqlSeesion之后关闭Session很重要,应该确保使用finally块来关闭它。

3、SqlSession创建过程

mybatis框架主要是围绕着SqlSessionFactory进行的,创建过程大概如下:

  1. 定义一个Configuration对象,其中包含数据源、事务、mapper文件资源以及影响数据库行为属性设置settings。
  2. 通过配置对象,则可以创建一个SqlSessionFactoryBuilder对象
  3. 通过 SqlSessionFactoryBuilder 获得SqlSessionFactory 的实例。
  4. SqlSessionFactory 的实例可以获得操作数据的SqlSession实例,通过这个实例对数据库进行操作。

4、SqlSessionTemplate

SqlSessionTemplate是MyBatis-Spring的核心。这个类负责管理MyBatis的SqlSession,调用MyBatis的SQL方法,翻译异常。SqlSessionTemplate是线程安全的,可以被多个DAO所共享使用。
当调用SQL方法时,包含从映射器getMapper()方法返回的方法,SqlSessionTemplate将会保证使用的SqlSession是和当前Spring的事务相关的。此外,它管理session的生命周期,包含必要的关闭,提交或回滚操作。
SqlSessionTemplate实现了SqlSession,这就是说要对MyBatis的SqlSession进行简易替换。
SqlSessionTemplate通常是被用来替代默认的MyBatis实现的DefaultSqlSession,因为它不能参与到Spring的事务中也不能被注入,因为它是线程不安全的。相同应用程序中两个类之间的转换可能会引起数据一致性的问题。
SqlSessionTemplate对象可以使用SqlSessionFactory作为构造方法的参数来创建。

5、DataSourceTransactionManager 核心事务管理器

对JDBC(Java Data Base Connectivity,java数据库连接)进行事务管理,在spring中是对JdbcTemplate进行事务管理。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值