本篇文章主要讲解的是使用多数据源,至于springboot与mybatisplus的整合这里只做简单介绍
springboot整合mybatisplus(非本文重点)
在pom文件中引入如下的依赖
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
就这么简单了,哈哈,其他的就不多说了哈,至于整合后mapper文件放在哪里就不用我多说了吧。
如何使用多数据源(重点)
①引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.16</version>
</dependency>
<!-- 导入多数据源依赖-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.1</version>
</dependency>
②配置yml
server:
# 设置项目的端口号
port: 8888
# servlet:
# context-path: /demo
spring:
mvc:
pathmatch:
# 加这个的原因是为了更好的使用swagger
matching-strategy: ant_path_matcher
jackson:
date-format: yyyy-MM-dd
time-zone: GMT+8
datasource:
dynamic:
primary: master
strict: false
datasource:
master:
username: root
password: 1230
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_plus?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
slave1:
username: root
password: 1230
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/basic-project?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
type: com.alibaba.druid.pool.DruidDataSource
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
③如何从主从数据库获取我们要的东西呢
我们的主数据库有个user表
从数据库中有个student表
④定义各自的实体类
package com.summer.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @date 2023-05-31 07:39
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
@TableId(type =IdType.ASSIGN_ID)
private Long id;
private String name;
private String email;
private String gender;
}
package com.summer.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* @date 2023-06-03 22:38
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
@TableId(type = IdType.ASSIGN_ID)
private String id;
private String name;
}
⑤定义各自的mapper接口
package com.summer.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.summer.domain.User;
/**
* @date 2023-05-31 07:39
*/
//@Repository
public interface UserMapper extends BaseMapper<User> {
}
package com.summer.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.summer.domain.Student;
/**
* @date 2023-06-03 22:39
*/
public interface StudentMapper extends BaseMapper<Student> {
}
⑥定义各自的Service以及实现类
package com.summer.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.summer.domain.Student;
/**
* @date 2023-06-03 22:40
*/
public interface StudentService extends IService<Student> {
}
package com.summer.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.domain.Student;
import com.summer.mapper.StudentMapper;
import org.springframework.stereotype.Service;
/**
* @date 2023-06-03 22:40
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {
}
package com.summer.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.summer.domain.User;
import java.util.List;
/**
* @date 2023-05-31 08:25
*/
public interface UserService extends IService<User> {
}
package com.summer.service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.summer.domain.User;
import com.summer.mapper.UserMapper;
import org.springframework.stereotype.Service;
/**
* @date 2023-05-31 08:26
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
⑦开始编写
我们上面讲过,user表是在我们的主数据库master里,student是在我们从数据库slave1里,那么我们在开发的时候如何区分他们呢?
这就需要我们使用一个注解@DS 来进行区分,我们在各自的Service类上添加注解
⑧测试
我写了一个接口来进行测试
@RestController
@RequestMapping("/adminApi/dynamic")
@RequiredArgsConstructor
public class DemoController {
private final UserService userService;
private final StudentService studentService;
@GetMapping("/hello")
public String hello(){
List<User> list =
userService.list();
System.out.println("user:"+list.size());
List<Student> list1 = studentService.list();
System.out.println("student:"+list1.size());
return "success";
}
如果我们主表和从表中的表的名字一样,比如都将user怎么办?
我们的DS注解现在是写在我们的Service类上的,这个注解可以写在我们指定的方法上,如果是写在类上的话比如
@DS("slave1") 那么这个类中的所有方法的操作都是从slave1对应的表中取得
如果写在方法上,那么这个方法对应的操作就是从slave1中取。