本章介绍SpringBoot 与 MyBatis 多数据源配置,SpringBoot与Mybatis整合内容可以参考【SpringBoot】SpringBoot 与Mybatis整合(十三)
1、准备2个数据库,本例以mysql为例
在第一个数据库test_mysql中,新建表user
1 -- ---------------------------- 2 -- Table structure for user 3 -- ---------------------------- 4 DROP TABLE IF EXISTS `user`; 5 CREATE TABLE `user` ( 6 `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘ID‘, 7 `name` varchar(32) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘名称‘, 8 PRIMARY KEY (`id`) 9 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 10 11 -- ---------------------------- 12 -- Records of user 13 -- ---------------------------- 14 BEGIN; 15 INSERT INTO `user` VALUES (1, ‘张三‘); 16 INSERT INTO `user` VALUES (2, ‘李四‘); 17 INSERT INTO `user` VALUES (3, ‘王五‘); 18 COMMIT;
在第二个数据库test_mysql2中,新建表dog
1 -- ---------------------------- 2 -- Table structure for dog 3 -- ---------------------------- 4 DROP TABLE IF EXISTS `dog`; 5 CREATE TABLE `dog` ( 6 `id` int(11) NOT NULL AUTO_INCREMENT COMMENT ‘ID‘, 7 `dog_name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT ‘狗名‘, 8 PRIMARY KEY (`id`) 9 ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; 10 11 -- ---------------------------- 12 -- Records of dog 13 -- ---------------------------- 14 BEGIN; 15 INSERT INTO `dog` VALUES (1, ‘旺财‘); 16 INSERT INTO `dog` VALUES (2, ‘二哈‘); 17 INSERT INTO `dog` VALUES (3, ‘大黑‘); 18 COMMIT;
1、新建一个SpringBoot Web项目,完成pom文件如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.test</groupId> 8 <artifactId>test-springboot-muldatasource</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <parent> 12 <groupId>org.springframework.boot</groupId> 13 <artifactId>spring-boot-starter-parent</artifactId> 14 <version>2.1.8.RELEASE</version> 15 </parent> 16 17 <properties> 18 19 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 20 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 21 <java.version>1.8</java.version> 22 </properties> 23 24 <dependencies> 25 26 <dependency> 27 <groupId>org.springframework.boot</groupId> 28 <artifactId>spring-boot-starter-web</artifactId> 29 </dependency> 30 31 <dependency> 32 <groupId>org.mybatis.spring.boot</groupId> 33 <artifactId>mybatis-spring-boot-starter</artifactId> 34 <version>2.0.1</version> 35 </dependency> 36 37 <!-- mysql --> 38 <dependency> 39 <groupId>mysql</groupId> 40 <artifactId>mysql-connector-java</artifactId> 41 <version>8.0.12</version> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-test</artifactId> 47 <scope>test</scope> 48 </dependency> 49 50 </dependencies> 51 52 53 <!-- SpringBoot打包插件,可以将代码打包成一个可执行的jar包 --> 54 <build> 55 <plugins> 56 <plugin> 57 <groupId>org.springframework.boot</groupId> 58 <artifactId>spring-boot-maven-plugin</artifactId> 59 </plugin> 60 </plugins> 61 </build> 62 63 </project>
2、配置文件application.yml
3、
【SpringBoot】SpringBoot 与 MyBatis 多数据源配置(二十九)
原文:https://www.cnblogs.com/h--d/p/12521577.html