直接上代码 已测可以正常使用
import com
.baomidou
.mybatisplus
.core
.exceptions
.MybatisPlusException
;
import com
.baomidou
.mybatisplus
.core
.toolkit
.StringPool
;
import com
.baomidou
.mybatisplus
.core
.toolkit
.StringUtils
;
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 java
.util
.ArrayList
;
import java
.util
.List
;
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 mpg
= new AutoGenerator();
GlobalConfig gc
= new GlobalConfig();
String projectPath
= System
.getProperty("user.dir");
gc
.setOutputDir(projectPath
+ "/src/main/java");
gc
.setAuthor("hqh");
gc
.setOpen(false);
mpg
.setGlobalConfig(gc
);
DataSourceConfig dsc
= new DataSourceConfig();
dsc
.setUrl("jdbc:mysql://localhost:3306/wmb?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai");
dsc
.setDriverName("com.mysql.cj.jdbc.Driver");
dsc
.setUsername("root");
dsc
.setPassword("root");
mpg
.setDataSource(dsc
);
PackageConfig pc
= new PackageConfig();
pc
.setParent("com.mlfo.wmb");
mpg
.setPackageInfo(pc
);
InjectionConfig cfg
= new InjectionConfig() {
@Override
public void initMap() {
}
};
String templatePath
= "/templates/mapper.xml.ftl";
List
<FileOutConfig> focList
= new ArrayList<>();
focList
.add(new FileOutConfig() {
@Override
public String
outputFile(TableInfo tableInfo
) {
return projectPath
+ "/src/main/resources/mapper/" + pc
.getModuleName()
+ "/" + 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
.setEntityLombokModel(true);
strategy
.setRestControllerStyle(true);
strategy
.setInclude(scanner("表名"));
strategy
.setControllerMappingHyphenStyle(true);
mpg
.setStrategy(strategy
);
mpg
.setTemplateEngine(new FreemarkerTemplateEngine());
mpg
.execute();
}
}
转载请注明原文地址: https://lol.8miu.com/read-22152.html