软件编程
位置:首页>> 软件编程>> java编程>> mybatis plus自动生成代码的示例代码

mybatis plus自动生成代码的示例代码

作者:weedmmg  发布时间:2022-03-01 10:00:25 

标签:mybatis,plus,生成代码

写一个简单的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") // 设置作者
? ? ? ? ? ? ? ? ? ? ? ? ? ? .fileOverride()
? ? ? ? ? ? ? ? ? ? ? ? ? ?//.enableSwagger() // 开启 swagger 模式
? ? ? ? ? ? ? ? ? ? ? ? ? ? .outputDir("D://gen//java//"); // 指定输出目录
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ? .packageConfig(builder -> {
? ? ? ? ? ? ? ? ? ? builder.parent("com.home") // 设置父包名
? ? ? ? ? ? ? ? ? ? ? ? ? ? .moduleName("system") // 设置父包模块名
? ? ? ? ? ? ? ? ? ? ? ? ? ? .controller("rest")//controller 改名 rest

? ? ? ? ? ? ? ? ? ? ? ? ? ? .pathInfo(Collections.singletonMap(OutputFile.xml, "D://gen//resources//mapper")); // 设置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()
? ? ? ? ? ? ? ? ? ? ? ? ? ? ; // 设置需要生成的表名
? ? ? ? ? ? ? ? ? ? ? ? ? ?// .addTablePrefix("t_", "c_"); // 设置过滤表前缀
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ?// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默认的是Velocity引擎模板
? ? ? ? ? ? ? ? .execute();
? ? }
}

修改内容

  • entity使用了BaseEntity,添加了 id、deleted、createTime、updateTime、createBy、updateBy等公共字段

  • controller使用了BaseRestController

  • tips:可以直接把代码生成在对应的工作目录

把生成的代码拷至对应的工作目录
添加创建时间、修改时间自动填充配置

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测试相应接口
http://127.0.0.1:10089/doc.html

参考资料
https://baomidou.com/pages/24112f/

来源:https://juejin.cn/post/7094537088086835207

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com