mybatis-plus-generator 自动生成代码
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
模板一
import com.baomidou.mybatisplus.generator.*;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.*;
import java.util.Objects;
public class MybatisPlusGeneratorApp {
private static final String ip = "127.0.0.1";
private static final String port = "3306";
private static final String database = "scott";
private static final String url = String.format("jdbc:mysql://%s:%s/%s?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai", ip, port, database);
private static final String driverName = "com.mysql.cj.jdbc.Driver";
private static final String username = "root";
private static final String password = "123456";
private static final String moduleName = null;
private static final String parentPackage = "xxx.yyy.zzz";
private static final String entityIn = "bean.po";
private static final String mapperIn = "dao";
private static final String xmlIn = "mappers";
private static final String serviceIn = "service";
private static final String tableNames = "department,employee";
public static void main(String[] args) {
try {
generator();
System.out.println("mybatis 代码生成成功");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void generator() {
AutoGenerator mpg = new AutoGenerator();
mpg.setGlobalConfig(globalConfig());
mpg.setDataSource(dataSourceConfig());
mpg.setPackageInfo(packageConfig());
mpg.setStrategy(strategyConfig());
mpg.setTemplate(templateConfig());
mpg.execute();
}
private static GlobalConfig globalConfig() {
String projectPath = System.getProperty("user.dir");
GlobalConfig globalConfig = new GlobalConfig();
if (Objects.equals(moduleName, null) || Objects.equals(moduleName.trim(), ""))
globalConfig.setOutputDir(projectPath + "/src/main/java");
else
globalConfig.setOutputDir(projectPath + "/" + moduleName + "/src/main/java");
globalConfig.setAuthor(System.getProperty("user.name"));
globalConfig.setDateType(DateType.TIME_PACK);
globalConfig.setOpen(false);
globalConfig.setEntityName("%sPo");
globalConfig.setMapperName("%sDao");
return globalConfig;
}
private static DataSourceConfig dataSourceConfig() {
DataSourceConfig dataSourceConfig = new DataSourceConfig();
dataSourceConfig.setUrl(url);
dataSourceConfig.setDriverName(driverName);
dataSourceConfig.setUsername(username);
dataSourceConfig.setPassword(password);
return dataSourceConfig;
}
private static PackageConfig packageConfig() {
PackageConfig packageConfig = new PackageConfig();
packageConfig.setParent(parentPackage);
packageConfig.setEntity(entityIn);
packageConfig.setMapper(mapperIn);
packageConfig.setXml(xmlIn);
return packageConfig;
}
private static StrategyConfig strategyConfig() {
StrategyConfig strategyConfig = new StrategyConfig();
strategyConfig.setNaming(NamingStrategy.underline_to_camel);
strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
strategyConfig.setEntityLombokModel(true);
strategyConfig.setRestControllerStyle(true);
strategyConfig.setInclude((tableNames).split(","));
strategyConfig.setControllerMappingHyphenStyle(true);
return strategyConfig;
}
private static TemplateConfig templateConfig() {
TemplateConfig templateConfig = new TemplateConfig();
templateConfig.setXml(ConstVal.TEMPLATE_XML);
templateConfig.setService(null);
templateConfig.setServiceImpl(null);
templateConfig.setController(null);
return templateConfig;
}
}
模板二(推荐、简单够用)
public void generator() {
String rootDir = "E:/generator";
String outputDir = rootDir + "/java";
String packageName = "xxx.yyy.zzz";
String mapperXmlDir = rootDir + "/resources/" + packageName.replaceAll("\\.", "/") + "/mapper";
String url =""
FastAutoGenerator.create(url, "root", "123456")
.globalConfig(builder -> {
builder.author("myname")
.fileOverride()
.outputDir(outputDir);
}).packageConfig(builder -> {
builder.parent(packageName)
.pathInfo(Collections.singletonMap(OutputFile.mapperXml, mapperXmlDir));
}).strategyConfig(builder -> {
builder.addInclude("id_departmant","test_employee")
.addTablePrefix("id_", "test_")
.entityBuilder().enableLombok()
.entityBuilder().columnNaming(NamingStrategy.no_change)
.controllerBuilder()
.enableRestStyle()
.mapperBuilder().enableMapperAnnotation();
}).templateEngine(new FreemarkerTemplateEngine())
.execute();
}