MyBatis-Plus代码生成器
package com.linyihan.util;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.mysql.cj.xdevapi.Table;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Scanner;
public class CodeGenerator {
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.isNotBlank(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
public static void main(String[] args) {
AutoGenerator autoGenerator = new AutoGenerator();
GlobalConfig globalConfig = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
globalConfig
.setOutputDir(projectPath+"/src/main/java")
.setAuthor("lin-yihan")
.setOpen(false)
.setFileOverride(false)
.setServiceName("%sService")
.setIdType(IdType.ID_WORKER)
.setDateType(DateType.ONLY_DATE)
.setSwagger2(true);
autoGenerator.setGlobalConfig(globalConfig);
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig
.setUsername("root")
.setPassword("admin")
.setDriverName("com.mysql.cj.jdbc.Driver")
.setUrl("jdbc:mysql://localhost:3306/library?userSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai")
.setDbType(DbType.MYSQL);
autoGenerator.setDataSource(dataSourceConfig);
PackageConfig packageConfig = new PackageConfig();
packageConfig
.setParent("com.linyihan")
.setEntity("entity")
.setMapper("mapper")
.setService("service")
.setController("controller");
autoGenerator.setPackageInfo(packageConfig);
StrategyConfig strategy = new StrategyConfig();
TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
TableFill updateTime = new TableFill("update_time",FieldFill.INSERT_UPDATE);
ArrayList<TableFill> tableFills = new ArrayList<>();
tableFills.add(createTime);
tableFills.add(updateTime);
strategy
.setInclude(scanner("表名,多个英文逗号分割").split(","))
.setNaming(NamingStrategy.underline_to_camel)
.setColumnNaming(NamingStrategy.underline_to_camel)
.setEntityLombokModel(true)
.setLogicDeleteFieldName("deleted")
.setTableFillList(tableFills)
.setVersionFieldName("version")
.setRestControllerStyle(true);
autoGenerator.setStrategy(strategy);
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(null);
autoGenerator.setTemplate(templateConfig);
autoGenerator.execute();
}
}