spring如何集成mybatisPlus
时间: 2023-05-23 15:06:49 浏览: 85
spring+mybatis-plus集成
3星 · 编辑精心推荐
A:要把Mybatis Plus集成到Spring框架中,可以按照以下步骤进行操作:
1. 添加Mybatis Plus和Mybatis的依赖:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.3.1</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
```
2. 配置数据源:
```properties
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false&maxReconnects=10&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
```
3. 配置Mybatis Plus:
```properties
# mybatis-plus配置
mybatis-plus.mapper-locations=classpath:mapper/*.xml
# 自动填充
mybatis-plus.global-config.db-config.logic-delete-value=1
mybatis-plus.global-config.db-config.logic-not-delete-value=0
# SQL执行效率插件
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
```
4. 配置Mapper扫描路径:
```java
@Configuration
@MapperScan("com.example.mapper")
public class MybatisPlusConfig {
}
```
5. 完成以上配置后就可以使用Mybatis Plus了,例如在Mapper接口中使用:
```java
@Repository
public interface UserMapper extends BaseMapper<User> {
}
```
以上就是集成Mybatis Plus到Spring中的常规步骤。
阅读全文