个人随笔
目录
五分钟整合springboot+mybatis-plus+druid+分包模式多数据源
2023-10-19 11:36:34

版本

  • 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、配置文件引入数据源

  1. spring:
  2. datasource:
  3. master:
  4. driver-class-name: com.mysql.cj.jdbc.Driver
  5. url: jdbc:mysql://localhost:3306/demo?serverTimezone=Asia/Shanghai
  6. username: root
  7. password: 123456
  8. slave1:
  9. driver-class-name: com.mysql.cj.jdbc.Driver
  10. url: jdbc:mysql://localhost:3306/demo2?serverTimezone=Asia/Shanghai
  11. username: root
  12. password: 123456

2、建库建表插入数据

  1. #demo1
  2. CREATE DATABASE demo1;
  3. CREATE TABLE user(
  4. userid BIGINT not null primary key COMMENT '主键ID,雪花算法生成',
  5. username VARCHAR(64) COMMENT '用户名称:这里用微信的昵称'
  6. );
  7. INSERT INTO `demo1`.`user` (`userid`, `username`) VALUES (1, '小林');
  8. INSERT INTO `demo1`.`user` (`userid`, `username`) VALUES (2, '小马');
  9. #demo2
  10. CREATE DATABASE demo2;
  11. CREATE TABLE user(
  12. userid BIGINT not null primary key COMMENT '主键ID,雪花算法生成',
  13. username VARCHAR(64) COMMENT '用户名称:这里用微信的昵称'
  14. );
  15. INSERT INTO `demo2`.`user` (`userid`, `username`) VALUES (1, '小林2');
  16. INSERT INTO `demo2`.`user` (`userid`, `username`) VALUES (2, '小马2');

3、新建User.class

  1. package com.example.demo;
  2. public class User {
  3. private String userid;
  4. private String username;
  5. public String getUserid() {
  6. return userid;
  7. }
  8. public void setUserid(String userid) {
  9. this.userid = userid;
  10. }
  11. public String getUsername() {
  12. return username;
  13. }
  14. public void setUsername(String username) {
  15. this.username = username;
  16. }
  17. @Override
  18. public String toString() {
  19. return "User{" +
  20. "userid='" + userid + '\'' +
  21. ", username='" + username + '\'' +
  22. '}';
  23. }
  24. }

4、建两个mapper

MasterUserMapper.class

  1. package com.example.demo.mapper.master;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.example.demo.User;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface MasterUserMapper extends BaseMapper<User> {
  7. }

Slave1UserMapper.class

  1. package com.example.demo.mapper.slave1;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.example.demo.User;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface Slave1UserMapper extends BaseMapper<User> {
  7. }

可以看到MasterUserMapper在com.example.demo.mapper.master包,Slave1UserMapper在com.example.demo.mapper.slave1包

5、在DemoController加上测试逻辑

  1. package com.example.demo;
  2. import com.example.demo.mapper.master.MasterUserMapper;
  3. import com.example.demo.mapper.slave1.Slave1UserMapper;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.util.List;
  8. @RestController
  9. public class DemoController {
  10. @Autowired()
  11. MasterUserMapper masterUserMapper;
  12. @Autowired()
  13. Slave1UserMapper slave1UserMapper;
  14. @GetMapping("/find1")
  15. public List<User> find1(){
  16. return masterUserMapper.selectList(null);
  17. }
  18. @GetMapping("/find2")
  19. public List<User> find2(){
  20. return slave1UserMapper.selectList(null);
  21. }
  22. }

6、新增DataSourcesConfig配置

  1. package com.example.demo;
  2. import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
  3. import org.springframework.boot.context.properties.ConfigurationProperties;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.context.annotation.Primary;
  7. import javax.sql.DataSource;
  8. @Configuration
  9. public class DataSourcesConfig {
  10. @Bean
  11. @ConfigurationProperties(prefix = "spring.datasource.master")
  12. DataSource dsMaster(){
  13. return DruidDataSourceBuilder.create().build();
  14. }
  15. @Bean
  16. @ConfigurationProperties(prefix = "spring.datasource.slave1")
  17. DataSource dsSlave1(){
  18. return DruidDataSourceBuilder.create().build();
  19. }
  20. }

