diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/pom.xml b/5.Spring-Boot-MyBatis-MultiDataSource/pom.xml new file mode 100644 index 0000000..76420c4 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + + com.springboot + Spring-Boot-MyBatis-MultiDataSource + 1.0-snapshot + jar + + demo + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 1.5.9.RELEASE + + + + + UTF-8 + UTF-8 + 1.7 + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.1 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + com.oracle + ojdbc6 + 6.0 + + + + + mysql + mysql-connector-java + + + + + com.alibaba + druid-spring-boot-starter + 1.1.6 + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/Application.java b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/Application.java new file mode 100644 index 0000000..a66ea9f --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/Application.java @@ -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); + } +} diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/controller/StudentController.java b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/controller/StudentController.java new file mode 100644 index 0000000..ec319b4 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/controller/StudentController.java @@ -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> queryStudentsFromOracle(){ + return this.studentService.getAllStudentsFromOralce(); + } + + @RequestMapping("querystudentsfrommysql") + public List> queryStudentsFromMysql(){ + return this.studentService.getAllStudentsFromMysql(); + } +} diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/datasource/MysqlDatasourceConfig.java b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/datasource/MysqlDatasourceConfig.java new file mode 100644 index 0000000..a2cc707 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/datasource/MysqlDatasourceConfig.java @@ -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(); + } +} diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/datasource/OracleDatasourceConfig.java b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/datasource/OracleDatasourceConfig.java new file mode 100644 index 0000000..f7cab58 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/datasource/OracleDatasourceConfig.java @@ -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(); + } +} diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/mysqldao/MysqlStudentMapper.java b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/mysqldao/MysqlStudentMapper.java new file mode 100644 index 0000000..d87fd70 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/mysqldao/MysqlStudentMapper.java @@ -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> getAllStudents(); +} diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/oracledao/OracleStudentMapper.java b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/oracledao/OracleStudentMapper.java new file mode 100644 index 0000000..6ec0b32 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/oracledao/OracleStudentMapper.java @@ -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> getAllStudents(); +} diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/service/StudentService.java b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/service/StudentService.java new file mode 100644 index 0000000..0bb11e4 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/service/StudentService.java @@ -0,0 +1,9 @@ +package com.springboot.service; + +import java.util.List; +import java.util.Map; + +public interface StudentService { + List> getAllStudentsFromOralce(); + List> getAllStudentsFromMysql(); +} diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/service/impl/StudentServiceImp.java b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/service/impl/StudentServiceImp.java new file mode 100644 index 0000000..5b838fc --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/java/com/springboot/service/impl/StudentServiceImp.java @@ -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> getAllStudentsFromOralce() { + return this.oracleStudentMapper.getAllStudents(); + } + + @Override + public List> getAllStudentsFromMysql() { + return this.mysqlStudentMapper.getAllStudents(); + } + +} diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/application.yml b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/application.yml new file mode 100644 index 0000000..aac44a5 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/application.yml @@ -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 + + + \ No newline at end of file diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mapper/mysql/MysqlStudentMapper.xml b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mapper/mysql/MysqlStudentMapper.xml new file mode 100644 index 0000000..984f52f --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mapper/mysql/MysqlStudentMapper.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mapper/oracle/OracleStudentMapper.xml b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mapper/oracle/OracleStudentMapper.xml new file mode 100644 index 0000000..1d1f515 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mapper/oracle/OracleStudentMapper.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mysql_sql.sql b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mysql_sql.sql new file mode 100644 index 0000000..5ed9652 --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/mysql_sql.sql @@ -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'); diff --git a/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/oracle_sql.sql b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/oracle_sql.sql new file mode 100644 index 0000000..d59548b --- /dev/null +++ b/5.Spring-Boot-MyBatis-MultiDataSource/src/main/resources/oracle_sql.sql @@ -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);