项目结构

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);
}
}

最新文章

  1. hdu 2191 珍惜现在,感恩生活
  2. JAVA对MySQL数据库的操作
  3. CR LF的由来
  4. 【读书笔记】iOS网络-保护网络传输
  5. ubuntu14安装Qt
  6. 开机SystemServer到ActivityManagerService启动过程分析
  7. Golang tips ----- 函数
  8. Android中的PopupWindow详解
  9. 网站压力测试工具webbench 安装与使用
  10. gulp编译less简单demo
  11. 关于PEER - PEER毅恒挚友 - Powered by Discuz!
  12. flume-sink报错 java.lang.IllegalStateException: close() called when transaction is OPEN - you must either commit or rollback first
  13. Mysql免安装版配置【图文版和文字版】
  14. Asp.net SignalR 让实时通讯变得简单
  15. pyDash:一个基于 web 的 Linux 性能监测工具
  16. June 3. 2018 Week 23rd Sunday
  17. Tinkoff Challenge - Final Round (Codeforces Round #414, rated, Div. 1 + Div. 2) 【ABC】
  18. twisted之Deferred类的分析
  19. LeetCode-Algorithms 1. 两数之和
  20. 条件注释判断浏览器<!--[if !IE]>

热门文章

  1. PHP学习之图像处理-水印类
  2. mac-chrome下手动安装vue-devtools
  3. C# 去除所有的html标签
  4. BUUCTF-writeup
  5. 21 Flutter仿京东商城项目 商品详情 请求接口渲染数据 商品属性数据渲染
  6. (翻译) Poor misunderstood 'var'
  7. 分布式存储——Build up a High Availability Distributed Key-Value Store
  8. idea控制台中文乱码“淇℃伅”
  9. 敏捷管理的大概背景和Scrum的特性
  10. Flutter打包release版本安卓apk包真机安装无法请求网络的解决方法