您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# MyBatis-Plus如何配置环境
MyBatis-Plus(简称MP)是MyBatis的增强工具,在保留原生功能的基础上提供了更多便捷功能。本文将详细介绍如何从零开始配置MyBatis-Plus环境。
## 一、环境准备
### 1. 基础依赖
确保项目中已包含:
- JDK 1.8+
- Maven 3.0+
- Spring Boot 2.x(可选)
- MyBatis 3.5+
### 2. 开发工具推荐
- IDEA/Eclipse
- MySQL 5.7+
- Postman(API测试)
## 二、Maven项目配置
### 1. 添加核心依赖
```xml
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mp_demo?useSSL=false
username: root
password: 123456
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 开启SQL日志
global-config:
db-config:
id-type: auto # 主键自增策略
@SpringBootApplication
@MapperScan("com.example.mapper") // 扫描Mapper接口
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
</dependency>
FastAutoGenerator.create("jdbc:mysql://localhost:3306/mp_demo", "root", "123456")
.globalConfig(builder -> builder.outputDir("src/main/java"))
.packageConfig(builder -> builder.parent("com.example"))
.strategyConfig(builder -> builder.addInclude("user"))
.execute();
spring.datasource
配置@TableName
注解指定表名@TableId
标注主键@TableField
检查是否配置了日志实现:
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
建议使用第三方组件:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot-starter</artifactId>
<version>3.6.1</version>
</dependency>
@Bean
public PerformanceInterceptor performanceInterceptor() {
PerformanceInterceptor interceptor = new PerformanceInterceptor();
interceptor.setFormat(true); // 格式化SQL
return interceptor;
}
通过以上步骤即可完成MyBatis-Plus的基础环境配置。更多高级功能可参考官方文档。 “`
该文档包含: 1. 环境要求说明 2. 分步骤的配置指南 3. 代码示例和YAML配置 4. 常见问题解决方案 5. 扩展功能建议 6. 官方文档引用
可根据实际项目需求调整数据库配置、包路径等参数。建议在正式项目中使用更高版本的依赖。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。