数据库类型:mysql
很多时候,需要创建大量的controller、service及impl、mapper、entity等类接口,特别是新项目起始阶段,随着数据库表的设计,然后代码上对业务接口、mapper接口、实体类等都需要一一映射对应创建,工作量就很大;大家会不会跟我一样会有这样的遐想,想着是否有能代替手动创建对应的类实体接口等的方法?比如一键生成?
有!并且我就已经用上了!今日前来就是想要当一波雷锋,做好事,不留名,救你于水火!免费送给大家一个超级超级超级好用的代码生成工具类,它不仅支持手动定义大量表创建对应的控制器等还对生成的文件能归类到自定义好的文件夹中,就算一张表,也是支持的哦;操作只需启动该工具类的main方法,就会自动生成对应的类接口实体,这你真值得拥有,不信你试试,不香你顺着网线爬过来敲爆我的"狗"头,好伐。
如果最后觉得该代码生成器对你有所帮助,还请不要吝啬你的赞哦,直接pia的点亮就完事了啦 up up up!!!
现在bug菌就把它分享出来,有需要的直接cv大法,赶紧拿走!助你早日解放双手!喜欢的记得关注收藏哦,安排ing!
package com.system.xiaoma.utils;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
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;
public class MyGenerator {
public static String scanner(String tip) {
Scanner scanner = new Scanner(System.in);
StringBuilder help = new StringBuilder();
help.append("请输入" + tip + ":");
System.out.println(help.toString());
if (scanner.hasNext()) {
String ipt = scanner.next();
if (StringUtils.isNotEmpty(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("luoyong");
gc.setOpen(false);
gc.setFileOverride(true);// 是否覆盖同名文件,默认是false
gc.setActiveRecord(true);// 不需要ActiveRecord特性的请改为false
gc.setEnableCache(false);// XML 二级缓存
gc.setBaseResultMap(true);// XML ResultMap 生成基本的resultmap
gc.setBaseColumnList(true);// XML columList 生成基本的sql片段
//实体属性 Swagger2 注解
gc.setSwagger2(true);
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/fk_system?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
//pc.setModuleName(scanner("模块名"));
pc.setParent("com.system.xiaoma");
pc.setEntity("entity");
pc.setMapper("mapper");
pc.setService("service");
pc.setServiceImpl("service.impl");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 自定义输出配置
List<FileOutConfig> focList = new ArrayList<>();
// 自定义配置会被优先输出
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
// 自定义输出文件名 , 如果你 Entity 设置了前后缀、此处注意 xml 的名称会跟着发生变化!!
return projectPath + "/src/main/resources/mapper/" + tableInfo.getEntityName() + "Mapper" +".xml";
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setSuperEntityClass("com.system.xiaoma.base.BaseEntity");
strategy.setEntityLombokModel(false);
strategy.setRestControllerStyle(true);
// 公共父类
strategy.setSuperControllerClass("com.system.xiaoma.base.BaseController");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
TemplateConfig tc = new TemplateConfig();
tc.setXml(null);
mpg.setTemplate(tc);
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
}
如下我给大家演示一下,确保所有小伙伴都能掌握:
1、启动main方法之后,在控制台会如下展示,按需输入要生成的数据库表名称,多表按英文逗号隔开;最后回车即可。
2、几秒就生成好了,最后一行会自动打印出文件生成完成!显示全部生成成功了。
3、你刷新下项目,生成的文件夹及文件都在。
如果不存在该表或者表名输入错了,会有如下提醒:
❤如果文章对您有所帮助,就请在文章末尾的左下角把大拇指点亮吧!(#^.^#);
❤如果喜欢bug菌分享的文章,就请给bug菌点个关注吧!(๑′ᴗ‵๑)づ╭❤~;
❤对文章有任何问题欢迎小伙伴们下方留言或者入群探讨【群号:708072830】;
❤鉴于个人经验有限,所有观点及技术研点,如有异议,请直接回复参与讨论(请勿发表攻击言论,谢谢);
❤版权声明:本文为博主原创文章,转载请附上原文出处链接和本文声明,版权所有,盗版必究!(*^▽^*).