非常好用的mybatis-plus代码生成器
新人发帖这是本人在公司实习时发现公司所用的这个代码生成器,是基于mybatis-plus代码生成器,可以生成controller,server以及dao相关东西,非常好用,使用也很简单,“开袋即用”’{:301_997:}。GenInfo:代码生成配置类,包含数据源,包地址信息。
GenInfoBuikder:构建器,并未使用
MybatisGenUtils: 代码生成工具类,核心方法所在。 通过UtilCollection工具类连接数据库,获取并展示数据库中的所有表格,然后键盘数据表格名后回车即可生成代码(支持多表同时生成"," 分割)
package com.common.generator.model;
import com.common.generator.utils.MybatisGenUtils;
/**
* 代码生成配置
*
* @author: LEIYU
*/
public class GenInfo {
private static final String CONSTRUCT = "/src/main/java";
/**
* 模块绝对路径 (模块右键 Copy Path)
*/
private String projectPath = "/Users/xxx/xxx/xxx-user/xxx-user-rest";
/**
* 作者
*/
private String author = "YLJUN";
/**
* 数据库连接
*/
private String url = "jdbc:mysql://localhost:3306/bishe?useUnicode=true&useSSL=false&characterEncoding=utf8";
private String driverName = "com.mysql.jdbc.Driver";
private String userName = "root";
private String password = "123456";
private String dbName = "bishe";
/**
* 是否生成 controller 与 service
*/
private Boolean genController = true;
private Boolean genService = true;
/**
* controller service 包名
*/
private String controllerPackageName = "com.xx.xx.user.rest.controller";
private String servicePackageName = "com.xx.xx.user.rest.service";
private String serviceImplPackageName;
/**
* dao包名
*/
private String entityPackageName = "com.xx.xx.user.rest.model.pojo";
private String mapperPackageName = "com.xx.xx.user.rest.dao.mapper";
private String xmlPackageName = "com.xx.xx.user.rest.dao.mapping";
/**
* 表前缀
*/
private String tablePrefix = "eb_";
/**
* 是否覆盖已存在的文件
*/
private Boolean overrideExistFile = true;
public String getProjectPath() {
return projectPath + CONSTRUCT;
}
public String getServiceImplPackageName() {
return servicePackageName + ".impl";
}
public String getDbName() {
return dbName;
}
public String getAuthor() {
return author;
}
public String getUrl() {
return url;
}
public String getDriverName() {
return driverName;
}
public String getUserName() {
return userName;
}
public String getPassword() {
return password;
}
public Boolean getGenController() {
return genController;
}
public Boolean getGenService() {
return genService;
}
public String getControllerPackageName() {
return controllerPackageName;
}
public String getServicePackageName() {
return servicePackageName;
}
public String getEntityPackageName() {
return entityPackageName;
}
public String getMapperPackageName() {
return mapperPackageName;
}
public String getXmlPackageName() {
return xmlPackageName;
}
public String getTablePrefix() {
return tablePrefix;
}
public Boolean getOverrideExistFile() {
return overrideExistFile;
}
public GenInfo setProjectPath(String projectPath) {
this.projectPath = projectPath;
return this;
}
public GenInfo setAuthor(String author) {
this.author = author;
return this;
}
public GenInfo setUrl(String url) {
this.url = url;
return this;
}
public GenInfo setDriverName(String driverName) {
this.driverName = driverName;
return this;
}
public GenInfo setUserName(String userName) {
this.userName = userName;
return this;
}
public GenInfo setPassword(String password) {
this.password = password;
return this;
}
public GenInfo setGenController(Boolean genController) {
this.genController = genController;
return this;
}
public GenInfo setGenService(Boolean genService) {
this.genService = genService;
return this;
}
public GenInfo setControllerPackageName(String controllerPackageName) {
this.controllerPackageName = controllerPackageName;
return this;
}
public GenInfo setServicePackageName(String servicePackageName) {
this.servicePackageName = servicePackageName;
return this;
}
public GenInfo setServiceImplPackageName(String serviceImplPackageName) {
this.serviceImplPackageName = serviceImplPackageName;
return this;
}
public GenInfo setEntityPackageName(String entityPackageName) {
this.entityPackageName = entityPackageName;
return this;
}
public GenInfo setMapperPackageName(String mapperPackageName) {
this.mapperPackageName = mapperPackageName;
return this;
}
public GenInfo setXmlPackageName(String xmlPackageName) {
this.xmlPackageName = xmlPackageName;
return this;
}
public GenInfo setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
return this;
}
public GenInfo setOverrideExistFile(Boolean overrideExistFile) {
this.overrideExistFile = overrideExistFile;
return this;
}
public GenInfo setDbName(String dbName) {
this.dbName = dbName;
return this;
}
public static GenInfo build() {
return new GenInfo();
}
public void over() {
MybatisGenUtils.codeCreate(this);
}
}
/**
* 代码生成
*
* @author: LEIYU
*/
public class MybatisGenUtils {
/**
* 读取控制台内容
*/
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 (UtilCollection.isNotEmpty(ipt)) {
return ipt;
}
}
throw new MybatisPlusException("请输入正确的" + tip + "!");
}
/**
* 代码生成
*
* @Param genInfo
*/
public static void codeCreate(GenInfo genInfo) {
System.out.println("-------开始生成代码-------");
// 代码生成器
AutoGenerator mpg = new AutoGenerator();
// 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setFileOverride(genInfo.getOverrideExistFile());
gc.setEnableCache(false);
gc.setBaseResultMap(true);
gc.setBaseColumnList(true);
//输入路径
gc.setOutputDir(genInfo.getProjectPath());
//作者
gc.setAuthor(genInfo.getAuthor());
gc.setOpen(false);
// gc.setSwagger2(true); 实体属性 Swagger2 注解
mpg.setGlobalConfig(gc);
// 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl(genInfo.getUrl());
dsc.setDriverName(genInfo.getDriverName());
dsc.setUsername(genInfo.getUserName());
dsc.setPassword(genInfo.getPassword());
mpg.setDataSource(dsc);
//自定义配置,必须配置,不然报错
mpg.setCfg(new InjectionConfig() {
@Override
public void initMap() {
//空的
}
});
// 包配置
PackageConfig pc = new PackageConfig();
pc.setParent(null);
pc.setController(genInfo.getGenController() ? genInfo.getControllerPackageName() : "TTT");
pc.setService(genInfo.getGenService() ? genInfo.getServicePackageName() : "TTT");
pc.setServiceImpl(genInfo.getGenService() ? genInfo.getServiceImplPackageName() : "TTT");
//配置dao包名
pc.setEntity(genInfo.getEntityPackageName());
pc.setMapper(genInfo.getMapperPackageName());
pc.setXml(genInfo.getXmlPackageName());
mpg.setPackageInfo(pc);
// 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
// 此处可以修改为您的表前缀
strategy.setTablePrefix(new String[]{genInfo.getTablePrefix()});
strategy.setNaming(NamingStrategy.underline_to_camel);
showTables(dsc, genInfo.getDbName());
strategy.setInclude(scanner("表名,多个英文逗号分割").split(","));
mpg.setStrategy(strategy);
mpg.setTemplateEngine(new FreemarkerTemplateEngine());
mpg.execute();
destory(gc);
System.out.println("");
System.out.println("-------代码生成完成-------");
}
/**
* 删除不必要的代码
*/
public static void destory(GlobalConfig globalConfig) {
String outputDir = globalConfig.getOutputDir() + "/TTT";
UtilCollection.deleteDir(new File(outputDir));
}
/**
* 显示所有表名,这里查询了注释
*
* @param dsc 数据源配置
* @param dbName 数据库名
*/
public static void showTables(DataSourceConfig dsc, String dbName) {
try {
ResultSet resultSet = dsc.getConn().createStatement().executeQuery("select TABLE_NAME as tableName,TABLE_COMMENT as tableComment from information_schema.`TABLES` where TABLE_SCHEMA = '" + dbName + "'");
while (resultSet.next()) {
String tableName = resultSet.getString(1);
System.err.println(tableName);
}
System.err.println("---------------------------");
System.err.println("");
} catch (SQLException e) {
e.printStackTrace();
}
}
}
毒盘代码:https://pan.baidu.com/s/1EMu3I1fpIkrSgE984cBJZQ ctuf as849167276 发表于 2020-7-27 15:44
好像和我的差不多,conrtoller,service,mapper会不会自动生成分页的那些方法?page,list,query等基础方 ...
没有基础方法,mybatis-plus已经封装好了这些基础方法的,直接调用就行 yljungg 发表于 2020-7-27 15:50
没有基础方法,mybatis-plus已经封装好了这些基础方法的,直接调用就行
我也在用plus,分页那些不会自动生成,后面再研究一下自定义生成模板。 谢谢分享 这个很优秀,棒 好像和我的差不多,conrtoller,service,mapper会不会自动生成分页的那些方法?page,list,query等基础方法? 虽然不会用,但是支持一哈你吧 可以可以 学到了 感觉和 plus 官网生成的没什么区别啊 感谢分享
页:
[1]
2