版本
- springboot 2.7.16
- druid 1.1.22
- mybatis-plus-boot-starter 3.4.2
- mysql 5.7.38
- dynamic-datasource-spring-boot-starter 4.1.3
当然别的版本应该也差不多,最多改改相关类
步骤
1、搭建一个简单springboot2.7.16+druid+mybatis-plus环境
五分钟搭建springboot2.7.16+druid+mybatis-plus环境
2、引入相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据库配置-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<!--数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<!--自动化配置-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.22</version>
</dependency>
<!--mybatis-plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>4.1.3</version>
</dependency>
3、配置文件引入数据源
spring:
autoconfigure:
#自动化配置 例外处理
exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
datasource:
type: com.alibaba.druid.pool.DruidDataSource
dynamic:
primary: master #设置默认的数据源或者数据源组,默认值即为mysql1
strict: false #严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
url: jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver # 3.2.0开始支持SPI可省略此配置
slave1:
url: jdbc:mysql://localhost:3306/demo2?serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
4、建库建表插入数据
#demo1
CREATE DATABASE demo1;
CREATE TABLE user(
userid BIGINT not null primary key COMMENT '主键ID,雪花算法生成',
username VARCHAR(64) COMMENT '用户名称:这里用微信的昵称'
);
INSERT INTO `demo1`.`user` (`userid`, `username`) VALUES (1, '小林');
INSERT INTO `demo1`.`user` (`userid`, `username`) VALUES (2, '小马');
#demo2
CREATE DATABASE demo2;
CREATE TABLE user(
userid BIGINT not null primary key COMMENT '主键ID,雪花算法生成',
username VARCHAR(64) COMMENT '用户名称:这里用微信的昵称'
);
INSERT INTO `demo2`.`user` (`userid`, `username`) VALUES (1, '小林2');
INSERT INTO `demo2`.`user` (`userid`, `username`) VALUES (2, '小马2');
5、新建User.class
package com.example.demo;
public class User {
private String userid;
private String username;
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@Override
public String toString() {
return "User{" +
"userid='" + userid + '\'' +
", username='" + username + '\'' +
'}';
}
}
6、在mapper,service或者方法上加上注解即可
MasterUserMapper.class
package com.example.demo.mapper.master;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.User;
import org.springframework.stereotype.Repository;
@Repository
@DS("master")
public interface MasterUserMapper extends BaseMapper<User> {
}
Slave1UserMapper.class
package com.example.demo.mapper.slave1;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.User;
import org.springframework.stereotype.Repository;
@Repository
@DS("slave1")
public interface Slave1UserMapper extends BaseMapper<User> {
}
7、在DemoController加上测试逻辑
package com.example.demo;
import com.example.demo.mapper.master.MasterUserMapper;
import com.example.demo.mapper.slave1.Slave1UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class DemoController {
@Autowired()
MasterUserMapper masterUserMapper;
@Autowired()
Slave1UserMapper slave1UserMapper;
@GetMapping("/find1")
public List<User> find1(){
return masterUserMapper.selectList(null);
}
@GetMapping("/find2")
public List<User> find2(){
return slave1UserMapper.selectList(null);
}
}
8、启动类加上注解@MapperScan
package com.example.demo;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan({"com.example.demo.mapper"})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
9、启动访问
[
{
"userid": "1",
"username": "小林1"
},
{
"userid": "2",
"username": "小马1"
}
]
[
{
"userid": "1",
"username": "小林2"
},
{
"userid": "2",
"username": "小马2"
}
]
搞定,还是很简单的!