改进加载默认配置文件的方式

This commit is contained in:
makejava 2021-08-14 11:47:43 +08:00
parent b7fa4cc653
commit 033ffad049
19 changed files with 445 additions and 235 deletions

View File

@ -1,9 +1,12 @@
package com.sjhy.plugin.dto;
import com.intellij.ide.fileTemplates.impl.UrlUtil;
import com.intellij.util.ExceptionUtil;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.entity.*;
import com.sjhy.plugin.enums.ColumnConfigType;
import com.sjhy.plugin.tool.CollectionUtil;
import com.sjhy.plugin.tool.JSON;
import com.sjhy.plugin.tool.StringUtils;
import lombok.Data;
@ -26,6 +29,14 @@ public class SettingsStorageDTO {
* @return 储存对象
*/
public static SettingsStorageDTO defaultVal() {
try {
// 从配置文件中加载配置
String json = UrlUtil.loadText(SettingsStorageDTO.class.getResource("/defaultConfig.json"));
return JSON.parse(json, SettingsStorageDTO.class);
} catch (Exception e) {
ExceptionUtil.rethrow(e);
}
// 配置文件加载失败直接创建配置
SettingsStorageDTO storage = new SettingsStorageDTO();
storage.author = GlobalDict.AUTHOR;
storage.version = GlobalDict.VERSION;

View File

@ -0,0 +1,12 @@
[
{
"title": "disable",
"type": "BOOLEAN",
"selectValue": ""
},
{
"title": "support",
"type": "SELECT",
"selectValue": "add,edit,query,del,ui"
}
]

File diff suppressed because one or more lines are too long

View File

@ -1,88 +1,88 @@
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!define
#setPackageSuffix("controller")
#setTableSuffix("Controller")
#save("/controller", "Controller.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##定义服务名
#set($serviceSortType = $!tool.append($!tableInfo.name, "Service"))
#set($serviceType = $!tool.append($!tableInfo.savePackageName, ".service.", $serviceSortType))
#set($serviceVarName = $!tool.firstLowerCase($serviceSortType))
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import $serviceType;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* $!{tableInfo.comment}控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping("/$!tool.firstLowerCase($!tableInfo.name)")
@AllArgsConstructor
public class $!{tableName} {
private $serviceSortType $serviceVarName;
/**
* 获取$!{tableInfo.comment}列表(分页)
*/
@GetMapping("/list")
public Page<$entityShortType> list(Pageable page) {
return null;
}
/**
* 获取$!{tableInfo.comment}
*/
@GetMapping("/get")
public $entityShortType get($!pk.shortType id) {
return ${serviceVarName}.findById(id);
}
/**
* 添加$!{tableInfo.comment}
*/
@PostMapping("/add")
public void add(@RequestBody $entityShortType $entityVarName) {
${serviceVarName}.save($entityVarName);
}
/**
* 修改$!{tableInfo.comment}
*/
@PostMapping("/update")
public void update(@RequestBody $entityShortType $entityVarName) {
${serviceVarName}.save($entityVarName);
}
/**
* 删除$!{tableInfo.comment}
*/
@PostMapping("/delete")
public void delete($!pk.shortType id) {
${serviceVarName}.deleteById(id);
}
}
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!define
#setPackageSuffix("controller")
#setTableSuffix("Controller")
#save("/controller", "Controller.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##定义服务名
#set($serviceSortType = $!tool.append($!tableInfo.name, "Service"))
#set($serviceType = $!tool.append($!tableInfo.savePackageName, ".service.", $serviceSortType))
#set($serviceVarName = $!tool.firstLowerCase($serviceSortType))
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import $serviceType;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* $!{tableInfo.comment}控制层
*
* @author $!author
* @since $!time.currTime()
*/
@RestController
@RequestMapping("/$!tool.firstLowerCase($!tableInfo.name)")
@AllArgsConstructor
public class $!{tableName} {
private $serviceSortType $serviceVarName;
/**
* 获取$!{tableInfo.comment}列表(分页)
*/
@GetMapping("/list")
public Page<$entityShortType> list(Pageable page) {
return null;
}
/**
* 获取$!{tableInfo.comment}
*/
@GetMapping("/get")
public $entityShortType get($!pk.shortType id) {
return ${serviceVarName}.findById(id);
}
/**
* 添加$!{tableInfo.comment}
*/
@PostMapping("/add")
public void add(@RequestBody $entityShortType $entityVarName) {
${serviceVarName}.save($entityVarName);
}
/**
* 修改$!{tableInfo.comment}
*/
@PostMapping("/update")
public void update(@RequestBody $entityShortType $entityVarName) {
${serviceVarName}.save($entityVarName);
}
/**
* 删除$!{tableInfo.comment}
*/
@PostMapping("/delete")
public void delete($!pk.shortType id) {
${serviceVarName}.deleteById(id);
}
}

View File

@ -1,30 +1,30 @@
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!define
#setPackageSuffix("repository")
#setTableSuffix("Repository")
#save("/repository", "Repository.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##实体类名、主键类名
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* $!{tableInfo.comment}持久层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends MongoRepository<$entityShortType, $pkShortType> {
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!define
#setPackageSuffix("repository")
#setTableSuffix("Repository")
#save("/repository", "Repository.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##实体类名、主键类名
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* $!{tableInfo.comment}持久层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} extends MongoRepository<$entityShortType, $pkShortType> {
}

View File

@ -1,44 +1,44 @@
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!define
#setPackageSuffix("service")
#setTableSuffix("Service")
#save("/service", "Service.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##实体类名、主键类名
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Collection;
import java.util.List;
/**
* $!{tableInfo.comment}业务层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} {
void save($entityShortType $entityVarName);
void deleteById($pkShortType id);
$entityShortType findById($pkShortType id);
List<$entityShortType> findById(Collection<$pkShortType> ids);
Page<$entityShortType> list(Pageable page);
##导入宏定义、设置包名、类名、文件名##导入宏定义
$!define
#setPackageSuffix("service")
#setTableSuffix("Service")
#save("/service", "Service.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##实体类名、主键类名
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Collection;
import java.util.List;
/**
* $!{tableInfo.comment}业务层
*
* @author $!author
* @since $!time.currTime()
*/
public interface $!{tableName} {
void save($entityShortType $entityVarName);
void deleteById($pkShortType id);
$entityShortType findById($pkShortType id);
List<$entityShortType> findById(Collection<$pkShortType> ids);
Page<$entityShortType> list(Pageable page);
}

View File

@ -1,76 +1,76 @@
##导入宏定义、设置包名、类名、文件名
$!define
#setPackageSuffix("service.impl")
#setTableSuffix("ServiceImpl")
#save("/service/impl", "ServiceImpl.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##业务层类名、持久层类名、实体名
#set($serviceSortType = $!tool.append($!tableInfo.name, "Service"))
#set($serviceType = $!tool.append($!tableInfo.savePackageName, ".service.", $serviceSortType))
#set($repositorySortType = $!tool.append($!tableInfo.name, "Repository"))
#set($repositoryType = $!tool.append($!tableInfo.savePackageName, ".repository.", $repositorySortType))
#set($repositoryVarName = $!tool.firstLowerCase($!repositorySortType))
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import $serviceType;
import $repositoryType;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* $!{tableInfo.comment}业务层
*
* @author $!author
* @since $!time.currTime()
*/
@Service
public class $!{tableName} implements $!serviceSortType {
@Resource
private $repositorySortType $repositoryVarName;
@Override
public void save($entityShortType $entityVarName) {
$!{repositoryVarName}.save($entityVarName);
}
@Override
public void deleteById($pkShortType id) {
$!{repositoryVarName}.delete(id);
}
@Override
public $entityShortType findById($pkShortType id) {
return $!{repositoryVarName}.findOne(id);
}
@Override
public List<$entityShortType> findById(Collection<$pkShortType> ids) {
Iterable<$entityShortType> iterable = $!{repositoryVarName}.findAll(ids);
return StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
}
@Override
public Page<$entityShortType> list(Pageable page) {
return $!{repositoryVarName}.findAll(page);
}
##导入宏定义、设置包名、类名、文件名
$!define
#setPackageSuffix("service.impl")
#setTableSuffix("ServiceImpl")
#save("/service/impl", "ServiceImpl.java")
##拿到主键
#if(!$tableInfo.pkColumn.isEmpty())
#set($pk = $tableInfo.pkColumn.get(0))
#end
##业务层类名、持久层类名、实体名
#set($serviceSortType = $!tool.append($!tableInfo.name, "Service"))
#set($serviceType = $!tool.append($!tableInfo.savePackageName, ".service.", $serviceSortType))
#set($repositorySortType = $!tool.append($!tableInfo.name, "Repository"))
#set($repositoryType = $!tool.append($!tableInfo.savePackageName, ".repository.", $repositorySortType))
#set($repositoryVarName = $!tool.firstLowerCase($!repositorySortType))
#set($entityShortType = $!tableInfo.name)
#set($entityType = $!tableInfo.psiClassObj.getQualifiedName())
#set($entityVarName = $!tool.firstLowerCase($!tableInfo.name))
#set($pkShortType = $!pk.shortType)
#set($pkType = $!pk.type)
import $pkType;
import $entityType;
import $serviceType;
import $repositoryType;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
/**
* $!{tableInfo.comment}业务层
*
* @author $!author
* @since $!time.currTime()
*/
@Service
public class $!{tableName} implements $!serviceSortType {
@Resource
private $repositorySortType $repositoryVarName;
@Override
public void save($entityShortType $entityVarName) {
$!{repositoryVarName}.save($entityVarName);
}
@Override
public void deleteById($pkShortType id) {
$!{repositoryVarName}.delete(id);
}
@Override
public $entityShortType findById($pkShortType id) {
return $!{repositoryVarName}.findOne(id);
}
@Override
public List<$entityShortType> findById(Collection<$pkShortType> ids) {
Iterable<$entityShortType> iterable = $!{repositoryVarName}.findAll(ids);
return StreamSupport.stream(iterable.spliterator(), false)
.collect(Collectors.toList());
}
@Override
public Page<$entityShortType> list(Pageable page) {
return $!{repositoryVarName}.findAll(page);
}
}

View File

@ -0,0 +1,62 @@
[
{
"matchType": "REGEX",
"columnType": "varchar(\\(\\d+\\))?",
"javaType": "java.lang.String"
},
{
"matchType": "REGEX",
"columnType": "char(\\(\\d+\\))?",
"javaType": "java.lang.String"
},
{
"matchType": "REGEX",
"columnType": "(tiny|medium|long)*text",
"javaType": "java.lang.String"
},
{
"matchType": "REGEX",
"columnType": "decimal(\\(\\d+,\\d+\\))?",
"javaType": "java.lang.Double"
},
{
"matchType": "ORDINARY",
"columnType": "integer",
"javaType": "java.lang.Integer"
},
{
"matchType": "REGEX",
"columnType": "(tiny|small|medium)*int(\\(\\d+\\))?",
"javaType": "java.lang.Integer"
},
{
"matchType": "ORDINARY",
"columnType": "int4",
"javaType": "java.lang.Integer"
},
{
"matchType": "ORDINARY",
"columnType": "int8",
"javaType": "java.lang.Long"
},
{
"matchType": "REGEX",
"columnType": "bigint(\\(\\d+\\))?",
"javaType": "java.lang.Long"
},
{
"matchType": "ORDINARY",
"columnType": "datetime",
"javaType": "java.util.Date"
},
{
"matchType": "ORDINARY",
"columnType": "timestamp",
"javaType": "java.util.Date"
},
{
"matchType": "ORDINARY",
"columnType": "boolean",
"javaType": "java.lang.Boolean"
}
]

View File

@ -0,0 +1,124 @@
package com.sjhy.plugin;
import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.io.FileUtilRt;
import com.sjhy.plugin.dict.GlobalDict;
import com.sjhy.plugin.dto.SettingsStorageDTO;
import com.sjhy.plugin.entity.*;
import com.sjhy.plugin.tool.JSON;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
/**
* 生成默认配置测试
* 由于resources目录下的配置文件不便与遍历利用测试用例提前遍历并生成配置文件运行时再直接加载配置
*
* @author makejava
* @version 1.0.0
* @date 2021/08/14 11:11
*/
public class GenerateDefaultConfigTest {
@Test
public void generate() throws IOException {
SettingsStorageDTO settingsStorage = new SettingsStorageDTO();
settingsStorage.setAuthor(GlobalDict.AUTHOR);
settingsStorage.setVersion(GlobalDict.VERSION);
settingsStorage.setUserSecure("");
File templateDir = new File(GenerateDefaultConfigTest.class.getResource("/template").getFile());
loadTemplate(settingsStorage, templateDir);
File globalConfigDir = new File(GenerateDefaultConfigTest.class.getResource("/globalConfig").getFile());
loadGlobalConfig(settingsStorage, globalConfigDir);
File typeMapperDir = new File(GenerateDefaultConfigTest.class.getResource("/typeMapper").getFile());
loadTypeMapper(settingsStorage, typeMapperDir);
File columnConfigDir = new File(GenerateDefaultConfigTest.class.getResource("/columnConfig").getFile());
loadColumnConfig(settingsStorage, columnConfigDir);
String json = JSON.toJson(settingsStorage);
// 所有的换行符号均改为\n
// 1.windows处理
json = json.replace("\\r\\n", "\\n");
// 2.mac处理
json = json.replace("\\r", "\\n");
FileUtil.writeToFile(new File(GenerateDefaultConfigTest.class.getResource("/").getFile().replace("out", "src").replace("test", "main").replace("classes", "resources") + "defaultConfig.json"), json);
}
private void loadTemplate(SettingsStorageDTO settingsStorage, File root) throws IOException {
settingsStorage.setCurrTemplateGroupName(GlobalDict.DEFAULT_GROUP_NAME);
settingsStorage.setTemplateGroupMap(new HashMap<>(root.listFiles().length));
for (File dir : root.listFiles()) {
TemplateGroup templateGroup = new TemplateGroup();
templateGroup.setName(dir.getName());
templateGroup.setElementList(new ArrayList<>());
if (dir.isDirectory()) {
for (File file : dir.listFiles()) {
Template template = new Template();
template.setName(file.getName());
template.setCode(FileUtilRt.loadFile(file));
templateGroup.getElementList().add(template);
}
}
settingsStorage.getTemplateGroupMap().put(templateGroup.getName(), templateGroup);
}
}
private void loadGlobalConfig(SettingsStorageDTO settingsStorage, File root) throws IOException {
settingsStorage.setCurrGlobalConfigGroupName(GlobalDict.DEFAULT_GROUP_NAME);
settingsStorage.setGlobalConfigGroupMap(new HashMap<>(root.listFiles().length));
for (File dir : root.listFiles()) {
GlobalConfigGroup globalConfigGroup = new GlobalConfigGroup();
globalConfigGroup.setName(dir.getName());
globalConfigGroup.setElementList(new ArrayList<>());
if (dir.isDirectory()) {
for (File file : dir.listFiles()) {
GlobalConfig globalConfig = new GlobalConfig();
globalConfig.setName(file.getName());
globalConfig.setValue(FileUtilRt.loadFile(file));
globalConfigGroup.getElementList().add(globalConfig);
}
}
settingsStorage.getGlobalConfigGroupMap().put(globalConfigGroup.getName(), globalConfigGroup);
}
}
private void loadTypeMapper(SettingsStorageDTO settingsStorage, File root) throws IOException {
settingsStorage.setCurrTypeMapperGroupName(GlobalDict.DEFAULT_GROUP_NAME);
settingsStorage.setTypeMapperGroupMap(new HashMap<>(root.listFiles().length));
for (File file : root.listFiles()) {
TypeMapperGroup typeMapperGroup = new TypeMapperGroup();
typeMapperGroup.setName(file.getName());
if (file.isFile()) {
String json = FileUtilRt.loadFile(file);
typeMapperGroup.setElementList(JSON.parse(json, new TypeReference<List<TypeMapper>>() {
}));
}
settingsStorage.getTypeMapperGroupMap().put(typeMapperGroup.getName(), typeMapperGroup);
}
}
private void loadColumnConfig(SettingsStorageDTO settingsStorage, File root) throws IOException {
settingsStorage.setCurrColumnConfigGroupName(GlobalDict.DEFAULT_GROUP_NAME);
settingsStorage.setColumnConfigGroupMap(new HashMap<>(root.listFiles().length));
for (File file : root.listFiles()) {
ColumnConfigGroup columnConfigGroup = new ColumnConfigGroup();
columnConfigGroup.setName(file.getName());
if (file.isFile()) {
String json = FileUtilRt.loadFile(file);
columnConfigGroup.setElementList(JSON.parse(json, new TypeReference<List<ColumnConfig>>() {
}));
}
settingsStorage.getColumnConfigGroupMap().put(columnConfigGroup.getName(), columnConfigGroup);
}
}
}