MyBatis-Plus 代码生成器模板
MyBatis-Plus 代码生成器模板
maven 依赖
<!--Mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.23</version>
</dependency>
<!-- druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.8</version>
</dependency>
<!-- mybatis plus -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<!-- mybatis plus 代码生成器 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.1</version>
</dependency>
<!-- mybatis plus 代码生成器模板 -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
代码生成器
public class CreateCode {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
final String projectPath = "user.dir";
gc.setOutputDir(projectPath+ "/src/main/java"); //生成的代码放哪里
gc.setFileOverride(true); // 是否覆盖
gc.setOpen(false); // 是否打开输出目录
// gc.setEnableCache(true); // 是否在xml中添加二级缓存配置
gc.setAuthor("XXX"); // 开发人员
// gc.setSwagger2(true); // 开启 swagger2 模式
gc.setBaseResultMap(true); // XML 开启 BaseResultMap
gc.setBaseColumnList(true); // XML 开启 baseColumnList
gc.setEntityName("%s"); // 实体命名方式
gc.setMapperName("%sMapper"); // mapper 命名方式
gc.setXmlName("%sMapper"); // Mapper xml 命名方式
gc.setServiceName("%sService"); // service 命名方式
gc.setServiceImplName("%sServiceImpl"); // service impl 命名方式
gc.setControllerName("%sController"); // controller 命名方式
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://IP:端口/数据库名?useUnicode=true&useSSL=true&characterEncoding=utf8&allowPublicKeyRetrieval=true"); // mysql url
dsc.setDriverName("com.mysql.cj.jdbc.Driver"); // mysql 驱动名
dsc.setUsername("root"); // mysql 用户名
dsc.setPassword("root"); // mysql 密码
dsc.setDbType(DbType.MYSQL); // 数据库类型
mpg.setDataSource(dsc);
// 包名配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.baidu"); //父包名
pc.setModuleName("user"); // 父包模块名
// pc.setEntity(""); // Entity包名
// pc.setService(""); // Service包名
// pc.setServiceImpl(""); // Service Impl包名
// pc.setMapper(""); // Mapper包名
// pc.setXml(""); // Mapper XML包名
// pc.setPathInfo(); // 路径配置信息
mpg.setPackageInfo(pc);
// 数据库表配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("account"); // 需要生成的表名
// strategy.setExclude("account"); // 需要排除的表名
// strategy.setTablePrefix("t"); //生成实体时去掉表前缀
// strategy.setFieldPrefix("legal_finished_trans_"); //生成实体时去掉表后缀
strategy.setChainModel(true); // 链式模型
strategy.setEntityLombokModel(true); //是否生成lombok注解
strategy.setRestControllerStyle(true); //生成 @RestController 控制器
strategy.setNaming(NamingStrategy.underline_to_camel); // 数据库表名与类名映射
strategy.setColumnNaming(NamingStrategy.underline_to_camel); // 数据库字段名与属性映射
strategy.setControllerMappingHyphenStyle(true); // 驼峰转连字符
strategy.setEntityTableFieldAnnotationEnable(true); // 生成字段注解
// 自动注入表字段
TableFill create_time = new TableFill("create_time", FieldFill.INSERT);//设置时的生成策略
TableFill update_time = new TableFill("update_time", FieldFill.INSERT_UPDATE);//设置更新时间的生成策略
strategy.setTableFillList(Arrays.asList(create_time, update_time)); // 自动注入表字段
mpg.setStrategy(strategy);
// 配置模板
// TemplateConfig templateConfig = new TemplateConfig();
// templateConfig.setEntity(""); // Java 实体类模板
// templateConfig.setMapper(""); // mapper 模板
// templateConfig.setXml(null); // mapper xml 模板
// templateConfig.setService(""); // Service 类模板
// templateConfig.setServiceImpl(""); // Service impl 实现类模板
// templateConfig.setController(""); // controller 控制器模板
// mpg.setTemplate(templateConfig);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
// 自定义输出配置
// String templatePath = "/templates/mapper.xml.ftl";
// 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);
mpg.execute();
System.out.println("完成");
}
}
MyBatis-Plus 代码生成器模板的更多相关文章
- 0120 springboot集成Mybatis和代码生成器
在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...
- 修改mybatis plus Generator模板生成字段注释枚举常量
修改mybatis plus Generator模板生成字段注释枚举常量 本文基于最新的mybatis-plus 3.0.1版本源码修改,如果使用其它版本,处理方式也类似,主要是生成Entity的Fr ...
- Java快速开发平台,JEECG 3.7.7闪电版本发布,增加多套主流UI代码生成器模板
JEECG 3.7.7 闪电版本发布,提供5套主流UI代码生成器模板 导读 ⊙平台性能优化,速度闪电般提升 ⊙提供5套新的主流UI代码生成器模板(Bootstrap表单+Boots ...
- mybatis配eclise模板,mybatis快速生成模板
eclipse中mybatis得mapper文件不提示(mybatis-3-mapper.dtd,mybatis-3-config.dtd) 1.下载该文件到你的硬盘文件夹下 2.windows -- ...
- 想做时间管理大师?你可以试试Mybatis Plus代码生成器
1. 前言 对于写Crud的老司机来说时间非常宝贵,一些样板代码写不但费时费力,而且枯燥无味.经常有小伙伴问我,胖哥你怎么天天那么有时间去搞新东西,透露一下秘诀呗. 好吧,今天就把Mybatis-pl ...
- 手把手教你Spring Boot整合Mybatis Plus 代码生成器
一.在pom.xml中添加所需依赖 <!-- MyBatis-Plus代码生成器--> <dependency> <groupId>com.baomidou< ...
- MVC使用的MetaModel代码生成器模板
代码生成器能使从一些重复的工作中缓解下来 在最近开发MVC项目中使用到了MetaModel用来设定Model的显示名称,数据限制的代码生成模板,自己第一做代码生成模板还有很多缺陷. 下面是模板代码: ...
- JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎
上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建.运行与配置>,从上篇博客的内容我们不难看出SpringBoot的便捷.本篇博客我们继续在上篇博客的基础上来看一下Spri ...
- mybatis自定义代码生成器(Generator)——自动生成model&dao代码
花了两天的时间研究了下mybatis的generator大体了解了其生成原理以及实现过程.感觉generator做的非常不错,给开发者也留足了空间.看完之后在generator的基础上实现了自定义的生 ...
随机推荐
- Thinkphp 生成的验证码不显示问题解决
在调用验证码之前加上 ob_clean(); 将: public function verify(){ $verify = new \Think\Verify(); ...
- 如何在windows 11中安装WSLG(WSL2)
什么是 WSL WSL(Windows Subsystem for Linux):Windows 系统中的一个子系统,在这个子系统上可以运行 Linux 操作系统. 可以让开发人员直接在 Window ...
- C++调试总结
一.参考: 本文主要参考<C++编程调试秘笈>一书. 在编写C++代码时,我们不应该自己捕捉缺陷,而是由编译器和可执行代码为我们做这些事情,该书便提供了这样的一个思考.作者以"调 ...
- 根据JavaScript中原生的XMLHttpRequest实现jQuery的Ajax
基本介绍 XmlHttpRequest XmlHttpRequest是JavaScript中原生的,历史悠久的一种发送网络请求的方案. 基本上所有前端框架对于网络请求的部分都是基于它来完成的. 在本章 ...
- Vue 脚手架学习
首先就是安装脚手架 npm install @vue/cil -g 全局安装 在这里我遇到一个问题:安装不了脚手架,报错显示: 通过苦逼的查找原因就是 以前使用的taobao镜像 导致的,删除镜像换成 ...
- RHCE_DAY05
cron周期性计划任务 cron周期性计划任务用来定期执行程序,目前最主要的用途是定期备份数据 软件包名:cronie.crontabs 服务名:crond 日志文件:/var/log/cron cr ...
- 批量删除gmail邮件
以删除tor.com发送的邮件为例说明. 首先点击邮件搜索框右边的三角,在"发件人"下面写上"tor.com": 点"搜索"按钮,看一下范围 ...
- Fluid + GooseFS 助力云原生数据编排与加速快速落地
前言 Fluid 作为基于 Kubernetes 开发的面向云原生存算分离场景下的数据调度和编排加速框架,已于近期完成了 v0.6.0 版本的正式发布.腾讯云容器 TKE 团队一直致力于参与 Flui ...
- SpringBoot开发二十二-统一处理异常
需求介绍 首先服务端分为三层:表现层,业务层,数据层. 请求过来先到表现层,表现层调用业务层,然后业务层调用数据层. 那么数据层出现异常它会抛出异常,那异常肯定是抛给调用者也就是业务层,那么业务层会再 ...
- STM32—TIMx实现编码器四倍频
文章目录 一.储备知识 二.TIMx的编码器模式介绍 1.计数边沿设置 2.选择极性和使能 3.使能 4.计数方向 三.代码部分 一.储备知识 通过STM32的定时器编码器接口模式对编码器进行四倍频, ...