寫一個簡單的mybatis plus插件自動生成代碼的例子
pom.xml 添加配置
<!-- mybatis plus 插件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.1</version>
</dependency>
<!-- mybatis plus 代碼生成插件-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.2</version>
</dependency>
<!-- mybatis plus代碼生成模板-->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.0</version>
</dependency>
添加生成代碼配置
package com.home.base.gen;/**
* @author chenxf
* @date 2022/5/5 15:00
*/
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import com.home.base.entity.BaseEntity;
import com.home.base.rest.BaseRestController;
import java.util.Collections;
/**
* @author chenxf
* @date 2022/5/5 15:00
*/
public class MybatisPlusGen {
public static void main(String[] args){
FastAutoGenerator
.create("jdbc:mysql://127.0.0.1:3306/policy_job?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF8", "root", "123456")
.globalConfig(builder -> {
builder.author("chenxf") // 設(shè)置作者
.fileOverride()
//.enableSwagger() // 開啟 swagger 模式
.outputDir("D://gen//java//"); // 指定輸出目錄
})
.packageConfig(builder -> {
builder.parent("com.home") // 設(shè)置父包名
.moduleName("system") // 設(shè)置父包模塊名
.controller("rest")//controller 改名 rest
.pathInfo(Collections.singletonMap(OutputFile.xml, "D://gen//resources//mapper")); // 設(shè)置mapperXml生成路徑
})
.templateConfig(builder -> {
builder.entity("/templates/entity.java")
.controller("/templates/controller.java");
})
.strategyConfig(builder -> {
builder.addInclude("test_test")
.entityBuilder().superClass(BaseEntity.class)
.disableSerialVersionUID()
.enableChainModel()
.enableLombok()
.enableRemoveIsPrefix()
.enableTableFieldAnnotation()
.logicDeleteColumnName("deleted")
.logicDeletePropertyName("deleteFlag")
.addSuperEntityColumns("id", "create_by","deleted", "create_time", "update_by", "update_time")
.addTableFills(new Column("create_time", FieldFill.INSERT))
.addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
.idType(IdType.AUTO)
.formatFileName("%sEntity").build()
.controllerBuilder()
.superClass(BaseRestController.class)
.enableRestStyle()
.formatFileName("%sApiController")
.build()
; // 設(shè)置需要生成的表名
// .addTablePrefix("t_", "c_"); // 設(shè)置過濾表前綴
})
// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板饿肺,默認(rèn)的是Velocity引擎模板
.execute();
}
}
- 修改內(nèi)容
- entity使用了BaseEntity,添加了 id呆躲、deleted、createTime墓怀、updateTime弹囚、createBy揉稚、updateBy等公共字段
- controller使用了BaseRestController
- tips:可以直接把代碼生成在對應(yīng)的工作目錄
把生成的代碼拷至對應(yīng)的工作目錄
添加創(chuàng)建時間秒啦、修改時間自動填充配置
package com.home.component;/**
* @author chenxf
* @date 2022/5/6 15:57
*/
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
/**
* @author chenxf
* @date 2022/5/6 15:57
*/
@Slf4j
@Component
public class BaseHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
log.info("start insert fill ....");
this.fillStrategy(metaObject, "createTime", LocalDateTime.now());
this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
//TODO set createBy
}
@Override
public void updateFill(MetaObject metaObject) {
log.info("start update fill ....");
this.fillStrategy(metaObject, "updateTime", LocalDateTime.now());
//TODO set updateBy
}
}
啟動 測試
訪問swagger測試相應(yīng)接口
http://127.0.0.1:10089/doc.html
示例代碼
https://gitee.com/WaiBao_cxf/mybatis-plus-demo