题记部分

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. OS之《内存管理》

    程序装入方式 绝对装入:程序逻辑地址和物理地址是完全对应的.不现实 可重定位装入:装入的时候重新 计算内存地址.程序中的实际地址加上程序载入的起始地址:但是解决不了进程挂起 后重新唤醒的问题.唤醒的后 ...

  2. [转载] 6 个技巧,提升 C++11 的 vector 性能

    转载:https://www.sohu.com/a/120595688_465979 Vector 就像是 C++ STL 容器的瑞士军刀.Bjarne Stoutsoup 有一句话 – " ...

  3. .NET 9 New features-AOT相关的改进

    上一篇文章给大家介绍了 .NET 9 New features-JSON序列化 本篇文章,研究分享一下关于AOT方面的改进 1. 什么是AOT AOT(Ahead-of-Time)编译是一种在应用程序 ...

  4. error C1083: 无法打开包括文件:“iostream.h”: No such file or directory

    用VS2010打开VC++6程序,按下F5键会发现有错误提示:error C1083: 无法打开包括文件:"iostream.h": No such file or directo ...

  5. JVM实战—4.JVM垃圾回收器的原理和调优

    大纲 1.JVM的新生代垃圾回收器ParNew如何工作 2.JVM老年代垃圾回收器CMS是如何工作的 3.线上部署系统时如何设置垃圾回收相关参数 4.新生代垃圾回收参数如何优化 5.老年代的垃圾回收参 ...

  6. 在MBP上运行推理LLaMA-7B&13B模型

    在MBP上运行推理LLaMA-7B模型 build this repo # build this repo git clone https://github.com/ggerganov/llama.c ...

  7. 基于高德地图API在Python中实现地图功能的方法

      本文介绍在高德开放平台中,申请.获取地图API的Key的方法:同时通过简单的Python代码,调取API信息,对所得Key的可用性加以验证.   首先,我们进入高德开放平台的官方网站.如果大家是第 ...

  8. JpaRepository动态代理执行原理

    本文基于spring-boot-starter-data-jpa:2.7.17分析 SpringBoot 里集成Jpa自动配置是如何处理的 通过分析SpringBoot 自动配置核心源码可以找到Jpa ...

  9. Harbor 共享后端高可用

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

  10. 探索自联接(SELF JOIN):揭示数据间复杂关系的强大工具

    title: 探索自联接(SELF JOIN):揭示数据间复杂关系的强大工具 date: 2025/1/11 updated: 2025/1/11 author: cmdragon excerpt: ...