hikari数据源配置类_SpringBoot2集成Mybatis Hikari多数据源配置
在开发微服务的过程中,有时我们的服务需要连接两个以上的数据库进行业务数据的CRUD操作,这时候就需要我们进行多数据源的配置。
pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
com.tl
hello-spring-boot-dependencies
1.0.0
../hello-spring-boot-dependencies/pom.xml
hello-spring-boot-mybatis-hikari
jar
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
org.springframework.boot
spring-boot-devtools
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.mybatis.spring.boot
mybatis-spring-boot-starter
org.projectlombok
lombok
io.springfox
springfox-swagger2
io.springfox
springfox-swagger-ui
org.apache.commons
commons-lang3
主要配置文件
datasource.properties
hikari.primary.jdbc-url=jdbc:mysql://localhost:3306/source_data
hikari.primary.username=root
hikari.primary.password=root
hikari.primary.driver-class-name=com.mysql.cj.jdbc.Driver
hikari.second.jdbc-url=jdbc:mysql://localhost:3306/target_data
hikari.second.username=root
hikari.second.password= root
hikari.second.driver-class-name=com.mysql.cj.jdbc.Driver
数据库连接池配置
PrimaryDataSource.java
package com.tl.hello.spring.boot.mybatis.hikari.config;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author tianl
* @date 2020/3/27 22:11
*/
@Configuration
@MapperScan(basePackages = {"com.tl.hello.spring.boot.mybatis.druid.dao.primary"}, sqlSessionFactoryRef = "primarySqlSessionFactory")
@PropertySource("classpath:config/datasource.properties")
public class PrimaryDataSourceConfig {
@Value("${hikari.primary.jdbc-url}")
private String jdbcUrl;
@Value("${hikari.primary.username}")
private String username;
@Value("${hikari.primary.password}")
private String password;
@Value("${hikari.primary.driver-class-name}")
private String driverClassName;
@Bean(name = {"primaryDataSource"})
@Primary
public HikariDataSource dataSource() {
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setUsername(username);
hikariDataSource.setPassword(password);
hikariDataSource.setDriverClassName(driverClassName);
return hikariDataSource;
}
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(this.dataSource());
}
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory sqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mapper/sourcedata/*Mapper.xml"));
return sessionFactory.getObject();
}
}
SecondDataSource.java
package com.tl.hello.spring.boot.mybatis.hikari.config;
import com.zaxxer.hikari.HikariDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* @author tianl
* @date 2020/3/27 22:53
*/
@Configuration
@MapperScan(basePackages = {"com.tl.hello.spring.boot.mybatis.druid.dao.second"},sqlSessionFactoryRef = "secondSqlSessionFactory")
@PropertySource("classpath:config/datasource.properties")
public class SecondDataSourceConfig {
@Value("${hikari.second.jdbc-url}")
private String jdbcUrl;
@Value("${hikari.second.username}")
private String username;
@Value("${hikari.second.password}")
private String password;
@Value("${hikari.second.driver-class-name}")
private String driverClassName;
@Bean(name = "secondDataSource")
public HikariDataSource dataSource(){
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setUsername(username);
hikariDataSource.setPassword(password);
hikariDataSource.setDriverClassName(driverClassName);
return hikariDataSource;
}
@Bean(name = "secondTransactionManager")
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(this.dataSource());
}
@Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory sqlSessionFactory(@Qualifier("secondDataSource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources("classpath:mapper/targetdata/*Mapper.xml"));
return sessionFactory.getObject();
}
}
测试:启动一个定时任务同步连个数据中数据
package com.tl.hello.spring.boot.mybatis.hikari.service;
import com.tl.hello.spring.boot.mybatis.hikari.model.primary.PrimaryUser;
import com.tl.hello.spring.boot.mybatis.hikari.model.second.SecondUser;
import com.tl.hello.spring.boot.mybatis.hikari.service.primary.PrimaryUserService;
import com.tl.hello.spring.boot.mybatis.hikari.service.second.SecondUserService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
/**
* @author tianl
* @date 2020/3/28 0:51
*/
@Service
public class SyncDataService {
@Resource
private PrimaryUserService primaryUserService;
@Resource
private SecondUserService secondUserService;
@Scheduled(cron = "0/5 * * * * ?")
public void sync(){
PrimaryUser primaryUser = primaryUserService.selectByPrimaryKey(19L);
SecondUser secondUser=new SecondUser();
secondUser.setAge(primaryUser.getAge());
secondUser.setName(primaryUser.getName());
secondUser.setUsername(primaryUser.getUsername());
secondUser.setPassword(primaryUser.getPassword());
secondUserService.insertOrUpdate(secondUser);
}
}
代码完成地址:[码云]
hikari数据源配置类_SpringBoot2集成Mybatis Hikari多数据源配置相关推荐
- hikari数据源配置类_spring-boot2项目默认hikari数据源的配置
spring-boot2中默认使用hikari连接池管理数据源链接.下面列出了项目中配置的内容,也是项目中常用的配置项,基本就够用了. 首先配置数据源: # spring datasource配置 s ...
- Spring Boot 集成 Mybatis 实现双数据源
转载自 Spring Boot 集成 Mybatis 实现双数据源 这里用到了Spring Boot + Mybatis + DynamicDataSource配置动态双数据源,可以动态切换数据源 ...
- SpringBoot集成Mybatis动态多数据源后,MybatisPlus的IPage失效的问题解决方案
背景 之前做数据抽取的时候,搭了一个mybatis动态数据源切换的架子.方便他们写抽取的代码.今天同事问我,架子里面的mybatisplus的IPage失效了是什么问题.想了一下,应该是写动态数据源的 ...
- springboot 集成mybatis_Spring Boot 集成Mybatis实现多数据源
静态的方式 我们以两套配置方式为例,在项目中有两套配置文件,两套mapper,两套SqlSessionFactory,各自处理各自的业务,这个两套mapper都可以进行增删改查的操作,在这两个主MYS ...
- 玩转 SpringBoot2.x 之自定义配置类整合Druid(Mybatis版)
专题系列分类:玩转SpringBoot2.x系列教程 前言 在阅读前这篇博客之前请先移步 玩转 SpringBoot 2.x 整合 Mybatis因为我们这篇博客是在其基础之上进行讲解的.在玩转 Sp ...
- Spring Boot 集成Mybatis实现多数据源
项目提交测试,趁着中当间的这个空档期,把springboot的多数据源配置学习一下,总体来说多数据源配置有两种方式,一种是静态的,一种是动态的. 静态的方式 我们以两套配置方式为例,在项目中有两套配置 ...
- @scheduled注解配置时间_SpringBoot2.0实战(32)配置定时任务
定时任务的几种实现方式: Timer:Java自带的java.util.Timer类,这个类允许你调度一个java.util.TimerTask任务.使用这种方式可以让你的程序按照某一个频度执行,但不 ...
- ssm注解配置连接mysql_基于注解和配置类的SSM(Spring+SpringMVC+Mybatis)项目详细配置...
在上一篇文章中介绍了使用注解和xml配置文件对项目进行配置,在这篇文章中将xml配置文件中的配置信息都改成使用注解或者配置类的形式. 第一步.配置pom.xml 在一个ssm项目中,可能需要用到的依赖 ...
- SpringBoot2.1 - Quartz自动化配置集成 yml方式配置免配置类和properties
首先非常感谢原作者恒宇少年 ,写了一遍好文章.找了很多文章终于找到比较有用有价值的高质量博文. 原贴地址:https://www.jianshu.com/p/056281e057b3 以下引用原文章. ...
最新文章
- 50行Python代码,获取公众号全部文章
- 用python爬取一个人所有信息_python实战===爬取所有微信好友的信息
- ajax async:false不管用_js 网络请求框架 ajax和axios、fetch的区别
- activity 生命周期_死磕Android_App 启动过程(含 Activity 启动过程)
- 【综述】深度长尾学习
- python和perl哪个好_做为脚本语言来说perl和python那个更有优势?
- 前端 - token 是什么?为什么每次请求头(HEADS)里要携带它?___请求时,为什么要携带token?
- Flutter 是移动应用程序开发的未来?
- Centos开机自动执行shell脚本启动tomcat服务器
- UbuntuSkills
- Permute 3 for mac(全能媒体格式转换器)
- 激活出现 错误0x800706F7 占位程序接收到错误数据
- html怎么插入word文档,word中怎样插入html代码?
- xp服务器文件写保护怎么删除,Xp系统磁盘被写保护无法复制文件解决方法
- 修改mtk平台power按键的gpio控制口
- WPF渲染事件CompositionTarget.Rendering
- lucene java 庖丁解牛_Lucene分词器之庖丁解牛
- 一 、Single Threaded Execution 模式
- hexo下next主题的优化
- 量子力学顺口溜_数学的幽默打油诗