add file
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
package com.demo.util;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.annotation.FieldFill;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.generator.AutoGenerator;
|
||||
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
|
||||
import com.baomidou.mybatisplus.generator.config.*;
|
||||
import com.baomidou.mybatisplus.generator.config.converts.MySqlTypeConvert;
|
||||
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
|
||||
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
|
||||
import com.baomidou.mybatisplus.generator.engine.BeetlTemplateEngine;
|
||||
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
|
||||
import com.demo.DataSource;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class CodeGenerator {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(CodeGenerator.class);
|
||||
|
||||
private final static String[] TABLE_ARRAY = {"tb_pluss_merc_member_set"};
|
||||
|
||||
private final static String MODULE_NAME = "code-generator";
|
||||
|
||||
/**
|
||||
* 表名前缀
|
||||
*/
|
||||
private final static String TABLE_PREFIX = "tb_pluss";
|
||||
|
||||
private final static String AUTHOR = "Djh";
|
||||
|
||||
private final static String PACKAGE_PREFIX = "cn.pluss.platform";
|
||||
|
||||
/**
|
||||
* 模板路径
|
||||
*/
|
||||
private final static String ENTITY = "templates/entity.java";
|
||||
private final static String MAPPER = "templates/mapper.java";
|
||||
// private final static String SERVICE = "templates/service.java";
|
||||
// private final static String SERVICE_IMPL = "templates/serviceImpl.java";
|
||||
private final static String CONTROLLER = "templates/controller.java";
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
YamlUtils yamlUtils = new YamlUtils();
|
||||
DataSource dataSource = yamlUtils.readDefaultYaml();
|
||||
|
||||
String projectPath = System.getProperty("user.dir") + "/" + MODULE_NAME;// 获取项目根目录
|
||||
|
||||
BeetlTemplateEngine beetlTemplateEngine = new BeetlTemplateEngine();
|
||||
|
||||
FastAutoGenerator.create(dataSource.getUrl(), dataSource.getUsername(), dataSource.getPassword())
|
||||
.templateEngine(beetlTemplateEngine)
|
||||
.globalConfig(builder -> {
|
||||
builder.author(AUTHOR) // 设置作者
|
||||
.outputDir(projectPath + "/src/main/java"); // 指定输出目录
|
||||
})
|
||||
.packageConfig(builder -> {
|
||||
builder.parent(PACKAGE_PREFIX) // 设置父包模块名
|
||||
.pathInfo(Collections.singletonMap(OutputFile.xml, projectPath + "/src/main/resources/mapper")); // 设置mapperXml生成路径
|
||||
})
|
||||
.strategyConfig(builder -> {
|
||||
builder.addInclude(TABLE_ARRAY) // 设置需要生成的表名
|
||||
.addTablePrefix(TABLE_PREFIX) // 设置过滤表前缀
|
||||
.entityBuilder().enableLombok()
|
||||
.serviceBuilder().formatServiceFileName("%sService")
|
||||
.controllerBuilder().enableRestStyle();
|
||||
})
|
||||
.execute();
|
||||
|
||||
logger.debug("执行生成");
|
||||
// 执行生成
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.demo.util;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @author Djh
|
||||
*/
|
||||
@Data
|
||||
public class TableFieldEntity {
|
||||
|
||||
private String name;
|
||||
|
||||
private String type;
|
||||
}
|
||||
33
code-generator/src/main/java/com/demo/util/TableReader.java
Normal file
33
code-generator/src/main/java/com/demo/util/TableReader.java
Normal file
@@ -0,0 +1,33 @@
|
||||
package com.demo.util;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Data
|
||||
public class TableReader {
|
||||
|
||||
private String url;
|
||||
|
||||
private String username;
|
||||
|
||||
private String password;
|
||||
|
||||
private String driverClassName;
|
||||
|
||||
public Map<String, Map<String, TableFieldEntity>> readTable(String... tableNames) {
|
||||
|
||||
Map<String, Map<String, TableFieldEntity>> result = new HashMap<>();
|
||||
Map<String, TableFieldEntity> tableMap = new HashMap<>();
|
||||
for (String tableName : tableNames) {
|
||||
tableMap = new HashMap<>();
|
||||
result.put(tableName, tableMap);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
36
code-generator/src/main/java/com/demo/util/YamlUtils.java
Normal file
36
code-generator/src/main/java/com/demo/util/YamlUtils.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.demo.util;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.demo.DataSource;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.net.URL;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author Djh
|
||||
*/
|
||||
public class YamlUtils {
|
||||
|
||||
public DataSource readDefaultYaml() throws FileNotFoundException {
|
||||
Yaml yaml = new Yaml();
|
||||
|
||||
URL url = this.getClass().getClassLoader().getResource("application.yml");
|
||||
if (url != null) {
|
||||
//获取test.yaml文件中的配置数据,然后转换为obj,
|
||||
//也可以将值转换为Map
|
||||
Map<String, Object> map = yaml.load(new FileInputStream(url.getFile()));
|
||||
System.out.println(map);
|
||||
return JSONObject.parseObject(JSONObject.toJSONString(map), DataSource.class);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws FileNotFoundException {
|
||||
YamlUtils yamlUtils = new YamlUtils();
|
||||
yamlUtils.readDefaultYaml();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user