栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis-plus 多数据源环境

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis-plus 多数据源环境

Mybatis-plus 多数据源环境

参考资料:https://www.bilibili.com/video/BV12R4y157Be?p=52

模拟多数据源环境 1.设置配置文件
spring:
# 配置数据源信息
  datasource:
    dynamic:
    # 设置默认的数据源或数据源组,默认值为master
      primary: master
      # 严格匹配数据源,默认为false;为true时未匹配到指定数据源会抛出异常
      strict: false
      datasource:
# 主数据源
        master:
          url: jdbc:mysql://localhost:3306/db_mp?characterEncoding=utf-8&useSSL=false
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: 123456
# 从数据源
        slave_1:
          url: jdbc:mysql://localhost:3306/db_mp1?characterEncoding=utf-8&useSSL=false
          driver-class-name: com.mysql.cj.jdbc.Driver
          username: root
          password: 123456

假设现在有两个数据源一个为db_mp,其中包含员工表t_employee;另一个为db_mp1,其中包含商品信息表t_product

2.搭建环境 导入依赖

            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            com.baomidou
            mybatis-plus-boot-starter
            3.5.1
        

        
            org.projectlombok
            lombok
            true
        

        
            mysql
            mysql-connector-java
            runtime
        

        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.5.1
        
entity

Employee

package com.example.mybatisplus_datasource.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

@TableName("t_employee")
@Data
public class Employee {
    private Integer id;

    private String name;

    private String gender;

    private String email;
    private String phoneNumber;
    @TableField(value = "departmentId")
    private Integer departmentId;
    private Integer salary;

}

Product

package com.example.mybatisplus_datasource.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.Version;
import lombok.Data;

@TableName("t_product")
@Data
public class Product {
    private Integer id;

    private String name;

    private Integer price;
    @Version//标识乐观锁版本号字段
    private Integer version;
}

mapper

EmployeeMapper

package com.example.mybatisplus_datasource.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus_datasource.entity.Employee;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeMapper extends BaseMapper {
}

ProductMapper

package com.example.mybatisplus_datasource.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.mybatisplus_datasource.entity.Product;
import org.springframework.stereotype.Repository;

@Repository
public interface ProductMapper extends BaseMapper {
}

service

EmployeeService

package com.example.mybatisplus_datasource.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.mybatisplus_datasource.entity.Employee;

public interface EmployeeService extends IService {
}

ProductService

package com.example.mybatisplus_datasource.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.mybatisplus_datasource.entity.Product;

public interface ProductService extends IService {

}

Impl

EmployeeServiceImpl
注解DS中的值和yml配置文件中的值相对应

package com.example.mybatisplus_datasource.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus_datasource.entity.Employee;
import com.example.mybatisplus_datasource.mapper.EmployeeMapper;
import com.example.mybatisplus_datasource.service.EmployeeService;
import org.springframework.stereotype.Service;

@Service
@DS("master")
public class EmployeeServiceImpl extends ServiceImpl implements EmployeeService {
}

ProductServiceImpl

package com.example.mybatisplus_datasource.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus_datasource.entity.Product;
import com.example.mybatisplus_datasource.mapper.ProductMapper;
import com.example.mybatisplus_datasource.service.EmployeeService;
import com.example.mybatisplus_datasource.service.ProductService;
import org.springframework.stereotype.Service;

@Service
@DS("slave_1")
public class ProductServiceImpl extends ServiceImpl implements ProductService {
}

测试类
package com.example.mybatisplus_datasource;

import com.example.mybatisplus_datasource.service.EmployeeService;
import com.example.mybatisplus_datasource.service.ProductService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class MybatisplusDatasourceApplicationTests {

    @Test
    void contextLoads() {
    }
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private ProductService productService;

    @Test
    public void test(){
        System.out.println(employeeService.getById(1));
        System.out.println(productService.getById(1));

    }

}

在测试类中使用getById方法查询数据库中的数据,根据返回结果判断是否成功配置多数据源环境,运行发现查询成功,多数据源环境成功配置。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/860027.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号