Skip to content

Commit

Permalink
feat: support without mybatis configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
linyimin0812 committed Aug 30, 2022
1 parent 96c5798 commit cc81e18
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 25 deletions.
9 changes: 8 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'org.linyimin'
version '1.0.1'
version '1.0.2'

repositories {
maven { url 'https://maven.aliyun.com/repository/public/' }
Expand All @@ -28,6 +28,13 @@ intellij {
}
patchPluginXml {
changeNotes = """
<h4>1.0.2</h4>
<ul>
<li>Suppot without mybatis configuration file</li>
</ul>
<ul>
<li>支持没有mybatis配置文件</li>
</ul>
<h4>1.0.1</h4>
<ul>
<li>Compatible with other plugins</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.intellij.psi.xml.XmlFile;
import com.intellij.psi.xml.XmlTag;
import io.github.linyimin.plugin.utils.MapperDomUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

Expand All @@ -26,9 +27,11 @@ public class MybatisXmlContentCache {

private static final Map<Project, Map<String /* namespace */, List<String> /* method name list */>> projectMybatisMapperMap = new HashMap<>();

private static final Map<Project, Map<String /* namespace */, List<XmlTag>>> projectMapperNamespaceMap = new HashMap<>();
private static final Map<Project, Map<String /* namespace */, Set<XmlTag>>> projectMapperNamespaceMap = new HashMap<>();

private static final Map<Project, Map<String /* method qualified name */, List<XmlTag>>> projectMapperMethodMap = new HashMap<>();
private static final Map<Project, Map<String /* method qualified name */, Set<XmlTag>>> projectMapperMethodMap = new HashMap<>();

private static final Map<Project, Map<String /* method qualified name */, String /* mapper xml string */>> projectMethodToMapperFilePath = new HashMap<>();


public static List<String> acquireConfigurations(Project project) {
Expand All @@ -48,22 +51,28 @@ public static List<String> acquireByNamespace(Project project) {
return new ArrayList<>(namespaces);
}

public static List<XmlTag> acquireByNamespace(Project project, String namespace) {
public static String acquireMapperPathByMethodName(Project project, String methodName) {
addXmlCache(project);

return projectMethodToMapperFilePath.getOrDefault(project, new HashMap<>()).get(methodName);
}

public static Set<XmlTag> acquireByNamespace(Project project, String namespace) {

addXmlCache(project);

Map<String /* namespace */, List<XmlTag>> cache = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());
Map<String /* namespace */, Set<XmlTag>> cache = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());

return cache.getOrDefault(namespace, new ArrayList<>());
return cache.getOrDefault(namespace, new HashSet<>());
}

public static List<XmlTag> acquireByMethodName(Project project, String methodQualifiedName) {
public static Set<XmlTag> acquireByMethodName(Project project, String methodQualifiedName) {

addXmlCache(project);

Map<String /* namespace */, List<XmlTag>> cache = projectMapperMethodMap.getOrDefault(project, new HashMap<>());
Map<String /* namespace */, Set<XmlTag>> cache = projectMapperMethodMap.getOrDefault(project, new HashMap<>());

return cache.getOrDefault(methodQualifiedName, new ArrayList<>());
return cache.getOrDefault(methodQualifiedName, new HashSet<>());
}

private static void addXmlCache(Project project) {
Expand Down Expand Up @@ -124,6 +133,8 @@ private static void addMapperCache(Project project, PsiFile psiFile) {

String id = subAttribute.getValue();

addMethodToMapperCache(project, namespace, id, psiFile);

addMethodXmlTagCache(project, namespace, id, subTag);

addNamespaceCache(project, namespace, id);
Expand All @@ -132,11 +143,30 @@ private static void addMapperCache(Project project, PsiFile psiFile) {

}

private static void addMethodToMapperCache(Project project, String namespace, String id, PsiFile psiFile) {
Map<String, String> methodCacheMap = projectMethodToMapperFilePath.getOrDefault(project, new HashMap<>());

String methodQualifiedName = namespace + "." + id;

String path = psiFile.getVirtualFile().getPath();

if (StringUtils.isBlank(path)) {
return;
}

path = path.substring(path.indexOf("resources/") + "resources/".length());

methodCacheMap.put(methodQualifiedName, path);

projectMethodToMapperFilePath.put(project, methodCacheMap);

}

private static void addNamespaceXmlTagCache(Project project, String namespace, XmlTag xmlTag) {

Map<String, List<XmlTag>> namespaceCacheMap = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());
Map<String, Set<XmlTag>> namespaceCacheMap = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());

List<XmlTag> tags = namespaceCacheMap.getOrDefault(namespace, new ArrayList<>());
Set<XmlTag> tags = namespaceCacheMap.getOrDefault(namespace, new HashSet<>());
tags.add(xmlTag);

namespaceCacheMap.put(namespace, tags);
Expand All @@ -147,11 +177,11 @@ private static void addNamespaceXmlTagCache(Project project, String namespace, X

private static void addMethodXmlTagCache(Project project, String namespace, String id, XmlTag xmlTag) {

Map<String, List<XmlTag>> methodCacheMap = projectMapperMethodMap.getOrDefault(project, new HashMap<>());
Map<String, Set<XmlTag>> methodCacheMap = projectMapperMethodMap.getOrDefault(project, new HashMap<>());

String methodQualifiedName = namespace + "." + id;

List<XmlTag> tags = methodCacheMap.getOrDefault(methodQualifiedName, new ArrayList<>());
Set<XmlTag> tags = methodCacheMap.getOrDefault(methodQualifiedName, new HashSet<>());
tags.add(xmlTag);

methodCacheMap.put(methodQualifiedName, tags);
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/io/github/linyimin/plugin/dom/Constant.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public class Constant {

public static final String DATABASE_URL_TEMPLATE = "jdbc:mysql://%s:%s/%s";

public static final String PLUGIN_CLASS_LOADER_MY_PARENTS = "myParents";
public static final String PLUGIN_CLASS_LOADER_PARENTS = "parents";

public static final String MYBATIS_LOGGING_SLF4J = "slf4j";
public static final String MYBATIS_LOGGING_LOG4J = "log4j";
public static final String CONFIGURATION_TEMPLATE = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n" +
"<!DOCTYPE configuration PUBLIC \"-//mybatis.org//DTD Config 3.0//EN\" \"http://mybatis.org/dtd/mybatis-3-config.dtd\">\n" +
"<configuration>\n" +
" <mappers>\n" +
" <mapper resource=\"${resource}\" />\n" +
" </mappers>\n" +
"</configuration>";

public static final String JAR_FILE_END = "!";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import com.intellij.psi.xml.XmlTag;
import io.github.linyimin.plugin.cache.MybatisXmlContentCache;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
* @author yiminlin
Expand All @@ -19,9 +19,9 @@ public class MapperInterfaceProcessor {
* @param psiElement {@link PsiElement}
* @return 满足要求的XmlTag列表
*/
public static List<XmlTag> processMapperInterface(PsiElement psiElement) {
public static Set<XmlTag> processMapperInterface(PsiElement psiElement) {
if (!(psiElement instanceof PsiClass)) {
return Collections.emptyList();
return Collections.emptySet();
}

PsiClass psiClass = (PsiClass) psiElement;
Expand All @@ -35,16 +35,16 @@ public static List<XmlTag> processMapperInterface(PsiElement psiElement) {
* @param element {@link PsiElement}
* @return 满足要求的XmlTag列表
*/
public static List<XmlTag> processMapperMethod(PsiElement element) {
public static Set<XmlTag> processMapperMethod(PsiElement element) {
if (!(element instanceof PsiMethod)) {
return Collections.emptyList();
return Collections.emptySet();
}

PsiMethod psiMethod = (PsiMethod) element;
PsiClass psiClass = ((PsiMethod) element).getContainingClass();

if (Objects.isNull(psiClass)) {
return Collections.emptyList();
return Collections.emptySet();
}

String qualifiedName = psiClass.getQualifiedName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.lang.reflect.Method;
import java.util.*;

import static io.github.linyimin.plugin.dom.Constant.CONFIGURATION_TEMPLATE;

/**
* @author yiminlin
* @date 2022/02/02 2:09 上午
Expand All @@ -43,7 +45,15 @@ public String generateSql(Project project, String methodQualifiedName, String pa
List<String> mybatisConfigs = MybatisXmlContentCache.acquireConfigurations(project);

if (org.apache.commons.collections.CollectionUtils.isEmpty(mybatisConfigs)) {
return "Oops! The plugin can't find the configuration file.";

String mapper = MybatisXmlContentCache.acquireMapperPathByMethodName(project, methodQualifiedName);
if (StringUtils.isBlank(mapper)) {
return "Oops! The plugin can't find the configuration file.";
}
String configuration = CONFIGURATION_TEMPLATE.replace("${resource}", mapper);

mybatisConfigs = Collections.singletonList(configuration);

}

MybatisPojoCompile.compile(project);
Expand Down

0 comments on commit cc81e18

Please sign in to comment.