Springboot+mybatis+druid 配置多数据源
项目结构

application.yml配置文件
spring:
application:
name: service
datasource:
primary:
jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username: gkh
password: 123456
driver-class-name: oracle.jdbc.driver.OracleDriver
type: com.alibaba.druid.pool.DruidDataSource #使用druid连接池
#url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
#type: oracle.jdbc.pool.OracleDataSource
secondary:
jdbc-url: jdbc:oracle:thin:@127.0.0.1:1521:ORCL
username: gkh
password: 123456
driver-class-name: oracle.jdbc.driver.OracleDriver
type: com.alibaba.druid.pool.DruidDataSource #使用druid连接池
主数据源配置代码
package com.gkh.springboot.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.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; /**
* @Primary:指定为默认数据源,没有该注解会报错,系统找不到默认数据源
* @MapperScan:扫描指定包的mapper作为对应数据源,建议每个数据源都用不同的包区分
* @Qualifier:与@Autowired类似,用作区分如果存在多个实现类要指定要注入哪个 参数为指定Bean的name
*/ @Configuration
@MapperScan(basePackages = "com.gkh.springboot.mapper.primary", sqlSessionFactoryRef = "primarySqlSessionFactory")
public class DataSource1Config { /**
* 生成数据源,@Primary注解声明为默认数据源
* @return
*/
@Bean(name="primaryDataSoure")
@Primary
@ConfigurationProperties(prefix = "spring.datasource.primary")
public DataSource primaryDataSource(){
return DataSourceBuilder.create().build();
} /**
* 创建sqlSessionFactory
* @param datasource
* @return
* @throws Exception
*/
@Bean(name = "primarySqlSessionFactory")
@Primary
public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSoure") DataSource datasource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(datasource);
return bean.getObject();
} /**
* 配置事务管理
* @param datasource
* @return
*/
@Bean(name = "primaryTransactionManager")
@Primary
public DataSourceTransactionManager primaryTransactionManager(@Qualifier("primaryDataSoure") DataSource datasource){
return new DataSourceTransactionManager(datasource);
} @Bean(name = "primarySqlSessionTemplate")
@Primary
public SqlSessionTemplate primarySqlSessionTemplate(@Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
第二个数据源代码
package com.gkh.springboot.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.jdbc.datasource.DataSourceTransactionManager; import javax.sql.DataSource; @Configuration
@MapperScan(basePackages = "com.gkh.springboot.mapper.secondary", sqlSessionFactoryRef = "secondSqlSessionFactory")
public class DataSource2Config { @Bean(name = "secondDatasource")
@ConfigurationProperties(prefix = "spring.datasource.secondary")
public DataSource secondDatasource(){
return DataSourceBuilder.create().build();
} @Bean(name = "secondSqlSessionFactory")
public SqlSessionFactory secondSqlSessionFactory(@Qualifier("secondDatasource") DataSource dataSource)
throws Exception{
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
return bean.getObject();
} @Bean(name = "secondTransactionManager")
public DataSourceTransactionManager sourceTransactionManager(@Qualifier("secondDatasource") DataSource dataSource){
return new DataSourceTransactionManager(dataSource);
} @Bean(name = "secondSqlSessionTemplate")
public SqlSessionTemplate secondSqlSessionTemplate(@Qualifier("secondSqlSessionFactory") SqlSessionFactory sqlSessionFactory){
return new SqlSessionTemplate(sqlSessionFactory);
}
}
Controller:
UserController
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService; /**
* 通过主键id查询
* @param id
* @return
*/
@GetMapping(value = "/getUser")
@ResponseBody
public User getUserById(@RequestParam("id") Long id){
return this.userService.getUserById(id);
}
}
StudentController
@Controller
@RequestMapping(value = "/student")
public class StudentController { @Autowired
private StudentService studentService; @GetMapping(value = "/getStudent/{id}")
@ResponseBody
public Student getStudent(@PathVariable int id){
return studentService.selectByPrimaryKey(id);
}
}
service
UserServiceImpl
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper; @Override
public User getUserById(Long id) {
return this.userMapper.selectByPrimaryKey(id);
}
}
StudentServiceImpl
@Service
public class StudentServiceImpl implements StudentService { @Autowired
StudentMapper studentMapper; @Override
public Student selectByPrimaryKey(int id) {
return studentMapper.selectByPrimaryKey(id);
}
}
Springboot+mybatis+druid 配置多数据源的更多相关文章
- springboot 2.1.3 + mybatis + druid配置多数据源
在一些大型的项目中,通常会选择多数据库来满足一些业务需求,此处讲解使用springboot.mybatis和druid来配置多数据源 1.依赖配置 pom文件引入相关依赖 <dependency ...
- 3分钟搞定SpringBoot+Mybatis+druid多数据源和分布式事务
文章来自: https://blog.csdn.net/qq_29242877/article/details/79033287 在一些复杂的应用开发中,一个应用可能会涉及到连接多个数据源,所谓多数据 ...
- SpringBoot整合MyBatisPlus配置动态数据源
目录 SpringBoot整合MyBatisPlus配置动态数据源 SpringBoot整合MyBatisPlus配置动态数据源 推文:2018开源中国最受欢迎的中国软件MyBatis-Plus My ...
- springboot+mybatis+druid+atomikos框架搭建及测试
前言 因为最近公司项目升级,需要将外网数据库的信息导入到内网数据库内.于是找了一些springboot多数据源的文章来看,同时也亲自动手实践.可是过程中也踩了不少的坑,主要原因是我看的文章大部分都是s ...
- springboot+mybatis+druid+sqlite/mysql/oracle
搭建springboot+mybatis+druid+sqlite/mysql/oracle附带测试 1.版本 springboot2.1.6 jdk1.8 2.最简springboot环境 http ...
- 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建
基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...
- JNDI学习总结(三)——Tomcat下使用Druid配置JNDI数据源
com.alibaba.druid.pool.DruidDataSourceFactory实现了javax.naming.spi.ObjectFactory,可以作为JNDI数据源来配置. 一.下载D ...
- Tomcat下使用Druid配置JNDI数据源
com.alibaba.druid.pool.DruidDataSourceFactory实现了javax.naming.spi.ObjectFactory,可以作为JNDI数据源来配置. 一.下载D ...
- JNDI学习总结(4)——Tomcat下使用Druid配置JNDI数据源
com.alibaba.druid.pool.DruidDataSourceFactory实现了javax.naming.spi.ObjectFactory,可以作为JNDI数据源来配置. 一.下载D ...
随机推荐
- mysql 给用户设置权限
grant all on wordpress.* to wordpress@'10.0.0.%' identified by 'wordpress'; all 全部权限 ...
- [go]ini配置文件解析
// config.ini [app] server.port = 8080 name = resk enabled = false time = 10s ;我是一个注释 #mysql数据库配置 [m ...
- create-react-app 工程,如何修改react端口号?
概要: 3000端口是webpack配置里面写的,可以通过传递一个PORT全局变量,来修改这个端口.当然,您还可以在node_modules/react-scripts/目录下面,批量搜索替换3000 ...
- BitmapDrawable
对Bitmap的一种封装,可以设置它包装的bitmap在BitmapDrawable区域中的绘制方式,有: 平铺填充,拉伸填或保持图片原始大小!以<bitmap>为根节点! 可选属性如下: ...
- tortoiseGit did not exit cleanly (exit code 128)
安装并配置好tortoiseGit之后,clone项目时,报错: git did not exit cleanly (exit code 128)如下图: 该问题解决方式: 1.确保Pageant启动 ...
- laravel中的管道设计模式
转自 http://laravelacademy.org/post/3088.html
- ID3算法(MATLAB)
ID3算法是一种贪心算法,用来构造决策树.ID3算法起源于概念学习系统(CLS),以信息熵的下降速度为选取测试属性的标准,即在每个节点选取还尚未被用来划分的具有最高信息增益的属性作为划分标准,然后继续 ...
- Path环境变量的作用
作用: 当我们要求系统运行一个程序(例如a.exe)而没有告诉它程序所在的完整路径时,系统会先在当前目录寻找是否存在a.exe,如果找到,直接运行:如果没有找到,会去path路径下面找.设置path, ...
- 物联网安全himqtt防火墙数据结构之ringbuffer环形缓冲区
物联网安全himqtt防火墙数据结构之ringbuffer环形缓冲区 随着5G的普及,物联网安全显得特别重要,himqtt是首款完整源码的高性能MQTT物联网防火墙 - MQTT Applicatio ...
- 通过IP得到IP所在地省市
/// <summary> /// 通过IP得到IP所在地省市(Porschev) /// </summary> /// <param name="ip&quo ...