初始化 MybatisPlus的代码是什么样的
时间: 2024-01-11 17:49:43 浏览: 28
初始化 MybatisPlus 的代码通常如下:
1. 引入 MybatisPlus 的依赖包:
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
```
2. 在 Spring Boot 的配置文件中,配置 MybatisPlus 的相关属性:
```yaml
mybatis-plus:
mapper-locations: classpath:mapper/*.xml # mapper xml 文件的路径
type-aliases-package: com.example.entity # 实体类的包路径
```
3. 在 Spring Boot 的配置文件中,配置数据源:
```yaml
spring:
datasource:
url: jdbc:mysql://localhost:3306/mybatis_plus_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
```
4. 在 Spring Boot 启动类中,添加 `@MapperScan` 注解,扫描 MybatisPlus 的 Mapper 接口:
```java
@SpringBootApplication
@MapperScan("com.example.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```
5. 编写实体类和对应的 Mapper 接口,继承 MybatisPlus 提供的 `BaseMapper` 接口:
```java
public interface UserMapper extends BaseMapper<User> {
}
```
这样就完成了 MybatisPlus 的初始化配置。