mybatisplus多数据源

适用于多种场景:纯粹多库、 读写分离、 一主多从、 混合模式等

场景说明:

我们创建两个库,分别为:mybatis_plus(以前的库不动)与mybatis_plus_1(新建),将mybatis_plus库的product表移动到mybatis_plus_1库,这样每个库一张表,通过一个测试用例分别获取用户数据与商品数据,如果获取到说明多库模拟成功

1.创建数据库及表

  • 创建数据库mybatis_plus_1和表`product

    CREATE DATABASE `mybatis_plus_1` /*!40100 DEFAULT CHARACTER SET utf8mb4 */;
    use `mybatis_plus_1`; 
    CREATE TABLE product ( 
        id BIGINT(20) NOT NULL COMMENT '主键ID', 
        name VARCHAR(30) NULL DEFAULT NULL COMMENT '商品名称', 
        price INT(11) DEFAULT 0 COMMENT '价格', 
        version INT(11) DEFAULT 0 COMMENT '乐观锁版本号', 
        PRIMARY KEY (id) 
    );
    
  • 添加测试数据

    INSERT INTO product (id, NAME, price) VALUES (1, '外星人笔记本', 100);
    
  • 删除mybatis_plus库中的product

    use mybatis_plus; 
    DROP TABLE IF EXISTS product;
    

mybatisplus多数据源_第1张图片

2.新建工程引入依赖

自行新建一个Spring Boot工程并选择MySQL驱动及Lombok依赖

引入MyBaits-Plus的依赖及多数据源的依赖

<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>mybatis-plus-boot-starterartifactId>
    <version>3.5.1version>
dependency>

<dependency>
    <groupId>com.baomidougroupId>
    <artifactId>dynamic-datasource-spring-boot-starterartifactId>
    <version>3.5.0version>
dependency>

3.编写配置文件

spring:
  # 配置数据源信息
  datasource:
    dynamic:
      # 设置默认的数据源或者数据源组,默认值即为master
      primary: master
      # 严格匹配数据源,默认false.true未匹配到指定数据源时抛异常,false使用默认数据源
      strict: false
      datasource:
        master:
          url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf-8&useSSL=false
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: Thm123456!
        slave_1:
          url: jdbc:mysql://localhost:3306/mybatis_plus_1?characterEncoding=utf-8&useSSL=false
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: Thm123456!

4.创建实体类

  • 新建一个User实体类

    @Data
    public class User {
        private Long id;
        private String name;
        private Integer age;
        private String email;
    }
    
  • 新建一个实体类Product

    @Data
    public class Product {
        private Long id;
        private String name;
        private Integer price;
        private Integer version;
    }
    

5.创建Mapper及Service

  • 新建接口UserMapper

    public interface UserMapper extends BaseMapper<User> {}
    
  • 新建接口ProductMapper

    public interface ProductMapper extends BaseMapper<Product> {}
    
  • 新建Service接口UserService指定操作的数据源

    public interface UserService extends IService<User> {}
    
  • 新建Service接口ProductService指定操作的数据源

    public interface ProductService extends IService<Product> {}
    
  • 建立Service的实现类

ProductServiceImpl

@Service
@DS("slave_1")
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
}

UserServiceImpl

/**
 * ServiceImpl实现了IService,提供了IService中基础功能的实现
 * 若ServiceImpl无法满足业务需求,则可以使用自定的UserService定义方法,并在实现类中实现
 */
@Service
@DS("master")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}

6.编写测试方法

记得在启动类中添加注解@MapperScan()

class TestDatasourceApplicationTests {
	@Resource
	UserService userService;

	@Resource
	ProductService productService;

	@Test
	void contextLoads() {
		User user = userService.getById(1L);
		Product product = productService.getById(1L);
		System.out.println("User = " + user);
		System.out.println("Product = " + product);
	}

}

image-20220522113049945

你可能感兴趣的:(mybatis,mybatis)