@
SpringBoot+MyBatis配置两种数据源
前言
前段时间做项目,要配置两种数据源,以后也是会经常遇到的,下面记录一下如何用pringBoot+MyBatis配置两种数据源。公司通用数据库是postgresql和Oracle,无奈Oracle实在太大,用MySQL代替一下。
一、. 项目结构
二、pom.xml
添加两种数据源的相关依赖
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
三、application.properties
这里也要配置两种数据源
spring.datasource.master.jdbc-url=jdbc:postgresql://39.99....
spring.datasource.master.username=
spring.datasource.master.password=
spring.datasource.master.driver-class-name=org.postgresql.Driver
spring.datasource.slave.jdbc-url=jdbc:mysql:// ........./.......?serverTimezone=GMT&useUnicode=true&characterEncoding=utf8
spring.datasource.slave.username=
spring.datasource.slave.password=
spring.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.master、spring.datasource.slave分别代表两种数据源的前缀,url要改成 jdbc-url,不然会报错:
spring.datasource.url 数据库的 JDBC URL。
spring.datasource.jdbc-url 用来重写自定义连接池
官方文档的解释是:
因为连接池的实际类型没有被公开,所以在您的自定义数据源的元数据中没有生成密钥,而且在IDE中没有完成(因为DataSource接口没有暴露属性)。另外,如果您碰巧在类路径上有Hikari,那么这个基本设置就不起作用了,因为Hikari没有url属性(但是确实有一个jdbcUrl属性)。
这里是引用 https://blog.csdn.net/newbie_907486852/article/details/81391525
注意:
com.mysql.jdbc.Driver 是 mysql-connector-java 5中的,
com.mysql.cj.jdbc.Driver 是 mysql-connector-java 6中的
四、 ※配置类
MasterConfig
package com.appport.config;
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;
//表示这个类为一个配置类
@Configuration
//配置mybatis的接口类放的地方
@MapperScan(basePackages = "com.appport.mapperone", sqlSessionFactoryRef = "masterSqlSessionFactory")
public class MasterConfig {
// 将这个对象放入Spring容器中
@Bean(name = "masterDataSource")
// 表示这个数据源是默认数据源
@Primary
// 读取application.properties中的配置参数映射成为一个对象
// prefix表示参数的前缀
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource getDateSource1() {
return DataSourceBuilder.create().build();
}
@Bean(name = "masterSqlSessionFactory")
// 表示这个数据源是默认数据源
@Primary
// @Qualifier表示查找Spring容器中名字为masterDataSource的对象
public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
// 设置mybatis的xml所在位置
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/one/*.xml"));
/* System.out.println("dddddddddddddddddddddddddddddd"); */
return bean.getObject();
}
@Bean("masterSqlSessionTemplate")
// 表示这个数据源是默认数据源
@Primary
public SqlSessionTemplate mastersqlsessiontemplate(
@Qualifier("masterSqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
SlaveConfig
package com.appport.config;
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.core.io.support.PathMatchingResourcePatternResolver;
@Configuration
@MapperScan(basePackages = "com.appport.mappertwo", sqlSessionFactoryRef = "slaveSqlSessionFactory")
public class SlaveConfig {
@Bean(name = "slaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource getDateSource2() {
return DataSourceBuilder.create().build();
}
@Bean(name = "slaveSqlSessionFactory")
public SqlSessionFactory slaveSqlSessionFactory(@Qualifier("slaveDataSource") DataSource datasource)
throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
bean.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/two/*.xml"));
return bean.getObject();
}
@Bean("slaveSqlSessionTemplate")
public SqlSessionTemplate slavesqlsessiontemplate(
@Qualifier("slaveSqlSessionFactory") SqlSessionFactory sessionfactory) {
return new SqlSessionTemplate(sessionfactory);
}
}
五. 测试相关代码
mapper:
package com.appport.mapperone;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import com.appport.domain.Qyxxb;
@Repository
@Mapper
public interface QyxxbMapper {
public List<Qyxxb> SelectQymc(Qyxxb qyxxb);
}
package com.appport.mappertwo;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import com.appport.domain.BidwinningNotice;
@Repository
@Mapper
public interface BidwinningNoticeMapper {
public List<BidwinningNotice> selectAll(String xm_bh);
}
mapper.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.appport.mapperone.QyxxbMapper">
<select id="SelectQymc" parameterType="java.util.Map" resultType="com.appport.domain.Qyxxb">
select * from qyxxb
</select>
</mapper>
<?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.appport.mappertwo.BidwinningNoticeMapper">
<select id="selectAll" resultType="com.appport.domain.BidwinningNotice">
select * from bidwinningnotice where xm_bh=#{xm_bh};
</select>
</mapper>
service:
package com.appport.service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.appport.domain.BidwinningNotice;
import com.appport.mappertwo.BidwinningNoticeMapper;
@Service
public class BidwinningNoticeService {
//定义日志打印
private static final Logger log = LoggerFactory.getLogger(BidwinningNoticeService.class);
@Autowired
private BidwinningNoticeMapper bidwinningNoticeMapper;
public List<BidwinningNotice> selectAll(String xm_bh){
return bidwinningNoticeMapper.selectAll(xm_bh);
}
}
package com.appport.service;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.appport.domain.Qyxxb;
import com.appport.mapperone.QyxxbMapper;
import com.appport.service.QyxxbService;
@Service
public class QyxxbService {
private static final Logger log = LoggerFactory.getLogger(QyxxbService.class);
@Autowired
private QyxxbMapper qyxxbMapper;
/**
* @param 查询企业信息
* */
public List<Qyxxb> SelectQymc(Qyxxb qyxxb)
{
List<Qyxxb> a=qyxxbMapper.SelectQymc(qyxxb);
return a;
}
}
controller:
package com.appport.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.appport.domain.BidwinningNotice;
import com.appport.domain.Qyxxb;
import com.appport.service.BidwinningNoticeService;
import com.appport.service.QyxxbService;
@RestController
public class NetDiskFileController {
@Autowired
private BidwinningNoticeService bidwinningNoticeService;
@Autowired
private QyxxbService qyxxbService;
@RequestMapping("/selectAll")
public List<BidwinningNotice> selectAll(String xm_bh){
List<BidwinningNotice> list=bidwinningNoticeService.selectAll(xm_bh);
System.out.println(list);
return list;
}
@RequestMapping("/SelectQymc")
public List<Qyxxb> SelectQymc(Qyxxb qyxxb){
List<Qyxxb> list=qyxxbService.SelectQymc(qyxxb);
System.out.println(list);
return list;
}
}
六. 执行结果
七. 总结
百度很强大,一大堆的案例,至于我参考的哪位大牛的,很遗憾,后来怎么找也找不到了,在此谢谢大佬了。。。