mybatis-plus自動生成最下位コード
4510 ワード
package com.zhm.blog.util;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import java.util.ArrayList;
import java.util.List;
/**
* mybatis-plus
* AutoGenerator Entity、Mapper、Mapper XML、Service、Controller
*/
public class CodeGenerator {
@Autowired
private Environment env;//
public static void createClasses(String tableName){
//
AutoGenerator mpg = new AutoGenerator();
//
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");//
gc.setOutputDir(projectPath + "/src/main/java");// :D
gc.setFileOverride(false);// false
gc.setAuthor("zhm");//
gc.setOpen(false); // true
gc.setEnableCache(false);// xml false
gc.setIdType(null);// ID null
mpg.setGlobalConfig(gc);
//
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/cms-shangtang?serverTimezone=UTC");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);
//
PackageConfig pc = new PackageConfig();
pc.setModuleName(""); //
pc.setParent("com.zhm.blog");// 。 , ,
pc.setEntity("entity");//Entity
pc.setService("service");//Service
pc.setServiceImpl("serviceImpl");//Service Impl
pc.setMapper("mapper");//Mapper
pc.setXml("xml");//Mapper XML
pc.setController("controller");//Controller
mpg.setPackageInfo(pc);
//
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
// to do nothing
}
};
List focList = new ArrayList<>();
focList.add(new FileOutConfig("/templates/mapper.xml.ftl") {
@Override
public String outputFile(TableInfo tableInfo) {
//
return projectPath + "/src/main/resources/xml/" + pc.getModuleName()
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));
// ( )
StrategyConfig strategy = new StrategyConfig()
.entityTableFieldAnnotationEnable(true);// ,
strategy.setNaming(NamingStrategy.underline_to_camel);//
strategy.setColumnNaming(NamingStrategy.underline_to_camel);// , naming
strategy.setEntityLombokModel(true); //【 】 lombok ( false)
strategy.setRestControllerStyle(true);// @RestController
// strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");// Entity ,
// strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");// Controller ,
strategy.setInclude(tableName);// , ( exclude )
// strategy.setSuperEntityColumns("id");// Entity ,
strategy.setControllerMappingHyphenStyle(true);//
// strategy.setTablePrefix(pc.getModuleName() + "_");//
strategy.setLogicDeleteFieldName("del_flag");//
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}
public static void main(String[] args) {
createClasses("user");
}
}