依賴 pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- oracle驅(qū)動 -->
<!-- 可能在maven倉庫中找不到翎承,需要下載jar包,加載進(jìn)本地倉庫 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>6.0</version>
</dependency>
<!-- mysql驅(qū)動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid數(shù)據(jù)源驅(qū)動 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.6</version>
</dependency>
配置文件application.yml
server:
context-path: /web
spring:
datasource:
druid:
# 數(shù)據(jù)庫訪問配置, 使用druid數(shù)據(jù)源
# 數(shù)據(jù)源1 mysql
mysql:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://10.0.0.3:3306/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull
username: root
password: root
# 數(shù)據(jù)源2 oracle
oracle:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:ORCL
username: c##abc
password: 123456
# 連接池配置
initial-size: 5
min-idle: 5
max-active: 20
# 連接等待超時時間
max-wait: 30000
# 配置檢測可以關(guān)閉的空閑連接間隔時間
time-between-eviction-runs-millis: 60000
# 配置連接在池中的最小生存時間
min-evictable-idle-time-millis: 300000
validation-query: select '1' from dual
test-while-idle: true
test-on-borrow: false
test-on-return: false
# 打開PSCache料滥,并且指定每個連接上PSCache的大小
pool-prepared-statements: true
max-open-prepared-statements: 20
max-pool-prepared-statement-per-connection-size: 20
# 配置監(jiān)控統(tǒng)計攔截的filters, 去掉后監(jiān)控界面sql無法統(tǒng)計, 'wall'用于防火墻
filters: stat,wall
# Spring監(jiān)控AOP切入點开财,如x.y.z.service.*,配置多個英文逗號分隔
aop-patterns: com.springboot.servie.*
# WebStatFilter配置
web-stat-filter:
enabled: true
# 添加過濾規(guī)則
url-pattern: /*
# 忽略過濾的格式
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
# StatViewServlet配置
stat-view-servlet:
enabled: true
# 訪問路徑為/druid時,跳轉(zhuǎn)到StatViewServlet
url-pattern: /druid/*
# 是否能夠重置數(shù)據(jù)
reset-enable: false
# 需要賬號密碼才能訪問控制臺
login-username: druid
login-password: druid123
# IP白名單
# allow: 127.0.0.1
# IP黑名單(共同存在時,deny優(yōu)先于allow)
# deny: 192.168.1.218
# 配置StatFilter
filter:
stat:
log-slow-sql: true
配置多數(shù)據(jù)源
mysql:
MysqlDatasourceConfig.class
Configuration
// 指定掃描的mapper包
@MapperScan(basePackages = "com.springboot.mysqlDao", sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MysqlDatasourceConfig {
// mybatis mapper掃描路徑
private static final String MAPPER_LOCATION = "classpath:mapper/mysql/*.xml";
@Primary
@Bean(name = "mysqldatasource")
// 使用yml里的配置配置數(shù)據(jù)源builder
@ConfigurationProperties("spring.datasource.druid.mysql")
public DataSource mysqlDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "mysqlTransactionManager")
@Primary
public DataSourceTransactionManager mysqlTransactionManager() {
return new DataSourceTransactionManager(mysqlDataSource());
}
@Bean(name = "mysqlSqlSessionFactory")
@Primary
public SqlSessionFactory mysqlSqlSessionFactory(@Qualifier("mysqldatasource") DataSource dataSource)
throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//如果不使用xml的方式配置mapper,則可以省去下面這行mapper location的配置泪勒。
sessionFactory.setMapperLocations(
new PathMatchingResourcePatternResolver().getResources(MysqlDatasourceConfig.MAPPER_LOCATION));
return sessionFactory.getObject();
}
}
oracle:
OracleDatasourceConfig.class
@Configuration
@MapperScan(basePackages = "com.springboot.oracleDao",
sqlSessionFactoryRef = "oracleSqlSessionFactory")
public class OracleDatasourceConfig {
// mybatis mapper掃描路徑
static final String MAPPER_LOCATION = "classpath:mapper/oracle/*.xml";
@Bean(name = "oracledatasource")
@ConfigurationProperties("spring.datasource.druid.oracle")
public DataSource oracleDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean(name = "oracleTransactionManager")
public DataSourceTransactionManager oracleTransactionManager() {
return new DataSourceTransactionManager(oracleDataSource());
}
@Bean(name = "oracleSqlSessionFactory")
public SqlSessionFactory oracleSqlSessionFactory(@Qualifier("oracledatasource") DataSource dataSource) throws Exception {
final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
//如果不使用xml的方式配置mapper,則可以省去下面這行mapper location的配置减途。
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver()
.getResources("classpath:mapper/oracle/*.xml"));
return sessionFactory.getObject();
}
}
mapper文件
mysql:MysqlStudentMapper.class
package com.springboot.mysqlDao;
@Mapper
public interface MysqlStudentMapper {
List<Map<String, Object>> getAllStudents();
}
oracle: OracleStudentMapper.class
package com.springboot.oracleDao;
@Mapper
public interface OracleStudentMapper {
List<Map<String, Object>> getAllStudents();
}
service調(diào)用:
@Service
public class StudentServiceImp implements StudentService{
@Autowired
private OracleStudentMapper oracleStudentMapper;
@Autowired
private MysqlStudentMapper mysqlStudentMapper;
@Override
public List<Map<String, Object>> getAllStudentsFromOralce() {
return this.oracleStudentMapper.getAllStudents();
}
@Override
public List<Map<String, Object>> getAllStudentsFromMysql() {
return this.mysqlStudentMapper.getAllStudents();
}
}