Skip to content

Commit cc81e18

Browse files
committed
feat: support without mybatis configuration file
1 parent 96c5798 commit cc81e18

File tree

5 files changed

+74
-25
lines changed

5 files changed

+74
-25
lines changed

build.gradle

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ plugins {
44
}
55

66
group 'org.linyimin'
7-
version '1.0.1'
7+
version '1.0.2'
88

99
repositories {
1010
maven { url 'https://maven.aliyun.com/repository/public/' }
@@ -28,6 +28,13 @@ intellij {
2828
}
2929
patchPluginXml {
3030
changeNotes = """
31+
<h4>1.0.2</h4>
32+
<ul>
33+
<li>Suppot without mybatis configuration file</li>
34+
</ul>
35+
<ul>
36+
<li>支持没有mybatis配置文件</li>
37+
</ul>
3138
<h4>1.0.1</h4>
3239
<ul>
3340
<li>Compatible with other plugins</li>

src/main/java/io/github/linyimin/plugin/cache/MybatisXmlContentCache.java

+42-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.intellij.psi.xml.XmlFile;
1212
import com.intellij.psi.xml.XmlTag;
1313
import io.github.linyimin.plugin.utils.MapperDomUtils;
14+
import org.apache.commons.lang3.StringUtils;
1415

1516
import java.util.*;
1617

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

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

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

31-
private static final Map<Project, Map<String /* method qualified name */, List<XmlTag>>> projectMapperMethodMap = new HashMap<>();
32+
private static final Map<Project, Map<String /* method qualified name */, Set<XmlTag>>> projectMapperMethodMap = new HashMap<>();
33+
34+
private static final Map<Project, Map<String /* method qualified name */, String /* mapper xml string */>> projectMethodToMapperFilePath = new HashMap<>();
3235

3336

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

51-
public static List<XmlTag> acquireByNamespace(Project project, String namespace) {
54+
public static String acquireMapperPathByMethodName(Project project, String methodName) {
55+
addXmlCache(project);
56+
57+
return projectMethodToMapperFilePath.getOrDefault(project, new HashMap<>()).get(methodName);
58+
}
59+
60+
public static Set<XmlTag> acquireByNamespace(Project project, String namespace) {
5261

5362
addXmlCache(project);
5463

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

57-
return cache.getOrDefault(namespace, new ArrayList<>());
66+
return cache.getOrDefault(namespace, new HashSet<>());
5867
}
5968

60-
public static List<XmlTag> acquireByMethodName(Project project, String methodQualifiedName) {
69+
public static Set<XmlTag> acquireByMethodName(Project project, String methodQualifiedName) {
6170

6271
addXmlCache(project);
6372

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

66-
return cache.getOrDefault(methodQualifiedName, new ArrayList<>());
75+
return cache.getOrDefault(methodQualifiedName, new HashSet<>());
6776
}
6877

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

125134
String id = subAttribute.getValue();
126135

136+
addMethodToMapperCache(project, namespace, id, psiFile);
137+
127138
addMethodXmlTagCache(project, namespace, id, subTag);
128139

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

133144
}
134145

146+
private static void addMethodToMapperCache(Project project, String namespace, String id, PsiFile psiFile) {
147+
Map<String, String> methodCacheMap = projectMethodToMapperFilePath.getOrDefault(project, new HashMap<>());
148+
149+
String methodQualifiedName = namespace + "." + id;
150+
151+
String path = psiFile.getVirtualFile().getPath();
152+
153+
if (StringUtils.isBlank(path)) {
154+
return;
155+
}
156+
157+
path = path.substring(path.indexOf("resources/") + "resources/".length());
158+
159+
methodCacheMap.put(methodQualifiedName, path);
160+
161+
projectMethodToMapperFilePath.put(project, methodCacheMap);
162+
163+
}
164+
135165
private static void addNamespaceXmlTagCache(Project project, String namespace, XmlTag xmlTag) {
136166

137-
Map<String, List<XmlTag>> namespaceCacheMap = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());
167+
Map<String, Set<XmlTag>> namespaceCacheMap = projectMapperNamespaceMap.getOrDefault(project, new HashMap<>());
138168

139-
List<XmlTag> tags = namespaceCacheMap.getOrDefault(namespace, new ArrayList<>());
169+
Set<XmlTag> tags = namespaceCacheMap.getOrDefault(namespace, new HashSet<>());
140170
tags.add(xmlTag);
141171

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

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

150-
Map<String, List<XmlTag>> methodCacheMap = projectMapperMethodMap.getOrDefault(project, new HashMap<>());
180+
Map<String, Set<XmlTag>> methodCacheMap = projectMapperMethodMap.getOrDefault(project, new HashMap<>());
151181

152182
String methodQualifiedName = namespace + "." + id;
153183

154-
List<XmlTag> tags = methodCacheMap.getOrDefault(methodQualifiedName, new ArrayList<>());
184+
Set<XmlTag> tags = methodCacheMap.getOrDefault(methodQualifiedName, new HashSet<>());
155185
tags.add(xmlTag);
156186

157187
methodCacheMap.put(methodQualifiedName, tags);

src/main/java/io/github/linyimin/plugin/dom/Constant.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,13 @@ public class Constant {
2929

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

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

3840
public static final String JAR_FILE_END = "!";
3941

src/main/java/io/github/linyimin/plugin/provider/MapperInterfaceProcessor.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
import com.intellij.psi.xml.XmlTag;
77
import io.github.linyimin.plugin.cache.MybatisXmlContentCache;
88
import java.util.Collections;
9-
import java.util.List;
109
import java.util.Objects;
10+
import java.util.Set;
1111

1212
/**
1313
* @author yiminlin
@@ -19,9 +19,9 @@ public class MapperInterfaceProcessor {
1919
* @param psiElement {@link PsiElement}
2020
* @return 满足要求的XmlTag列表
2121
*/
22-
public static List<XmlTag> processMapperInterface(PsiElement psiElement) {
22+
public static Set<XmlTag> processMapperInterface(PsiElement psiElement) {
2323
if (!(psiElement instanceof PsiClass)) {
24-
return Collections.emptyList();
24+
return Collections.emptySet();
2525
}
2626

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

4343
PsiMethod psiMethod = (PsiMethod) element;
4444
PsiClass psiClass = ((PsiMethod) element).getContainingClass();
4545

4646
if (Objects.isNull(psiClass)) {
47-
return Collections.emptyList();
47+
return Collections.emptySet();
4848
}
4949

5050
String qualifiedName = psiClass.getQualifiedName();

src/main/java/io/github/linyimin/plugin/service/SqlParamGenerateService.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import java.lang.reflect.Method;
2323
import java.util.*;
2424

25+
import static io.github.linyimin.plugin.dom.Constant.CONFIGURATION_TEMPLATE;
26+
2527
/**
2628
* @author yiminlin
2729
* @date 2022/02/02 2:09 上午
@@ -43,7 +45,15 @@ public String generateSql(Project project, String methodQualifiedName, String pa
4345
List<String> mybatisConfigs = MybatisXmlContentCache.acquireConfigurations(project);
4446

4547
if (org.apache.commons.collections.CollectionUtils.isEmpty(mybatisConfigs)) {
46-
return "Oops! The plugin can't find the configuration file.";
48+
49+
String mapper = MybatisXmlContentCache.acquireMapperPathByMethodName(project, methodQualifiedName);
50+
if (StringUtils.isBlank(mapper)) {
51+
return "Oops! The plugin can't find the configuration file.";
52+
}
53+
String configuration = CONFIGURATION_TEMPLATE.replace("${resource}", mapper);
54+
55+
mybatisConfigs = Collections.singletonList(configuration);
56+
4757
}
4858

4959
MybatisPojoCompile.compile(project);

0 commit comments

Comments
 (0)