java 代码生成器
Posted guxiaohai_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 代码生成器相关的知识,希望对你有一定的参考价值。
一:简介
根据指定数据结构表快捷生成代码
二:编写生成器
- 引入pom.xml文件包
<!--mybatis-generator-->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
- 逻辑编写
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
String generatorMoudleName = "codegenerator";
gc.setOutputDir(projectPath + "/" + generatorMoudleName);
gc.setAuthor(scanner("操作人名称"));
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 数据源配置
String datasourceStr = "jdbc:mysql://ip:port/数据库名?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC";
String username = "用户名";
String password = "密码";
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(datasourceStr);
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername(username);
dsc.setPassword(password);
mpg.setDataSource(dsc);
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent("com.vsofo");
mpg.setPackageInfo(pc);
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
// 如果模板引擎是 freemarker
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 + "/" + generatorMoudleName + "/xmlmapper/" + tableInfo.getEntityName()
+ "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
// 配置模板
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
mpg.setTemplate(templateConfig);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
// strategy.setSuperEntityClass("你自己的父类实体,没有就不用设置!");
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 公共父类
// strategy.setSuperControllerClass("你自己的父类控制器,没有就不用设置!");
// 写于父类中的公共字段
// strategy.setSuperEntityColumns("id");
strategy.setEntityTableFieldAnnotationEnable(true);
strategy.setInclude(scanner("表名,多个英文逗号分割").replaceAll("\\\\[|\\\\]", "").split(","));
strategy.setControllerMappingHyphenStyle(true);
strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
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) && !"".equals(ipt.trim())) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
三:执行效果
以上是关于java 代码生成器的主要内容,如果未能解决你的问题,请参考以下文章