Springboot+mybatis+druid 配置多数据源
2019-11-13 08:21:54来源:博客园 阅读 ()
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); } }
原文链接:https://www.cnblogs.com/gekh/p/11847643.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- springboot2配置JavaMelody与springMVC配置JavaMelody 2020-06-11
- Flink 如何分流数据 2020-06-11
- 数据源管理 | Kafka集群环境搭建,消息存储机制详解 2020-06-11
- nacos~配置中心功能~springboot的支持 2020-06-10
- 大公司都在做的大数据平台,为你精选这一份书单 2020-06-09
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash