springboot中mybatisplus配置信息
时间: 2023-12-16 16:28:12 浏览: 102
以下是SpringBoot整合MyBatisPlus配置信息的步骤:
1.在pom.xml文件中添加MyBatisPlus和MySQL驱动的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
```
2.在application.yml文件中配置数据源信息和MyBatisPlus的相关配置:
```yaml
spring:
datasource:
dynamic:
primary: master
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_master?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
slave:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db_slave?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
mybatis-plus:
mapper-locations: classpath*:mapper/**/*.xml
type-aliases-package: com.example.demo.entity
```
3.创建数据源配置类DynamicDataSourceConfig,用于动态切换数据源:
```java
@Configuration
public class DynamicDataSourceConfig {
@Bean
@ConfigurationProperties("spring.datasource.druid.master")
public DataSource masterDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@ConfigurationProperties("spring.datasource.druid.slave")
public DataSource slaveDataSource() {
return DruidDataSourceBuilder.create().build();
}
@Bean
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource slaveDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceEnum.MASTER.getName(), masterDataSource);
targetDataSources.put(DataSourceEnum.SLAVE.getName(), slaveDataSource);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
}
```
4.创建数据源枚举类DataSourceEnum,用于定义数据源的名称:
```java
public enum DataSourceEnum {
MASTER("master"),
SLAVE("slave");
private String name;
DataSourceEnum(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
```
5.创建动态数据源类DynamicDataSource,用于动态切换数据源:
```java
public class DynamicDataSource extends AbstractRoutingDataSource {
private static final ThreadLocal<String> CONTEXT_HOLDER = new ThreadLocal<>();
private final Map<Object, Object> targetDataSources;
public DynamicDataSource(DataSource defaultDataSource, Map<Object, Object> targetDataSources) {
super.setDefaultTargetDataSource(defaultDataSource);
this.targetDataSources = targetDataSources;
super.setTargetDataSources(this.targetDataSources);
super.afterPropertiesSet();
}
@Override
protected Object determineCurrentLookupKey() {
return getDataSource();
}
public static void setDataSource(String dataSource) {
CONTEXT_HOLDER.set(dataSource);
}
public static String getDataSource() {
return CONTEXT_HOLDER.get();
}
public static void clearDataSource() {
CONTEXT_HOLDER.remove();
}
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
}
}
```
6.创建数据源切换的AOP类DataSourceAspect,用于在方法执行前切换数据源:
```java
@Aspect
@Component
public class DataSourceAspect {
@Pointcut("@annotation(com.example.demo.annotation.DataSource)")
public void dataSourcePointCut() {
}
@Before("dataSourcePointCut()")
public void before(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
DataSource dataSource = signature.getMethod().getAnnotation(DataSource.class);
if (dataSource == null) {
DynamicDataSource.setDataSource(DataSourceEnum.MASTER.getName());
} else {
DynamicDataSource.setDataSource(dataSource.value().getName());
}
}
@After("dataSourcePointCut()")
public void after(JoinPoint joinPoint) {
DynamicDataSource.clearDataSource();
}
}
```
7.在Mapper接口中使用@Mapper注解,并在Mapper.xml文件中使用MyBatisPlus提供的标签进行SQL语句的编写。
阅读全文