mybatis配置mysql数据源_springboot+mybatis+Druid配置多数据源(mysql+postgre)
springboot+mybatis+Druid配置多数据源(mysql+postgre)
引入pom依赖
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
com.alibaba
druid-spring-boot-starter
1.1.9
org.springframework.boot
spring-boot-starter-jdbc
mysql
mysql-connector-java
runtime
org.postgresql
postgresql
设置application多数据源
只是在原有的数据库配置外层添加了db1,db2,用来区分数据库;
注意:url使用的是jdbc的jdbc-url
durid无需改变
spring:
datasource:
db1: #配置数据源1:此处配置的mysql数据库
driverClassName: com.mysql.jdbc.Driver
username: root
password: 123456
# spring2.0此处为jdbc-url
jdbc-url: jdbc:mysql://127.0.0.1:3306/aa?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
type: com.alibaba.druid.pool.DruidDataSource
db2: #配置数据源2,此处配置的是postgre数据库
jdbc-url: jdbc:postgresql://127.0.0.1:5432/test1
username: postgres
password: 123456
driverClassName: org.postgresql.Driver
# platform: postgres
type: com.alibaba.druid.pool.DruidDataSource
initialization-mode: always
# ============================== druid ============================== #
druid:
#最大活跃数
maxActive: 20
#初始化数量
initialSize: 1
#最大连接等待超时时间
maxWait: 60000
#打开PSCache,并且指定每个连接PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
#通过connectionProperties属性来打开mergeSql功能;慢SQL记录
#connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 1 from dual
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
#配置监控统计拦截的filters,去掉后监控界面sql将无法统计,‘wall‘用于防火墙
filters: stat, wall, log4j
config配置
注意:两个数据库的xml文件和mapper文件需要分开放置
db1config配置(主数据库配置)
注意:db1设置为了主数据库,则需要配置注解@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;
/**主数据源mysql配置(需要加@Primary)
* mysql data source config
*
* @author 荆世玉
* @date 2019/2/26 9:07:16
* @description postgresql data source config
*/
@Configuration
//注入mapper
@MapperScan(basePackages = "net.cc.commons.mapper.mysql.**", sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class DataSource1Config {
@Bean
@Primary
@ConfigurationProperties(prefix = "spring.datasource.db1")//设置配置
public DataSource db1DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
@Primary
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
//设置对应的xml文件位置
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/mysql/*.xml"));
return bean.getObject();
}
@Bean
@Primary
public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
@Primary
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
db2config配置(其他数据库)
注意:
1.虽然数据库不同,但是配置相同,只是其他数据库不需要添加@Primary注解;
2.bean的名字不能重复
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;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
/**
* postgresql 配置(无需@Primary)
*
* @author 荆世玉
* @date 2019/2/26 9:07:16
* @description postgresql data source config
*/
@Configuration
//注入mapper
@MapperScan(basePackages = "net.cc.commons.mapper.postgre.**", sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DataSource2Config {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db2DataSource() {
return DataSourceBuilder.create().build();
}
@Bean
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mappers/postgre/*.xml"));
return bean.getObject();
}
@Bean
public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2DataSource") DataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
@Bean
public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
return new SqlSessionTemplate(sqlSessionFactory);
}
}
事务处理
注入事务的时候需要标明对应数据库(主数据库可以不用设置)
如:db2的数据库的事务"
@Transactional(value = "db2TransactionManager")
mapper层
不同数据库的mapper文件不能载同一个包中,config在注入时会注入错误;
使用时,与单数据源时一致
@Select("select * from table_1 ")
public List all();
public List all2();
select * from table_1
[{"id1":1,"id2":23,"id3":2222},{"id1":2,"id2":2222,"id3":111}]
mybatis配置mysql数据源_springboot+mybatis+Druid配置多数据源(mysql+postgre)相关推荐
- druid 多数据源_SpringBoot+Mybatis+Druid+PageHelper 实现多数据源并分页
今天跟大家分享常用的SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页的知识. 1 SpringBoot+Mybatis+ Druid+PageHelper ...
- druid 多数据源_SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页
作者: 虚无境http://cnblogs.com/xuwujing/p/8964927.html 前言 本篇文章主要讲述的是SpringBoot整合Mybatis.Druid和PageHelper ...
- springboot pom文件添加mysql组件_SpringBoot+Mybatis 通过databaseIdProvider支持多数据库
概述 本人最近接到一个任务,将一个系统改成同时兼容Oracle和PostgreSQL(原来是仅支持Oracle).虽然大部分的sql语句通用,但是还有许多语法存在差异,所以我们可以通过mybatis自 ...
- java实体类生成mysql表_springboot+mybatis通过实体类自动生成数据库表的方法
前言 本章介绍使用mybatis结合mysql数据库自动根据实体类生成相关的数据库表. 首先引入相关的pom包我这里使用的是springboot2.1.8.RELEASE的版本 org.mybatis ...
- spring mysql整合_springboot mybatis mysql 整合
1.pom文件配置 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test ...
- mysql jdbc 多数据源_springboot jdbc连接多个数据源
依赖 注:github中源码依赖是精简后的结果. org.springframework.boot spring-boot-starter-web org.springframework.boot s ...
- jpa 连接多个mysql 数据库_springboot环境jpa配置多个数据源流程
一 简介 开发过程中,我们都需要和数据库打交道,小项目一个数据库就可以,代码也比较简单,只要连接一个数据库,本文中,由于业务需求,我们需要连接另一个库去查询数据,因此要对旧版本(只实现连接一个库的jp ...
- hibernate整合mysql配置文件_springboot整合hibernate配置
添加sprngboot依赖<?xml version="1.0" encoding="UTF-8"?> xmlns:xsi="http: ...
- mysql druid 多数据源_SpringBoot使用阿里数据库连接池Druid以及多数据源配置
SpringBoot使用阿里数据库连接池Druid以及多数据源配置 引入依赖 com.alibaba druid 1.0.29 数据源配置 1.数据源 #master数据源 spring.dataso ...
最新文章
- linux服务器centos系统apache路径不区分大小写的解决办法(适用WDCP面板)
- (转载)简单linux C++内存池
- Musical Theme
- Qt:OpenCV—Q图像处理基本操作(Code)
- mongodb web_MongoDB和Web应用程序
- SpringBoot配置多Redis数据源
- Dolibarr可以做什么
- 3个方法教你怎么避免拼多多比价订单
- 二. Binding 详解
- 文化袁探索专栏——Activity、Window和View三者间关系
- 你知道数据在内存中是如何存储的嘛?
- html分列代码,C#/VB.NET Excel数据分列(示例代码)
- sau交流学习社区--看小说的lovebook一个无线端BS应用
- ImageIO javax.imageio;读取图片问题
- 使用MIC进行变量选择
- 实验一 验证74LS181运算和逻辑功能
- delphi7 获取计算机名,Delphi中根据IP地址得到主机名
- html touch ui,零React基础入门Amaze UI Touch!
- Symbian OS内存泄漏
- 直击大数据业务痛点:浪潮整合优势推出云海大数据一体机