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("完成");
}
}

最新文章

  1. OC 类别与扩展(匿名类别)
  2. 初步理解JNDI
  3. mac osx 快捷键符号以及意义 触发角:锁屏
  4. 数组机、局域网ip查找
  5. VMware 虚拟机使用 NAT 方式联网
  6. FTP远程命令集
  7. hdu 4271 动态规划
  8. java中float/double浮点数的计算失精度问题(转)
  9. RowSet的使用
  10. Nexus5/6刷 lineageos 过程
  11. Elasticsearch过滤器——filter
  12. 安装anaconda与tensorflow
  13. Jmeter如何把响应数据的结果保存到本地的一个文件
  14. docker-java Docker的java API
  15. C#用Infragistics 导入导出Excel(一)
  16. asp.net core中使用log4net
  17. Python(七)之OS模块
  18. WebService测试工具介绍及下载
  19. Map容器中keySet()、entrySet()
  20. 解决Tomcat出现内存溢出的问题

热门文章

  1. 网络安全日记 ① IIS 之web服务器搭建以及dns转发配置
  2. Mybatis学习笔记-缓存
  3. 模拟input type=file
  4. Jetpack Compose 1.0 终于要投入使用了!
  5. 算法竞赛中的常用JAVA API :HashSet 和 TreeSet(转载)
  6. 免费个人图床搭建gitee+PicGo
  7. Git-02-版本回退
  8. 渲染优化之CSS Containment
  9. .Net Core with 微服务 - 分布式事务 - TCC
  10. 【编程语言】Matlab 学习记录