7、MyBatisConfigMaster.class

  1. package com.example.demo;
  2. import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.mybatis.spring.SqlSessionFactoryBean;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import javax.annotation.Resource;
  9. import javax.sql.DataSource;
  10. @Configuration
  11. @MapperScan(basePackages = "com.example.demo.mapper.master",
  12. sqlSessionFactoryRef = "sqlSessionFactory1")
  13. public class MyBatisConfigMaster {
  14. @Resource
  15. DataSource dsMaster;
  16. @Bean
  17. SqlSessionFactory sqlSessionFactory1()throws Exception {
  18. MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean ();
  19. bean.setDataSource(dsMaster);
  20. return bean.getObject();
  21. }
  22. }

这里注意要用:MybatisSqlSessionFactoryBean

8、MyBatisConfigSlave1.class

  1. package com.example.demo;
  2. import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
  3. import org.apache.ibatis.session.SqlSessionFactory;
  4. import org.mybatis.spring.SqlSessionFactoryBean;
  5. import org.mybatis.spring.annotation.MapperScan;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.Configuration;
  8. import javax.annotation.Resource;
  9. import javax.sql.DataSource;
  10. @Configuration
  11. @MapperScan(basePackages = "com.example.demo.mapper.slave1",
  12. sqlSessionFactoryRef = "sqlSessionFactory2")
  13. public class MyBatisConfigSlave1 {
  14. @Resource
  15. DataSource dsSlave1;
  16. @Bean
  17. SqlSessionFactory sqlSessionFactory2()throws Exception {
  18. MybatisSqlSessionFactoryBean bean = new MybatisSqlSessionFactoryBean ();
  19. bean.setDataSource(dsSlave1);
  20. return bean.getObject();
  21. }
  22. }

这里注意要用:MybatisSqlSessionFactoryBean

9、启动类排除MybatisPlusAutoConfiguration

  1. package com.example.demo;
  2. import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
  3. import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
  4. import org.mybatis.spring.annotation.MapperScan;
  5. import org.springframework.boot.SpringApplication;
  6. import org.springframework.boot.autoconfigure.SpringBootApplication;
  7. @SpringBootApplication(exclude = {MybatisPlusAutoConfiguration.class})
  8. public class DemoApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(DemoApplication.class, args);
  11. }
  12. }

需要排除MybatisPlusAutoConfiguration 数据源自动配置,排除Springboot的DataSourceAutoConfiguration 无效

后面发现,好像不排除也是可以的!

9、启动访问

http://localhost:8080/find1

  1. [
  2. {
  3. "userid": "1",
  4. "username": "小林1"
  5. },
  6. {
  7. "userid": "2",
  8. "username": "小马1"
  9. }
  10. ]

http://localhost:8080/find2

  1. [
  2. {
  3. "userid": "1",
  4. "username": "小林2"
  5. },
  6. {
  7. "userid": "2",
  8. "username": "小马2"
  9. }
  10. ]

也可以用测试类来简单测试下

  1. package com.example.demo;
  2. import org.junit.jupiter.api.Test;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.boot.test.context.SpringBootTest;
  5. import org.springframework.boot.test.web.client.TestRestTemplate;
  6. import org.springframework.http.ResponseEntity;
  7. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  8. public class HelloControllerIT {
  9. @Autowired
  10. private TestRestTemplate template;
  11. @Test
  12. public void getHello() throws Exception {
  13. ResponseEntity<String> response = template.getForEntity("/find2", String.class);
  14. System.out.println(response.getBody());
  15. }
  16. }

搞定,还是很简单的!

 77

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2