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 代码生成器模板的更多相关文章

  1. 0120 springboot集成Mybatis和代码生成器

    在日常开发中,数据持久技术使用的架子使用频率最高的有3个,即spring-jdbc , spring-jpa, spring-mybatis.详情可以看我之前的一篇文章spring操作数据库的3个架子 ...

  2. 修改mybatis plus Generator模板生成字段注释枚举常量

    修改mybatis plus Generator模板生成字段注释枚举常量 本文基于最新的mybatis-plus 3.0.1版本源码修改,如果使用其它版本,处理方式也类似,主要是生成Entity的Fr ...

  3. Java快速开发平台,JEECG 3.7.7闪电版本发布,增加多套主流UI代码生成器模板

    JEECG 3.7.7 闪电版本发布,提供5套主流UI代码生成器模板 导读 ⊙平台性能优化,速度闪电般提升           ⊙提供5套新的主流UI代码生成器模板(Bootstrap表单+Boots ...

  4. mybatis配eclise模板,mybatis快速生成模板

    eclipse中mybatis得mapper文件不提示(mybatis-3-mapper.dtd,mybatis-3-config.dtd) 1.下载该文件到你的硬盘文件夹下 2.windows -- ...

  5. 想做时间管理大师?你可以试试Mybatis Plus代码生成器

    1. 前言 对于写Crud的老司机来说时间非常宝贵,一些样板代码写不但费时费力,而且枯燥无味.经常有小伙伴问我,胖哥你怎么天天那么有时间去搞新东西,透露一下秘诀呗. 好吧,今天就把Mybatis-pl ...

  6. 手把手教你Spring Boot整合Mybatis Plus 代码生成器

    一.在pom.xml中添加所需依赖 <!-- MyBatis-Plus代码生成器--> <dependency> <groupId>com.baomidou< ...

  7. MVC使用的MetaModel代码生成器模板

    代码生成器能使从一些重复的工作中缓解下来 在最近开发MVC项目中使用到了MetaModel用来设定Model的显示名称,数据限制的代码生成模板,自己第一做代码生成模板还有很多缺陷. 下面是模板代码: ...

  8. JavaEE开发之SpringBoot整合MyBatis以及Thymeleaf模板引擎

    上篇博客我们聊了<JavaEE开发之SpringBoot工程的创建.运行与配置>,从上篇博客的内容我们不难看出SpringBoot的便捷.本篇博客我们继续在上篇博客的基础上来看一下Spri ...

  9. mybatis自定义代码生成器(Generator)——自动生成model&dao代码

    花了两天的时间研究了下mybatis的generator大体了解了其生成原理以及实现过程.感觉generator做的非常不错,给开发者也留足了空间.看完之后在generator的基础上实现了自定义的生 ...

随机推荐

  1. GC相关问题

    为什么会有新生代? 如果不分代,所有对象全部在一个区域,每次GC都需要对全堆进行扫描,存在效率问题.分代后,可分别控制回收频率,并采用不同的回收算法,确保GC性能全局最优. 为什么新生代会采用复制算法 ...

  2. docker容器存储

    写在前面 我们在上篇学习了容器网络,对容器网络驱动bridge工作原理做了较为详细的介绍,今天小作文一起看看容器中另一个关键域-存储. 容器的存储可以分为两大类: 一种是与镜像相关的即我们在<d ...

  3. git config 配置简写命令

    在多人协作开发时,一般用git来进行代码管理. git有一些命令如:git pull . git push等等,这些命令可以设置alias,也就是缩写. 如:git pull 是 git pl, gi ...

  4. Nginx搭建与相关配置

    目录 一.Nginx简介 1.1 概述 1.2 Nginx与Apache的差异 二.编译安装Nginx服务 2.1 将nginx软件包传到主机/opt目录下 2.2.安装依赖包 2.3.添加模块编译安 ...

  5. ECDSA—模加减模块

    如果a,b GF(P),则加法运算a+b=r (mod p),其中r满足0<r<p-1,即a+b除以p的余数,该操作成为模p加法.对于模减运算可以视为另类的模加运算,即a+(-b)=k ( ...

  6. sqli-labs lesson 21-22

    less 21: username:admin password:admin 登录. 发现这里和之前不太一样.用到了base64加密而不是之前的明文了. 传送门:base64在线编码解码 所以要做的就 ...

  7. Linux 并发服务器编程(多进程)

    文章目录 说明 注意事项 server.c client.c 运行截图 说明 在Linux中通过流式套接字编程(TCP),实现一个并发服务器的访问回显,适合刚学完Linux套接字编程的朋友进行巩固训练 ...

  8. SQL 练习28

    查询平均成绩大于等于 85 的所有学生的学号.姓名和平均成绩 SELECT Student.SId,Student.Sname,平均成绩 FROM Student , (SELECT sid,AVG( ...

  9. NOIP 模拟 7 寿司

    题解 题目 这道题考试的时候直接打暴力,结果暴力连样例都过不了,最后放上去一个玄学东西,骗了 \(5pts\). 正解: 此题中我们可以看到原序列是一个环,所以我们要把它拆成一条链,那么我们需要暴力枚 ...

  10. NOIP 模拟 $25\; \rm string$

    题解 \(by\;zj\varphi\) 考虑对于母串的每个字符,它在匹配串中有多少前缀,多少后缀. 设 \(f_i\) 表示 \(i\) 位置匹配上的前缀,\(g_i\) 为后缀,那么答案为 \(\ ...