要在SpringBoot中使用MyBatisPlus的ActiveRecord模式操作多个数据源的CRUD,可以采取以下步骤:
1. 添加依赖
在pom.xml文件中添加如下依赖:
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
<!-- 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>最新版本</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>最新版本</version>
</dependency>
2. 配置数据源
在application.yml文件中添加如下数据源配置:
spring:
datasource:
master:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
slave:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db2?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
3.配置MyBatisPlus
创建MyBatisPlus的配置类,并添加如下配置:
@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {
/**
* 主数据源
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource masterDataSource() {
return DruidDataSourceBuilder.create().build();
}
/**
* 从数据源
*/
@Bean
@ConfigurationProperties(prefix = "spring.datasource.slave")
public DataSource slaveDataSource() {
return DruidDataSourceBuilder.create().build();
}
/**
* 动态数据源配置
*/
@Bean
public DataSource dynamicDataSource(@Qualifier("masterDataSource") DataSource masterDataSource,
@Qualifier("slaveDataSource") DataSource slaveDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put("master", masterDataSource);
targetDataSources.put("slave", slaveDataSource);
// 这里默认数据源是 master
DynamicDataSource dataSource = new DynamicDataSource();
dataSource.setTargetDataSources(targetDataSources);
dataSource.setDefaultTargetDataSource(masterDataSource);
return dataSource;
}
/**
* SqlSessionFactory配置
*/
@Bean
public SqlSessionFactory sqlSessionFactoryBean(@Qualifier("dynamicDataSource") DataSource dynamicDataSource) throws Exception {
MybatisSqlSessionFactoryBean mybatisPlus = new MybatisSqlSessionFactoryBean();
mybatisPlus.setDataSource(dynamicDataSource);
// 配置MyBatis-Plus分页插件
mybatisPlus.setPlugins(new PaginationInterceptor());
return mybatisPlus.getObject();
}
}
4. 定义实体类
创建与数据表对应的实体类,并继承MyBatisPlus的Model类。
public class User extends Model<User> {
private Long id;
private String name;
private Integer age;
private String email;
// getter & setter
}
5. CRUD操作
public class User extends Model<User> {
private Long id;
private String name;
private Integer age;
private String email;
// getter & setter
/**
* 插入数据
*/
public boolean insertUser() {
return insert();
}
/**
* 查询单条记录
*/
public User selectOneUser(Long id) {
return selectById(id);
}
/**
* 更新数据
*/
public boolean updateUser() {
return updateById();
}
/**
* 删除数据
*/
public boolean deleteUser(Long id) {
return deleteById(id);
}
}
在Service层调用实体类的方法即可完成CRUD操作:
@Service
public class UserService {
/**
* 主数据源
*/
@Autowired
@Qualifier("master")
private DataSource masterDataSource;
/**
* 从数据源
*/
@Autowired
@Qualifier("slave")
private DataSource slaveDataSource;
/**
* 添加用户
*/
public boolean addUser(User user) {
return user.setDataSource(masterDataSource).insertUser();
}
/**
* 查询用户
*/
public User getUser(Long id) {
return new User().setDataSource(slaveDataSource).selectOneUser(id);
}
/**
* 更新用户
*/
public boolean updateUser(User user) {
return user.setDataSource(masterDataSource).updateUser();
}
/**
* 删除用户
*/
public boolean deleteUser(Long id) {
return new User().setDataSource(masterDataSource).deleteUser(id);
}
}