版本
- springboot 2.7.16
- druid 1.1.22
- mybatis-plus-boot-starter 3.4.2
- mysql 5.7.38
当然别的版本应该也差不多,最多改改相关类
步骤
1、搭建一个简单springboot2.7.16+druid+mybatis-plus环境
五分钟搭建springboot2.7.16+druid+mybatis-plus环境
1、配置文件引入数据源
spring:
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai
username: root
password: 123456
slave1:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/demo2?serverTimezone=Asia/Shanghai
username: root
password: 123456
2、建库建表插入数据
#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');
3、新建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 + '\'' +
'}';
}
}
4、建两个mapper
MasterUserMapper.class
package com.example.demo.mapper.master;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.User;
import org.springframework.stereotype.Repository;
@Repository
public interface MasterUserMapper extends BaseMapper<User> {
}
Slave1UserMapper.class
package com.example.demo.mapper.slave1;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.User;
import org.springframework.stereotype.Repository;
@Repository
public interface Slave1UserMapper extends BaseMapper<User> {
}
可以看到MasterUserMapper在com.example.demo.mapper.master包,Slave1UserMapper在com.example.demo.mapper.slave1包
5、在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);
}
}
6、新增DataSourcesConfig配置
package com.example.demo;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
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 javax.sql.DataSource;
@Configuration
public class DataSourcesConfig {
@Bean
@ConfigurationProperties(prefix = "spring.datasource.master")
DataSource dsMaster(){
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties(prefix = "spring.datasource.slave1")
DataSource dsSlave1(){
return DruidDataSourceBuilder.create().build();
}
}
7、MyBatisConfigMaster.class
package com.example.demo;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.master",
sqlSessionFactoryRef = "sqlSessionFactory1")
public class MyBatisConfigMaster {
@Resource
DataSource dsMaster;
@Bean
SqlSessionFactory sqlSessionFactory1()throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean ();
bean.setDataSource(dsMaster);
return bean.getObject();
}
}
这里注意要用:MybatisSqlSessionFactoryBean
8、MyBatisConfigSlave1.class
package com.example.demo;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
import javax.sql.DataSource;
@Configuration
@MapperScan(basePackages = "com.example.demo.mapper.slave1",
sqlSessionFactoryRef = "sqlSessionFactory2")
public class MyBatisConfigSlave1 {
@Resource
DataSource dsSlave1;
@Bean
SqlSessionFactory sqlSessionFactory2()throws Exception {
MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean ();
bean.setDataSource(dsSlave1);
return bean.getObject();
}
}
这里注意要用:MybatisSqlSessionFactoryBean
9、启动类排除MybatisPlusAutoConfiguration
package com.example.demo;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(exclude = {MybatisPlusAutoConfiguration.class})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
需要排除MybatisPlusAutoConfiguration 数据源自动配置,排除Springboot的DataSourceAutoConfiguration 无效
后面发现,好像不排除也是可以的!
9、启动访问
[
{
"userid": "1",
"username": "小林1"
},
{
"userid": "2",
"username": "小马1"
}
]
[
{
"userid": "1",
"username": "小林2"
},
{
"userid": "2",
"username": "小马2"
}
]
也可以用测试类来简单测试下
package com.example.demo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HelloControllerIT {
@Autowired
private TestRestTemplate template;
@Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity("/find2", String.class);
System.out.println(response.getBody());
}
}
搞定,还是很简单的!