mirror of https://gitee.com/makejava/EasyCode.git
Merge branch 'feat-generate-from-entity' of https://github.com/lkqm/EasyCode into feat-generate-from-entity
This commit is contained in:
commit
497644d0dd
|
@ -5,25 +5,27 @@ import com.intellij.openapi.actionSystem.AnAction;
|
|||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.CommonDataKeys;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.javadoc.PsiDocComment;
|
||||
import com.intellij.psi.javadoc.PsiDocToken;
|
||||
import com.sjhy.plugin.entity.ColumnInfo;
|
||||
import com.sjhy.plugin.entity.TableInfo;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.psi.PsiClass;
|
||||
import com.intellij.psi.PsiJavaFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.sjhy.plugin.tool.CacheDataUtils;
|
||||
import com.sjhy.plugin.ui.SelectSavePath;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 从Java类生成代码菜单
|
||||
*
|
||||
* @author Mario Luo
|
||||
*/
|
||||
public class EasyCodeEntityAction extends AnAction {
|
||||
|
||||
private CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
|
||||
private final CacheDataUtils cacheDataUtils = CacheDataUtils.getInstance();
|
||||
|
||||
@Override
|
||||
public void actionPerformed(@NotNull AnActionEvent event) {
|
||||
|
@ -31,23 +33,47 @@ public class EasyCodeEntityAction extends AnAction {
|
|||
if (project == null) {
|
||||
return;
|
||||
}
|
||||
PsiFile psiFile = event.getData(CommonDataKeys.PSI_FILE);
|
||||
if (!(psiFile instanceof PsiJavaFile)) {
|
||||
|
||||
// 过滤选择Java文件
|
||||
VirtualFile[] psiFiles = event.getData(CommonDataKeys.VIRTUAL_FILE_ARRAY);
|
||||
PsiManager psiManager = PsiManager.getInstance(project);
|
||||
List<PsiJavaFile> psiJavaFiles = Arrays.stream(psiFiles)
|
||||
.map(psiManager::findFile)
|
||||
.filter(f -> f instanceof PsiJavaFile)
|
||||
.map(f -> (PsiJavaFile) f)
|
||||
.collect(Collectors.toList());
|
||||
if (psiJavaFiles.size() == 0) {
|
||||
return;
|
||||
}
|
||||
PsiJavaFile psiJavaFile = (PsiJavaFile) psiFile;
|
||||
PsiClass psiClass = Arrays.stream(psiJavaFile.getClasses())
|
||||
.filter(o -> o.getModifierList() != null && o.getModifierList().hasModifierProperty("public"))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (psiClass == null) {
|
||||
|
||||
// 获取选中的类
|
||||
List<PsiClass> psiClassList = resolvePsiClassByFile(psiJavaFiles);
|
||||
if (psiClassList.size() == 0) {
|
||||
return;
|
||||
}
|
||||
cacheDataUtils.setSelectPsiClass(psiClass);
|
||||
cacheDataUtils.setPsiClassList(Lists.newArrayList(psiClass));
|
||||
|
||||
// 缓存选中值
|
||||
cacheDataUtils.setSelectPsiClass(psiClassList.get(0));
|
||||
cacheDataUtils.setPsiClassList(psiClassList);
|
||||
new SelectSavePath(project, true).open();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析类
|
||||
*/
|
||||
private List<PsiClass> resolvePsiClassByFile(List<PsiJavaFile> psiJavaFiles) {
|
||||
List<PsiClass> psiClassList = Lists.newArrayListWithCapacity(psiJavaFiles.size());
|
||||
for (PsiJavaFile psiJavaFile : psiJavaFiles) {
|
||||
PsiClass psiClass = Arrays.stream(psiJavaFile.getClasses())
|
||||
.filter(o -> o.getModifierList() != null && o.getModifierList().hasModifierProperty(PsiModifier.PUBLIC))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (psiClass != null) {
|
||||
psiClassList.add(psiClass);
|
||||
}
|
||||
}
|
||||
return psiClassList;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -118,11 +118,11 @@ public class Settings implements PersistentStateComponent<Settings> {
|
|||
List<TypeMapper> typeMapperList = new ArrayList<>();
|
||||
typeMapperList.add(new TypeMapper("varchar(\\(\\d+\\))?", "java.lang.String"));
|
||||
typeMapperList.add(new TypeMapper("char(\\(\\d+\\))?", "java.lang.String"));
|
||||
typeMapperList.add(new TypeMapper("text", "java.lang.String"));
|
||||
typeMapperList.add(new TypeMapper("(tiny|medium|long)*text", "java.lang.String"));
|
||||
typeMapperList.add(new TypeMapper("decimal(\\(\\d+\\))?", "java.lang.Double"));
|
||||
typeMapperList.add(new TypeMapper("decimal(\\(\\d+,\\d+\\))?", "java.lang.Double"));
|
||||
typeMapperList.add(new TypeMapper("integer", "java.lang.Integer"));
|
||||
typeMapperList.add(new TypeMapper("int(\\(\\d+\\))?", "java.lang.Integer"));
|
||||
typeMapperList.add(new TypeMapper("(tiny|small|medium)*int(\\(\\d+\\))?", "java.lang.Integer"));
|
||||
typeMapperList.add(new TypeMapper("int4", "java.lang.Integer"));
|
||||
typeMapperList.add(new TypeMapper("int8", "java.lang.Long"));
|
||||
typeMapperList.add(new TypeMapper("bigint(\\(\\d+\\))?", "java.lang.Long"));
|
||||
|
|
|
@ -24,14 +24,8 @@ import com.sjhy.plugin.entity.SaveFile;
|
|||
import com.sjhy.plugin.entity.TableInfo;
|
||||
import com.sjhy.plugin.entity.TypeMapper;
|
||||
import com.sjhy.plugin.service.TableInfoService;
|
||||
import com.sjhy.plugin.tool.CloneUtils;
|
||||
import com.sjhy.plugin.tool.CollectionUtil;
|
||||
import com.sjhy.plugin.tool.CurrGroupUtils;
|
||||
import com.sjhy.plugin.tool.FileUtils;
|
||||
import com.sjhy.plugin.tool.MessageDialogUtils;
|
||||
import com.sjhy.plugin.tool.NameUtils;
|
||||
import com.sjhy.plugin.tool.ProjectUtils;
|
||||
import com.sjhy.plugin.tool.StringUtils;
|
||||
import com.sjhy.plugin.tool.*;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
@ -44,6 +38,7 @@ import java.util.Objects;
|
|||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
import java.util.regex.PatternSyntaxException;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
|
@ -396,13 +391,16 @@ public class TableInfoServiceImpl implements TableInfoService {
|
|||
tableInfo.setPkColumn(new ArrayList<>());
|
||||
tableInfo.setOtherColumn(new ArrayList<>());
|
||||
for (PsiField field : psiClass.getAllFields()) {
|
||||
if (PsiClassGenerateUtils.isSkipField(field)) {
|
||||
continue;
|
||||
}
|
||||
ColumnInfo columnInfo = new ColumnInfo();
|
||||
columnInfo.setName(field.getName());
|
||||
columnInfo.setShortType(field.getType().getPresentableText());
|
||||
columnInfo.setType(field.getType().getCanonicalText());
|
||||
columnInfo.setComment(parsePsiClassComment(field.getDocComment()));
|
||||
tableInfo.getFullColumn().add(columnInfo);
|
||||
if ("id".equals(field.getName())) {
|
||||
if (PsiClassGenerateUtils.isPkField(field)) {
|
||||
tableInfo.getPkColumn().add(columnInfo);
|
||||
} else {
|
||||
tableInfo.getOtherColumn().add(columnInfo);
|
||||
|
@ -419,6 +417,7 @@ public class TableInfoServiceImpl implements TableInfoService {
|
|||
return fullName.substring(0, genericIdx);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public TableInfo getTableInfoAndConfigByPsiClass(PsiClass selectPsiClass) {
|
||||
TableInfo tableInfo = this.getTableInfoByPsiClass(selectPsiClass);
|
||||
|
@ -535,7 +534,7 @@ public class TableInfoServiceImpl implements TableInfoService {
|
|||
String schemaName = DasUtil.getSchema(dbTable);
|
||||
return schemaName + "-" + dbTable.getName() + ".json";
|
||||
} else {
|
||||
return tableInfo.getName() + "_class.json";
|
||||
return tableInfo.getPsiClassObj().getQualifiedName() + ".json";
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,54 @@
|
|||
package com.sjhy.plugin.tool;
|
||||
|
||||
import com.intellij.psi.PsiAnnotation;
|
||||
import com.intellij.psi.PsiField;
|
||||
import com.intellij.psi.PsiModifier;
|
||||
import com.intellij.psi.PsiModifierList;
|
||||
|
||||
/**
|
||||
* 从实体类生成代码业务工具类
|
||||
*
|
||||
* @author Mario Luo
|
||||
*/
|
||||
public final class PsiClassGenerateUtils {
|
||||
|
||||
private PsiClassGenerateUtils() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否是主键字段
|
||||
*/
|
||||
public static boolean isPkField(PsiField field) {
|
||||
if("id".equals(field.getName())) {
|
||||
return true;
|
||||
}
|
||||
PsiAnnotation idAnnotation = field.getAnnotation("org.springframework.data.annotation.Id");
|
||||
if (idAnnotation != null) {
|
||||
return true;
|
||||
}
|
||||
idAnnotation = field.getAnnotation("javax.persistence.Id");
|
||||
if (idAnnotation != null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否需要跳过该字段
|
||||
*/
|
||||
public static boolean isSkipField(PsiField field) {
|
||||
PsiModifierList modifierList = field.getModifierList();
|
||||
if(modifierList != null && modifierList.hasExplicitModifier(PsiModifier.STATIC)) {
|
||||
return true;
|
||||
}
|
||||
PsiAnnotation annotation = field.getAnnotation("org.springframework.data.annotation.Transient");
|
||||
if (annotation != null) {
|
||||
return true;
|
||||
}
|
||||
annotation = field.getAnnotation("javax.persistence.Transient");
|
||||
if (annotation != null) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -241,9 +241,11 @@
|
|||
<!--生成代码菜单-->
|
||||
<add-to-group group-id="DatabaseViewPopupMenu" anchor="first"/>
|
||||
</group>
|
||||
<action id="con.sjhy.easy.code.action.EasyCodeFromEntity"
|
||||
<!-- 实体类生成代码 -->
|
||||
<action id="com.sjhy.plugin.actions.EasyCodeEntityAction"
|
||||
class="com.sjhy.plugin.actions.EasyCodeEntityAction" text="EasyCode...">
|
||||
<add-to-group group-id="GenerateGroup" anchor="last"/>
|
||||
<add-to-group group-id="ProjectViewPopupMenu" anchor="last"/>
|
||||
</action>
|
||||
</actions>
|
||||
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
##导入宏定义、设置包名、类名、文件名##导入宏定义
|
||||
$!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))
|
||||
|
||||
|
||||
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;
|
||||
|
||||
/**
|
||||
* 获取列表(分页)
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public Page<$entityShortType> list(Pageable page) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一条数据
|
||||
*/
|
||||
@GetMapping("/get")
|
||||
public $entityShortType get($!pk.shortType id) {
|
||||
return ${serviceVarName}.findById(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加
|
||||
*/
|
||||
@PostMapping("/add")
|
||||
public void add(@RequestBody $entityShortType $entityVarName) {
|
||||
${serviceVarName}.save($entityVarName);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 修改
|
||||
*/
|
||||
@PostMapping("/update")
|
||||
public void update(@RequestBody $entityShortType $entityVarName) {
|
||||
${serviceVarName}.save($entityVarName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除
|
||||
*/
|
||||
@PostMapping("/delete")
|
||||
public void delete($!pk.shortType id) {
|
||||
${serviceVarName}.deleteById(id);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
##导入宏定义、设置包名、类名、文件名##导入宏定义
|
||||
$!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($pkShortType = $!pk.shortType)
|
||||
|
||||
#if($tableInfo.psiClassObj)
|
||||
import $!tableInfo.psiClassObj.getQualifiedName();
|
||||
#end
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
|
||||
|
||||
/**
|
||||
* $!{tableInfo.comment}持久层
|
||||
*
|
||||
* @author $!author
|
||||
* @since $!time.currTime()
|
||||
*/
|
||||
public interface $!{tableName} extends MongoRepository<$entityShortType, $pkShortType> {
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
##导入宏定义、设置包名、类名、文件名##导入宏定义
|
||||
$!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)
|
||||
|
||||
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);
|
||||
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
##导入宏定义、设置包名、类名、文件名
|
||||
$!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)
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue