Spring Boot整合MyBatis-Plus,并通过AutoGenerator生成项目骨架代码
以下是一个简化的Spring Boot整合MyBatis-Plus并使用AutoGenerator自动生成项目骨架代码的示例:
- 在
pom.xml
中添加MyBatis-Plus和AutoGenerator的依赖:
<dependencies>
<!-- MyBatis-Plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.x.x</version>
</dependency>
<!-- AutoGenerator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.x.x</version>
</dependency>
</dependencies>
- 创建一个Java类用于生成代码:
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
public class MyBatisPlusGenerator {
public static void main(String[] args) {
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig.Builder("jdbc:mysql://localhost:3306/your_database", "username", "password").build();
// 全局配置
GlobalConfig gc = new GlobalConfig.Builder()
.outputDir(System.getProperty("user.dir") + "/src/main/java")
.author("author")
.build();
// 包配置
PackageConfig pc = new PackageConfig.Builder()
.parent("com.yourpackage")
.entity("model")
.mapper("mapper")
.service("service")
.serviceImpl("service.impl")
.controller("controller")
.build();
// 策略配置
StrategyConfig strategyConfig = new StrategyConfig.Builder()
.enableCapitalMode(true)
.enableTableFieldAnnotation(true)
.enableLombok(true)
.build();
// 自定义表名和需要生成的表
TableInfo tableInfo = new TableInfo.Builder("your_table_name", "your_table_name").build();
// 自动生成
AutoGenerator ag = new AutoGenerator();
ag.setGlobalConfig(gc);
ag.setDataSource(dsc);
ag.setPackageInfo(pc);
ag.setStrategy(strategyConfig);
ag.se
评论已关闭