mybatis?plus自動(dòng)生成代碼的示例代碼
寫(xiě)一個(gè)簡(jiǎn)單的mybatis plus插件自動(dòng)生成代碼的例子
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() // 開(kāi)啟 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è)置過(guò)濾表前綴
? ? ? ? ? ? ? ? })
? ? ? ? ? ? ? ?// .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板,默認(rèn)的是Velocity引擎模板
? ? ? ? ? ? ? ? .execute();
? ? }
}修改內(nèi)容
- entity使用了BaseEntity,添加了 id、deleted、createTime、updateTime、createBy、updateBy等公共字段
- controller使用了BaseRestController
- tips:可以直接把代碼生成在對(duì)應(yīng)的工作目錄
把生成的代碼拷至對(duì)應(yīng)的工作目錄
添加創(chuàng)建時(shí)間、修改時(shí)間自動(dò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
? ? }
}啟動(dòng) 測(cè)試
訪問(wèn)swagger測(cè)試相應(yīng)接口
http://127.0.0.1:10089/doc.html
參考資料
https://baomidou.com/pages/24112f/
到此這篇關(guān)于mybatis plus自動(dòng)生成代碼的示例代碼的文章就介紹到這了,更多相關(guān)mybatis plus自動(dòng)生成代碼內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 獲取Html文本中的img標(biāo)簽下src中的內(nèi)容方法
今天小編就為大家分享一篇Java 獲取Html文本中的img標(biāo)簽下src中的內(nèi)容方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-06-06
JavaWeb?Servlet實(shí)現(xiàn)文件上傳與下載功能實(shí)例
因自己負(fù)責(zé)的項(xiàng)目中需要實(shí)現(xiàn)文件上傳,所以下面下面這篇文章主要給大家介紹了關(guān)于JavaWeb?Servlet實(shí)現(xiàn)文件上傳與下載功能的相關(guān)資料,需要的朋友可以參考下2022-04-04
idea中定時(shí)及多數(shù)據(jù)源配置方法
因項(xiàng)目要求,需要定時(shí)從達(dá)夢(mèng)數(shù)據(jù)庫(kù)中取數(shù)據(jù),并插入或更新到ORACLE數(shù)據(jù)庫(kù)中,這篇文章主要介紹了idea中定時(shí)及多數(shù)據(jù)源配置方法,需要的朋友可以參考下2023-12-12
java.lang.UnsatisfiedLinkError: %1 不是有效的Win32應(yīng)用程序錯(cuò)誤解決
這篇文章主要給大家介紹了關(guān)于java.lang.UnsatisfiedLinkError: %1 不是有效的Win32應(yīng)用程序錯(cuò)誤的解決方法,文中介紹的非常詳細(xì),需要的朋友們可以參考學(xué)習(xí),下面來(lái)一起看看吧。2017-03-03
使用Logback設(shè)置property參數(shù)方式
這篇文章主要介紹了使用Logback設(shè)置property參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
帶你輕松搞定Java面向?qū)ο蟮木幊?-數(shù)組,集合框架
Java是面向?qū)ο蟮母呒?jí)編程語(yǔ)言,類(lèi)和對(duì)象是 Java程序的構(gòu)成核心。圍繞著Java類(lèi)和Java對(duì)象,有三大基本特性:封裝是Java 類(lèi)的編寫(xiě)規(guī)范、繼承是類(lèi)與類(lèi)之間聯(lián)系的一種形式、而多態(tài)為系統(tǒng)組件或模塊之間解耦提供了解決方案2021-06-06

