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的基础上实现了自定义的生 ...
随机推荐
- 一键设置WPS_Office_2019专业版的定时自动备份的批处理文件
一键设置WPS_Office_2019专业版的定时自动备份的批处理文件 rem ================================================ rem 一键设置WPS ...
- CentOS 8.0 安装docker 报错:Problem: package docker-ce-3:19.03.4-3.el7.x86_64 requires containerd.io >= 1
1.错误内容 package docker-ce-3:19.03.2-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of th ...
- 两万字长文,彻底搞懂Kafka!
1.为什么有消息系统 1.解耦合 2.异步处理 例如电商平台,秒杀活动. 一般流程会分为: 风险控制 库存锁定 生成订单 短信通知 更新数据 通过消息系统将秒杀活动业务拆分开,将不急需处理的业务放在后 ...
- LaTex公式语法教程及手册(附emlogpro公式显示插件katex说明)
目录 第一列 第二列 第三列 效果 求和(使用\sum标签) 文本效果 本插件简介 积分(使用\int标签) 文本大小 LaTex是什么 空格 特殊符号 LaTex公式使用教程及手册 定界符 LaTe ...
- linux联网、远程
使用VMware虚拟机创建linux系统CentOS 7,连接网络以及远程Moba工具. 一.首先安装好VMware虚拟机和CentOS系统,网上很多教程,不在赘述. 二.打开VMware进行配置 1 ...
- S3C2440—6.串口的printf实现
文章目录 一.框架 二.printf函数原理 2.1 printf的声明 2.2 参数解读 2.3 如何得到可变参数的值 2.4 解决变参的宏定义 2.5 完成printf函数的封装 三.结合UART ...
- Python 读写文件的正确方式
当你用 Python 写程序时,不论是简单的脚本,还是复杂的大型项目,其中最常见的操作就是读写文件.不管是简单的文本文件.繁杂的日志文件,还是分析图片等媒体文件中的字节数据,都需要用到 Python ...
- 5、二进制安装K8s 之 部署kube-scheduler
二进制安装K8s之部署kube-scheduler 1.创建配置文件 cat > /data/k8s/config/kube-scheduler.conf << EOF KUBE_S ...
- Tcp三次握手中细节
TCP/IP协议中,TCP协议提供可靠的连接服务,采用三次握手建立一个连接,如下图所示.主机A为客户机,主机B为服务器 说明:(1)第一次握手:建立连接时,客户端A发送SYN包(SYN=j)到服务器B ...
- 梦琪小生 【转】【WPF】WPF MVVM 简单实例
1 新建WPF 应用程序WPFMVVMExample 程序结构如下图所示. 2 Model实现 在Model文件夹下新建业务类StudentModel(类文件StudentModel.cs),类的详细 ...