Mybatis-plus 代码生成器的使用

1.maven 引入依赖


com.baomidou
mybatis-plus-generator
3.2.0


org.apache.velocity
velocity-engine-core
2.1




org.freemarker
freemarker
2.3.28

  1. 添加配置函数

public class CodeGenerator {
public static void main(String[] args) {
// 代码生成器
AutoGenerator mpg = new AutoGenerator();

// 全局配置
GlobalConfig gc = new GlobalConfig();
final String projectPath = System.getProperty("user.dir");
gc.setOutputDir(projectPath + "/src/main/java");
gc.setAuthor("zhangtao");
gc.setOpen(false);
mpg.setGlobalConfig(gc);

// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/vueblog2?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL");
// dsc.setSchemaName("public");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
mpg.setDataSource(dsc);

// 包配置
PackageConfig pc = new PackageConfig();
//自定义模块名
final String moduleName="reward"; //自己定义模块名
pc.setModuleName(moduleName);
pc.setParent("com.example"); //自己定义包名
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/mapper/" + moduleName
+ "/" + tableInfo.getEntityName() + "Mapper" + StringPool.DOT_XML;
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);
mpg.setTemplate(new TemplateConfig().setXml(null));

// 策略配置(个性化定制)
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);

//实体类自动继承Entity,不需要也可以
//strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
//小辣椒默认为true
strategy.setEntityLombokModel(false);
strategy.setRestControllerStyle(true);
//控制层自动继承父类BaseController,不需要也可以
// strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
// //设置要被扫描的表名
// strategy.setInclude("mynewemp");
// strategy.setSuperEntityColumns("id");
// strategy.setControllerMappingHyphenStyle(true);
// strategy.setTablePrefix(pc.getModuleName() + "_");
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
}

}

posted @ 2019-08-28 14:19  dsad22  阅读(671)  评论(0)    收藏  举报