题记部分

001 || 引入依赖

<!-- Code Generator -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.32</version>
</dependency>
<dependency>
<groupId>com.spring4all</groupId>
<artifactId>spring-boot-starter-swagger</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>

002 || 核心内容

package com.harley.common;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine; import java.util.ArrayList;
import java.util.List;
import java.util.Scanner; /**
* @author harley
* @since 2024-12-09 22:00:02
*/
public class CodeGenerator { /**
* <p>
* 读取控制台内容
* </p>
*/
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入").append(tip).append(":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
} public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator(); // 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("harley");
gc.setOpen(false);
gc.setSwagger2(true); // 实体属性 Swagger2 注解
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
gc.setServiceName("%sService");
mpg.setGlobalConfig(gc); // 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/accountmanager?useUnicode=true&useSSL=false&characterEncoding=utf8");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("!QAZ2wsx");
mpg.setDataSource(dsc); // 包配置
PackageConfig pc = new PackageConfig();
// pc.setModuleName(scanner("模块名"));
// 二、模块配置
pc.setParent("com.harley")
.setEntity("entity")
.setController("controller")
.setMapper("mapper")
.setService("service")
.setServiceImpl("service.impl");
mpg.setPackageInfo(pc); // 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
}; // 如果模板引擎是 freemarker
String templatePath = "/templates/mapper.xml.ftl";
// 如果模板引擎是 velocity
// String templatePath = "/templates/mapper.xml.vm"; // 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig(templatePath) {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
/*
cfg.setFileCreate(new IFileCreate() {
@Override
public boolean isCreate(ConfigBuilder configBuilder, FileType fileType, String filePath) {
// 判断自定义文件夹是否需要创建
checkDir("调用默认方法创建的目录,自定义目录用");
if (fileType == FileType.MAPPER) {
// 已经生成 mapper 文件判断存在,不想重新生成返回 false
return !new File(filePath).exists();
}
// 允许生成模板文件
return true;
}
});
*/
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg); // 配置模板
TemplateConfig templateConfig = new TemplateConfig(); // 配置自定义输出模板
//指定自定义模板路径,注意不要带上.ftl/.vm, 会根据使用的模板引擎自动识别
// templateConfig.setEntity("templates/entity2.java");
// templateConfig.setService();
// templateConfig.setController(); templateConfig.setXml(null);
mpg.setTemplate(templateConfig); // 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
// strategy.setTablePrefix(pc.getModuleName() + "_");
// 四、注意是否要去掉表前缀
// strategy.setTablePrefix("tb_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}

003 || 使用

直接运行CodeGenerator,根据提示即可自动生成controller、mapper、service、service实现类、实体类等各层的代码

