Spring Boot MyBatis配置Druid多数据源
This commit is contained in:
parent
27400f6d64
commit
f9a23be8ce
|
|
@ -0,0 +1,77 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.springboot</groupId>
|
||||
<artifactId>Spring-Boot-MyBatis-MultiDataSource</artifactId>
|
||||
<version>1.0-snapshot</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>demo</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.9.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.7</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<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驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.oracle</groupId>
|
||||
<artifactId>ojdbc6</artifactId>
|
||||
<version>6.0</version>
|
||||
</dependency>
|
||||
|
||||
<!-- mysql驱动 -->
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- druid数据源驱动 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid-spring-boot-starter</artifactId>
|
||||
<version>1.1.6</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
</project>
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package com.springboot;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class,args);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
package com.springboot.controller;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.springboot.service.StudentService;
|
||||
|
||||
@RestController
|
||||
public class StudentController {
|
||||
|
||||
@Autowired
|
||||
private StudentService studentService;
|
||||
|
||||
@RequestMapping("querystudentsfromoracle")
|
||||
public List<Map<String, Object>> queryStudentsFromOracle(){
|
||||
return this.studentService.getAllStudentsFromOralce();
|
||||
}
|
||||
|
||||
@RequestMapping("querystudentsfrommysql")
|
||||
public List<Map<String, Object>> queryStudentsFromMysql(){
|
||||
return this.studentService.getAllStudentsFromMysql();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
package com.springboot.datasource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
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 com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
|
||||
@Configuration
|
||||
@MapperScan(basePackages = MysqlDatasourceConfig.PACKAGE, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
|
||||
public class MysqlDatasourceConfig {
|
||||
|
||||
// mysqldao扫描路径
|
||||
static final String PACKAGE = "com.springboot.mysqldao";
|
||||
// mybatis mapper扫描路径
|
||||
static final String MAPPER_LOCATION = "classpath:mapper/mysql/*.xml";
|
||||
|
||||
@Primary
|
||||
@Bean(name = "mysqldatasource")
|
||||
@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();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
package com.springboot.datasource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.mybatis.spring.SqlSessionFactoryBean;
|
||||
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.core.io.support.PathMatchingResourcePatternResolver;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
|
||||
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
|
||||
|
||||
@Configuration
|
||||
@MapperScan(basePackages = OracleDatasourceConfig.PACKAGE,
|
||||
sqlSessionFactoryRef = "oracleSqlSessionFactory")
|
||||
public class OracleDatasourceConfig {
|
||||
|
||||
// oracledao扫描路径
|
||||
static final String PACKAGE = "com.springboot.oracledao";
|
||||
// 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(OracleDatasourceConfig.MAPPER_LOCATION));
|
||||
return sessionFactory.getObject();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.springboot.mysqldao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface MysqlStudentMapper {
|
||||
List<Map<String, Object>> getAllStudents();
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.springboot.oracledao;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
@Mapper
|
||||
public interface OracleStudentMapper {
|
||||
List<Map<String, Object>> getAllStudents();
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
package com.springboot.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface StudentService {
|
||||
List<Map<String, Object>> getAllStudentsFromOralce();
|
||||
List<Map<String, Object>> getAllStudentsFromMysql();
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
package com.springboot.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.springboot.mysqldao.MysqlStudentMapper;
|
||||
import com.springboot.oracledao.OracleStudentMapper;
|
||||
import com.springboot.service.StudentService;
|
||||
|
||||
@Service("studentService")
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
server:
|
||||
context-path: /web
|
||||
|
||||
spring:
|
||||
datasource:
|
||||
druid:
|
||||
# 数据库访问配置, 使用druid数据源
|
||||
# 数据源1 mysql
|
||||
mysql:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: com.mysql.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&autoReconnect=true&failOverReadOnly=false&zeroDateTimeBehavior=convertToNull
|
||||
username: root
|
||||
password: 123456
|
||||
# 数据源2 oracle
|
||||
oracle:
|
||||
type: com.alibaba.druid.pool.DruidDataSource
|
||||
driver-class-name: oracle.jdbc.driver.OracleDriver
|
||||
url: jdbc:oracle:thin:@localhost:1521:ORCL
|
||||
username: test
|
||||
password: 123456
|
||||
|
||||
# 连接池配置
|
||||
initial-size: 5
|
||||
min-idle: 5
|
||||
max-active: 20
|
||||
# 连接等待超时时间
|
||||
max-wait: 30000
|
||||
# 配置检测可以关闭的空闲连接间隔时间
|
||||
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
|
||||
# 配置监控统计拦截的filters, 去掉后监控界面sql无法统计, 'wall'用于防火墙
|
||||
filters: stat,wall
|
||||
# Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔
|
||||
aop-patterns: com.springboot.servie.*
|
||||
|
||||
|
||||
# WebStatFilter配置
|
||||
web-stat-filter:
|
||||
enabled: true
|
||||
# 添加过滤规则
|
||||
url-pattern: /*
|
||||
# 忽略过滤的格式
|
||||
exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
|
||||
|
||||
# StatViewServlet配置
|
||||
stat-view-servlet:
|
||||
enabled: true
|
||||
# 访问路径为/druid时,跳转到StatViewServlet
|
||||
url-pattern: /druid/*
|
||||
# 是否能够重置数据
|
||||
reset-enable: false
|
||||
# 需要账号密码才能访问控制台
|
||||
login-username: druid
|
||||
login-password: druid123
|
||||
# IP白名单
|
||||
# allow: 127.0.0.1
|
||||
# IP黑名单(共同存在时,deny优先于allow)
|
||||
# deny: 192.168.1.218
|
||||
|
||||
# 配置StatFilter
|
||||
filter:
|
||||
stat:
|
||||
log-slow-sql: true
|
||||
|
||||
|
||||
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<?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.springboot.mysqldao.MysqlStudentMapper">
|
||||
<select id="getAllStudents" resultType="java.util.Map">
|
||||
select * from student
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
<?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.springboot.oracledao.OracleStudentMapper">
|
||||
<select id="getAllStudents" resultType="java.util.Map">
|
||||
select * from student
|
||||
</select>
|
||||
</mapper>
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
Navicat MySQL Data Transfer
|
||||
|
||||
Source Server : localhost_3306
|
||||
Source Server Version : 50717
|
||||
Source Host : localhost:3306
|
||||
Source Database : test
|
||||
|
||||
Target Server Type : MYSQL
|
||||
Target Server Version : 50717
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 2018-05-02 14:32:22
|
||||
*/
|
||||
|
||||
SET FOREIGN_KEY_CHECKS=0;
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for student
|
||||
-- ----------------------------
|
||||
DROP TABLE IF EXISTS `student`;
|
||||
CREATE TABLE `student` (
|
||||
`SNO` varchar(3) NOT NULL,
|
||||
`SNAME` varchar(10) NOT NULL,
|
||||
`SSEX` char(2) NOT NULL,
|
||||
`DATASOURCE` varchar(10) DEFAULT NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of student
|
||||
-- ----------------------------
|
||||
INSERT INTO `student` VALUES ('001', 'KangKang', 'M', 'mysql');
|
||||
INSERT INTO `student` VALUES ('002', 'Mike', 'M', 'mysql');
|
||||
|
|
@ -0,0 +1,47 @@
|
|||
/*
|
||||
Navicat Oracle Data Transfer
|
||||
Oracle Client Version : 11.2.0.1.0
|
||||
|
||||
Source Server : localhost_test
|
||||
Source Server Version : 110200
|
||||
Source Host : localhost:1521
|
||||
Source Schema : TEST
|
||||
|
||||
Target Server Type : ORACLE
|
||||
Target Server Version : 110200
|
||||
File Encoding : 65001
|
||||
|
||||
Date: 2018-05-02 14:31:18
|
||||
*/
|
||||
|
||||
|
||||
-- ----------------------------
|
||||
-- Table structure for STUDENT
|
||||
-- ----------------------------
|
||||
DROP TABLE STUDENT;
|
||||
CREATE TABLE STUDENT (
|
||||
SNO VARCHAR2(3 BYTE) NOT NULL ,
|
||||
SNAME VARCHAR2(9 BYTE) NOT NULL ,
|
||||
SSEX CHAR(2 BYTE) NOT NULL ,
|
||||
DATASOURCE VARCHAR2(10 BYTE) NULL
|
||||
)
|
||||
LOGGING
|
||||
NOCOMPRESS
|
||||
NOCACHE
|
||||
|
||||
;
|
||||
|
||||
-- ----------------------------
|
||||
-- Records of STUDENT
|
||||
-- ----------------------------
|
||||
INSERT INTO STUDENT VALUES ('001', 'KangKang', 'M ', 'oracle');
|
||||
INSERT INTO STUDENT VALUES ('002', 'Mike', 'M ', 'oracle');
|
||||
INSERT INTO STUDENT VALUES ('003', 'Jane', 'F ', 'oracle');
|
||||
INSERT INTO STUDENT VALUES ('004', 'Maria', 'F ', 'oracle');
|
||||
|
||||
-- ----------------------------
|
||||
-- Checks structure for table STUDENT
|
||||
-- ----------------------------
|
||||
ALTER TABLE STUDENT ADD CHECK (SNO IS NOT NULL);
|
||||
ALTER TABLE STUDENT ADD CHECK (SNAME IS NOT NULL);
|
||||
ALTER TABLE STUDENT ADD CHECK (SSEX IS NOT NULL);
|
||||
Loading…
Reference in New Issue