Springboot+mybatis双数据源(Druid和jdbc)
Posted 胡乐天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springboot+mybatis双数据源(Druid和jdbc)相关的知识,希望对你有一定的参考价值。
项目包结构
POM文件
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--这是第三方整合的启动包,在spring-boot-dependencies中没有版本管理,所以需要自己声明版本号-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
配置文件:db1位Druid,db2位jdbc连接。具体要看配置类
server:
port: 8090
spring:
application:
name: bus
datasource:
db1:
#Druid
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://132mysql.rds.aliyuncs.com:3306/rydemo?useSSL=false&serverTimezone=Asia/Shanghai
username: hult
password: ******
druid:
#
initial-size: 10
#
min-idle: 5
#
max-active: 20
#
max-wait: 600000
time-between-eviction-runs-millis: 60000
min-evictable-idle-time-millis: 300000
db2:
driver-class-name: com.mysql.cj.jdbc.Driver
jdbc-url: jdbc:mysql://132mysql.rds.aliyuncs.com:3306/rydemo?useSSL=false&serverTimezone=Asia/Shanghai
username: hult
password: ******
配置类一(Druid)
package com.lt.bus.config;
import com.alibaba.druid.pool.DruidDataSource;
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.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.lt.bus.mapper.db1",sqlSessionTemplateRef = "db1SqlSessionTemplate")
public class DataSource1Config
// private static final String MAPPER_LOCAL = "classpath:mybatisxml/db1/TaskMapper.xml";
private static final String MAPPER_LOCAL = "classpath:mybatisxml/db1/*.xml";
@Bean
@Primary
public SqlSessionTemplate db1SqlSessionTemplate(@Qualifier("db1SqlSessionFactory")SqlSessionFactory sqlSessionFactory)
return new SqlSessionTemplate(sqlSessionFactory);
@Bean
@Primary
public DataSourceTransactionManager db1TransactionManager(@Qualifier("db1DataSource") DataSource dataSource)
return new DataSourceTransactionManager(dataSource);
@Bean
@Primary
public SqlSessionFactory db1SqlSessionFactory(@Qualifier("db1DataSource") DataSource dataSource) throws Exception
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCAL));
bean.setDataSource(dataSource);
//别名
bean.setTypeAliasesPackage("com.lt.bus.pojo");
//自动驼峰
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
return bean.getObject();
@Bean
@ConfigurationProperties(prefix = "spring.datasource.db1")
@Primary
public DataSource db1DataSource()
// DataSourceBuilder.create().build():JDBC数据源
// return DataSourceBuilder.create().build();
return new DruidDataSource();
配置类二(JDBC)
package com.lt.bus.config;
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.lt.bus.mapper.db2",sqlSessionTemplateRef = "db2SqlSessionTemplate")
public class DataSource2Config
// private static final String MAPPER_LOCAL = "classpath:mybatisxml/db2/RptInstanceMapper.xml";
private static final String MAPPER_LOCAL = "classpath:mybatisxml/db2/*.xml";
@Bean
public SqlSessionTemplate db2SqlSessionTemplate(@Qualifier("db2SqlSessionFactory")SqlSessionFactory sqlSessionFactory)
return new SqlSessionTemplate(sqlSessionFactory);
@Bean
public DataSourceTransactionManager db2TransactionManager(@Qualifier("db2DataSource") DataSource dataSource)
return new DataSourceTransactionManager(dataSource);
@Bean
public SqlSessionFactory db2SqlSessionFactory(@Qualifier("db2DataSource") DataSource dataSource) throws Exception
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(MAPPER_LOCAL));
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("com.lt.bus.pojo");
//自动驼峰
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setMapUnderscoreToCamelCase(true);
bean.setConfiguration(configuration);
return bean.getObject();
@Bean
@ConfigurationProperties(prefix = "spring.datasource.db2")
public DataSource db2DataSource()
return DataSourceBuilder.create().build();
剩下的mapper和mapperxml文件正常写到对应目录即可。
xml文件(此文件和正常没啥区别,记录再此是可以拷贝dtd)
<?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.lt.bus.mapper.db1.TaskMapper">
<select id="selectAllUnExeTask" resultType="task">
select * from ed_task_exec_rec where exec_type='定时' and res is null
</select>
<update id="updateExecIng" parameterType="task">
update ed_task_exec_rec set res='正在执行',exec_start_time=NOW() where task_id in
<foreach collection="collection" item="task" open="(" separator="," close=")">
#task.taskId
</foreach>
</update>
<update id="updateResult">
update ed_task_exec_rec set res=#res,exec_end_time=now(),res_msg=#message where task_id=#taskId
</update>
</mapper>
以上是关于Springboot+mybatis双数据源(Druid和jdbc)的主要内容,如果未能解决你的问题,请参考以下文章
Springboot+mybatis双数据源(Druid和jdbc)
Spring Boot 双数据源Mybatis+MongoDB配置
springMvc+Mybatis多数据源配置报错 Invalid bound statement
Spring Boot 2.0.4整合Spring Data JPA和Druid,双数据源
SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源配置 MyBatis事务控制druid 监控)