JavaUtils - [03] 代码生成器(旧)的更多相关文章

  1. NFinal学习笔记 03—代码生成器

    NFinal代码生成器与其他的代码生成器不太一样,只需要运行模块下的WebComplier.aspx即可生成最终的web层代码.包括数据库的操作,Router类, 调试文件等.附上一段代码与大家分享 ...

  2. C++11 能好怎?

    0. 摘要 近期读了一些关于C++11标准的材料. 本篇博客将从新标准的优点.与旧版本的区别和使用方法三个角度,大致介绍我对C++11的认识. C++11标准,原名C++0x, 是03版旧标准的更新. ...

  3. 【转】.Net+MySQL组合开发 乱码篇

    所用工具MySQL5.022VS2005 Team SuiteMySQL Connector Net 5.0.3EMS SQL Manage 2005 For MySQL使用过MySQL的朋友都知道有 ...

  4. Linux第03天

    Linux 第03天 1.Linux帐号和ACL权限管理 1.帐号和用户组 1.1 用户标识符————UID(root为0 系统用户为1~499 普通用户为500~65535) 1.2 用户组标识符— ...

  5. 安装旧版的docker-engine-1.12.6

    执行kubeadm init --api-advertise-addresses=172.16.160.211命令的时候,提示docker版本太新了 想要安装旧版docker,可以使用以下方法: yu ...

  6. DB 查询分析器 6.03 如何灵活、快捷地操作国产达梦数据库

    DB 查询分析器 6.03 如何灵活.快捷地操作国产达梦数据库 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要       本文详细地介绍了"万能数据库查询分析器&qu ...

  7. DB 查询分析器 6.03 在Windows 8 上安装与运行演示

           DB 查询分析器 6.03 在Windows 8 上安装与运行演示 马根峰                ( 广东联合电子服务股份有限公司, 广州 510300) 摘要          ...

  8. “万能数据库查询分析器” 5.03发布,访问EXCEL将自动为表名前后加上中括弧

        "万能数据库查询分析器" 5.03发布,访问EXCEL将自动为表名前后加上中括弧 1          引言    中国本土程序员马根峰推出的个人作品----万能数据库查询 ...

  9. .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现

    本篇我将带着大家一起来对Dapper进行下封装并实现基本的增删改查.分页操作的同步异步方法的实现(已实现MSSQL,MySql,PgSQL).同时我们再实现一下仓储层的代码生成器,这样的话,我们只需要 ...

  10. 自己写的C#三层代码生成器

    思来想去用T4生成代码要学习它的语法,C#本身能很简单地生成txt文件,为啥不直接批量替换模板方式自己写个的三层代码生成器.说干就干,2个小时搞定.当然各层还可以做的更精细,比如DAL层的Add方法I ...

随机推荐

  1. docker创建Tomcat

    安装docker 查找tomcat docker search tomcat 下载镜像 docker pull tomcat 查看下载的镜像 docker images 运行Tomcat docker ...

  2. 【前端】【H5 API】地理定位(获取经纬度)

    H5 API 地理定位 地理定位在日常生活中应用比较广泛,例如,互联网打车.在线地图等.在HTML 5的规范中,增加了获取用户地理位置信息的接口Geolocation,开发者可以通过经纬度来获取用户的 ...

  3. Qt编写的项目作品32-定制化安装包工具(雨田哥作品)

    一.功能特点 纯Qt编写,跨平台. 支持自定义安装目录等. 安装和卸载界面可自定义. 一键式脚本build.bat,生成安装包EXE. 兼容XP系统. 支持配置文件填充安装包信息. 指定应用程序中文名 ...

  4. 以大模型攻大模型之💫Jailbreaking Black Box Large Language Models in 🎢Twenty Queries

    "在高层次上,PAIR将两个黑盒LLMs--我们称之为攻击者和目标--相互对抗--"具体是如何对抗的?请各位看官仔细阅读~

  5. svtools lmerge具体算法

    svtools具有不同的子命令以实现不同的功能,其中一个就是lmerge.根据其帮助文档(merge LUMPY calls inside a single file from svtools lso ...

  6. Harbor 共享后端高可用

    1. 主机配置 主机地址 主机配置 主机角色 软件版本 192.168.1.60 CPU:4C MEM:4GB Disk: 100GB Harbor+Keepalived Harbor 2.1.3 K ...

  7. docker-daemon配置

    { "api-cors-header":"", ------在引擎API中设置CORS标头 "authorization-plugins": ...

  8. Solution Set -「NOIP Simu.」20221113

    \(\mathscr{A}\sim\) 游戏   Cover:「ARC 087E」Prefix-free Game.   Tags:「A.博弈-SG 函数」「A.数据结构-Trie」   想了半天 ( ...

  9. Solution -「NOI 2017」「洛谷 P3823」蚯蚓排队

    \(\mathscr{Description}\)   Link.   (自己看题, 我总不能让题意比题解还长吧?) \(\mathscr{Solution}\)   下一组我一定写成 solutio ...

  10. Solution -「AGC 020F」Arcs on a Circle

    \(\mathscr{Description}\)   Link.   在一个周长为 \(c\) 的圆周上放置长度分别为 \(l_1,l_2,\cdots,l_n\) 的弧,每条弧的位置独立均匀随机. ...