教你使用springboot配置多数据源

论坛 期权论坛 脚本     
niminba   2021-5-26 12:31   3663   0

一、建库建表

1.1 创建数据库db1和数据库db2

在这里插入图片描述
在这里插入图片描述

1.2 在数据库db1中创建表db1

在这里插入图片描述

CREATE TABLE `db1` (
  `id` int unsigned zerofill NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `age` int unsigned zerofill DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

1.3 在数据库db2中创建表db2

在这里插入图片描述

CREATE TABLE `db2` (
  `id` int unsigned zerofill NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `age` int unsigned zerofill DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

二、创建springboot项目

2.1 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>2.1.4</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

2.2 创建application.yml文件(与 2.3 二选一进行配置,推荐此方法)

server:
  port: 8080 # 启动端口
spring:
  datasource:
    db1: # 数据源1
      jdbc-url: jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver
    db2: # 数据源2
      jdbc-url: jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
      username: root
      password: root
      driver-class-name: com.mysql.cj.jdbc.Driver

2.3 创建application.properties文件(与 2.2 二选一进行配置)

server.port=8080

      spring.datasource.db1.url=jdbc:mysql://localhost:3306/db1?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
      spring.datasource.db1.username=root
      spring.datasource.db1.password=root
      spring.datasource.db1.driver-class-name=com.mysql.jdbc.Driver

      spring.datasource.db2.url=jdbc:mysql://localhost:3306/db2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8
      spring.datasource.db2.username=root
      spring.datasource.db2.password=root
      spring.datasource.db2.driver-class-name=com.mysql.jdbc.Driver

2.4 创建mapper文件

我个人是放在mapper包下,文件随便命名的
代码随便写的,测试而已

在这里插入图片描述

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

/**
 * @Author if
 * @Description: What is it
 * @Date 2021-05-20 下午 09:52
 */
@Mapper
public interface Db1Mapper {
    @Insert("insert into db1(name,age) values('if',18)")
    int add();
}
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;

/**
 * @Author if
 * @Description: What is it
 * @Date 2021-05-20 下午 09:52
 */
@Mapper
public interface Db2Mapper {
    @Insert("insert into db2(name,age) values('fi',81)")
    int add();
}
?K^XNih~zhn{~{XyNyX[>ih~z[ZJ~Z^YZIiJ
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP