Skip to content

Commit e1ba463

Browse files
committed
update
1 parent 90bbb74 commit e1ba463

File tree

5 files changed

+63
-218
lines changed

5 files changed

+63
-218
lines changed

README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,12 @@
7979
````
8080
5. [自定义查询](src/main/java/com/revengemission/plugins/mybatis/GenericMapperPlugin.java), map传参数, 单独的mapper接口, 使用时注意过滤危险字符防止注入
8181
````
82-
<plugin type="com.revengemission.plugins.mybatis.GenericMapperPlugin">
83-
<property name="withMapperAnnotation" value="true"/>
84-
</plugin>
82+
<plugin type="com.revengemission.plugins.mybatis.GenericMapperPlugin"/>
8583
86-
Map<String, Object> paramsMapWithSql = new HashMap<>();
87-
paramsMapWithSql.put("sql", "select * from user_entity where name = #{name}");
84+
String sql = "select * from user_entity where name = #{parameters.name}";
85+
Map<String, Object> parameters = new HashMap<>();
8886
paramsMapWithSql.put("name", "zhangsan");
89-
List<LinkedHashMap<String, Object>> linkedHashMapList = genericMapper.queryForList(paramsMapWithSql);
87+
List<Map<String, Object>> mapList = genericMapper.queryForList(sql, parameters);
9088
````
9189
6. [mybatis model class上添加注解](src/main/java/com/revengemission/plugins/mybatis/ModelAnnotationPlugin.java)
9290
````

src/main/java/com/revengemission/plugins/mybatis/DynamicSqlSourceX.txt

Lines changed: 0 additions & 73 deletions
This file was deleted.
Lines changed: 59 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.revengemission.plugins.mybatis;
22

3-
43
import org.mybatis.generator.api.GeneratedJavaFile;
54
import org.mybatis.generator.api.IntrospectedTable;
65
import org.mybatis.generator.api.JavaFormatter;
@@ -11,20 +10,11 @@
1110
import org.mybatis.generator.api.dom.java.Parameter;
1211
import org.slf4j.LoggerFactory;
1312

14-
import java.io.BufferedReader;
15-
import java.io.File;
16-
import java.io.InputStreamReader;
17-
import java.nio.file.Files;
18-
import java.nio.file.Path;
19-
import java.nio.file.Paths;
20-
import java.nio.file.StandardOpenOption;
2113
import java.util.ArrayList;
2214
import java.util.List;
23-
import java.util.Map;
2415

2516
/**
2617
* 自定义查询、更新,生成单独的 mapper 文件
27-
* 在启用Mybatis cache的情况下,会有缓存不同步问题,此时建议使用【MybatisCustomSqlPlugin】
2818
*/
2919
public class GenericMapperPlugin extends AbstractXmbgPlugin {
3020

@@ -34,10 +24,11 @@ public class GenericMapperPlugin extends AbstractXmbgPlugin {
3424
private String queryForMapMethodName = "queryForMap";
3525
private String queryForListMethodName = "queryForList";
3626
private String queryForObjectMethodName = "queryForObject";
27+
private String countMethodName = "count";
28+
private String deleteMethodName = "delete";
29+
private String insertMethodName = "insert";
3730
private String updateMethodName = "update";
38-
private boolean withMapperAnnotation = true;
3931
private String encoding = "UTF-8";
40-
private String pluginPackageRelativePath = "com/revengemission/plugins/mybatis/";
4132

4233

4334
@Override
@@ -46,16 +37,6 @@ public void initialized(IntrospectedTable introspectedTable) {
4637
if (javaFileEncoding != null && !javaFileEncoding.trim().isEmpty()) {
4738
encoding = javaFileEncoding;
4839
}
49-
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
50-
if ("withMapperAnnotation".equalsIgnoreCase(entry.getKey().toString().trim())) {
51-
withMapperAnnotation = Boolean.parseBoolean(entry.getValue().toString().trim());
52-
break;
53-
}
54-
}
55-
56-
createJavaFile("DynamicSqlSourceX");
57-
createJavaFile("XMLScriptBuilderX");
58-
createJavaFile("MatchAnyLanguageDriver");
5940
}
6041

6142
@Override
@@ -65,102 +46,93 @@ public boolean validate(List<String> warnings) {
6546

6647
@Override
6748
public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles() {
68-
FullyQualifiedJavaType mapTypeString = new FullyQualifiedJavaType("Map<String, Object>");
69-
FullyQualifiedJavaType linkedHashMapTypeString = new FullyQualifiedJavaType("LinkedHashMap<String, Object>");
70-
FullyQualifiedJavaType listMapTypeString = new FullyQualifiedJavaType("List<LinkedHashMap<String, Object>>");
49+
FullyQualifiedJavaType longType = new FullyQualifiedJavaType("long");
50+
FullyQualifiedJavaType mapType = new FullyQualifiedJavaType("Map<String, Object>");
51+
FullyQualifiedJavaType listMapType = new FullyQualifiedJavaType("List<Map<String, Object>>");
7152
JavaFormatter javaFormatter = context.getJavaFormatter();
7253
FullyQualifiedJavaType interfaceType = new FullyQualifiedJavaType(context.getJavaClientGeneratorConfiguration().getTargetPackage() + "." + mapperName);
7354
Interface anInterface = new Interface(interfaceType);
7455
anInterface.setVisibility(JavaVisibility.PUBLIC);
7556

76-
FullyQualifiedJavaType langJavaType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Lang");
77-
anInterface.addImportedType(langJavaType);
78-
if (withMapperAnnotation) {
79-
FullyQualifiedJavaType mapperJavaType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper");
80-
anInterface.addImportedType(mapperJavaType);
81-
anInterface.addAnnotation("@Mapper");
82-
}
57+
FullyQualifiedJavaType mapperJavaType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Mapper");
58+
anInterface.addImportedType(mapperJavaType);
59+
anInterface.addAnnotation("@Mapper");
8360
FullyQualifiedJavaType selectJavaType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Select");
8461
anInterface.addImportedType(selectJavaType);
8562

86-
// FullyQualifiedJavaType updateJavaType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Update");
87-
// anInterface.addImportedType(updateJavaType);
63+
FullyQualifiedJavaType updateJavaType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Update");
64+
anInterface.addImportedType(updateJavaType);
65+
66+
FullyQualifiedJavaType insertJavaType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Insert");
67+
anInterface.addImportedType(insertJavaType);
68+
69+
FullyQualifiedJavaType deleteJavaType = new FullyQualifiedJavaType("org.apache.ibatis.annotations.Delete");
70+
anInterface.addImportedType(deleteJavaType);
8871

89-
anInterface.addImportedType(new FullyQualifiedJavaType("java.util.LinkedHashMap"));
9072
anInterface.addImportedType(new FullyQualifiedJavaType("java.util.Map"));
9173
anInterface.addImportedType(FullyQualifiedJavaType.getNewListInstance());
9274

9375
Method queryForMapMethod = new Method(queryForMapMethodName);
9476
queryForMapMethod.setAbstract(true);
95-
queryForMapMethod.addParameter(new Parameter(mapTypeString, "paramsMapWithSql"));
96-
queryForMapMethod.setReturnType(linkedHashMapTypeString);
97-
queryForMapMethod.addAnnotation("@Select(\"<script><select>不需要修改这一行!paramsMapWithSql 中放入sql 语句以及需要的占位符参数</select></script>\")");
98-
queryForMapMethod.addAnnotation("@Lang(MatchAnyLanguageDriver.class)");
77+
queryForMapMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "sql"));
78+
queryForMapMethod.addParameter(new Parameter(mapType, "parameters"));
79+
queryForMapMethod.setReturnType(mapType);
80+
queryForMapMethod.addAnnotation("@Select(\"${sql}\")");
9981
anInterface.addMethod(queryForMapMethod);
10082

10183
Method queryForListMethod = new Method(queryForListMethodName);
10284
queryForListMethod.setAbstract(true);
103-
queryForListMethod.addParameter(new Parameter(mapTypeString, "paramsMapWithSql"));
104-
queryForListMethod.setReturnType(listMapTypeString);
105-
queryForListMethod.addAnnotation("@Select(\"<script><select>不需要修改这一行!paramsMapWithSql 中放入sql 语句以及需要的占位符参数</select></script>\")");
106-
queryForListMethod.addAnnotation("@Lang(MatchAnyLanguageDriver.class)");
85+
queryForListMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "sql"));
86+
queryForListMethod.addParameter(new Parameter(mapType, "parameters"));
87+
queryForListMethod.setReturnType(listMapType);
88+
queryForListMethod.addAnnotation("@Select(\"${sql}\")");
10789
anInterface.addMethod(queryForListMethod);
10890

10991
Method queryForObjectMethod = new Method(queryForObjectMethodName);
11092
queryForObjectMethod.setAbstract(true);
111-
queryForObjectMethod.addParameter(new Parameter(mapTypeString, "paramsMapWithSql"));
93+
queryForObjectMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "sql"));
94+
queryForObjectMethod.addParameter(new Parameter(mapType, "parameters"));
11295
queryForObjectMethod.setReturnType(FullyQualifiedJavaType.getObjectInstance());
113-
queryForObjectMethod.addAnnotation("@Select(\"<script><select>不需要修改这一行!paramsMapWithSql 中放入sql 语句以及需要的占位符参数</select></script>\")");
114-
queryForObjectMethod.addAnnotation("@Lang(MatchAnyLanguageDriver.class)");
96+
queryForObjectMethod.addAnnotation("@Select(\"${sql}\")");
11597
anInterface.addMethod(queryForObjectMethod);
11698

117-
// Method updateMethod = new Method(updateMethodName);
118-
// updateMethod.setAbstract(true);
119-
// updateMethod.addParameter(new Parameter(mapTypeString, "paramsMapWithSql"));
120-
// updateMethod.setReturnType(FullyQualifiedJavaType.getIntInstance());
121-
// updateMethod.addAnnotation("@Update(\"<script><update>不需要修改这一行!paramsMapWithSql 中放入sql 语句以及需要的占位符参数</update></script>\")");
122-
// updateMethod.addAnnotation("@Lang(MatchAnyLanguageDriver.class)");
123-
// anInterface.addMethod(updateMethod);
99+
Method countMethod = new Method(countMethodName);
100+
countMethod.setAbstract(true);
101+
countMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "sql"));
102+
countMethod.addParameter(new Parameter(mapType, "parameters"));
103+
countMethod.setReturnType(longType);
104+
countMethod.addAnnotation("@Select(\"${sql}\")");
105+
anInterface.addMethod(countMethod);
106+
107+
Method updateMethod = new Method(updateMethodName);
108+
updateMethod.setAbstract(true);
109+
updateMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "sql"));
110+
updateMethod.addParameter(new Parameter(mapType, "parameters"));
111+
updateMethod.setReturnType(FullyQualifiedJavaType.getIntInstance());
112+
updateMethod.addAnnotation("@Update(\"${sql}\")");
113+
anInterface.addMethod(updateMethod);
114+
115+
Method insertMethod = new Method(insertMethodName);
116+
insertMethod.setAbstract(true);
117+
insertMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "sql"));
118+
insertMethod.addParameter(new Parameter(mapType, "parameters"));
119+
insertMethod.setReturnType(FullyQualifiedJavaType.getIntInstance());
120+
insertMethod.addAnnotation("@Insert(\"${sql}\")");
121+
anInterface.addMethod(insertMethod);
122+
123+
Method deleteMethod = new Method(deleteMethodName);
124+
deleteMethod.setAbstract(true);
125+
deleteMethod.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(), "sql"));
126+
deleteMethod.addParameter(new Parameter(mapType, "parameters"));
127+
deleteMethod.setReturnType(FullyQualifiedJavaType.getIntInstance());
128+
deleteMethod.addAnnotation("@Delete(\"${sql}\")");
129+
anInterface.addMethod(deleteMethod);
124130

125131
GeneratedJavaFile generatedJavaFile = new GeneratedJavaFile(anInterface, context.getJavaClientGeneratorConfiguration().getTargetProject(), encoding, javaFormatter);
126132
List<GeneratedJavaFile> answer = new ArrayList<>(16);
127133
answer.add(generatedJavaFile);
128134
return answer;
129135
}
130136

131-
/**
132-
* 根据txt文件生成java文件
133-
*
134-
* @param txtFileName txt文件名和java文件名
135-
*/
136-
void createJavaFile(String txtFileName) {
137-
String currentPath = System.getProperty("user.dir");
138-
String targetProject = context.getJavaClientGeneratorConfiguration().getTargetProject();
139-
String targetPackage = context.getJavaClientGeneratorConfiguration().getTargetPackage();
140-
Path targetClassFilePath = Paths.get(currentPath, targetProject, targetPackage.replace(".", File.separator));
141-
log.info("targetClassFilePath:{}", targetClassFilePath.toString());
142-
try {
143-
if (!Files.exists(targetClassFilePath)) {
144-
Files.createDirectories(targetClassFilePath);
145-
}
146-
StringBuffer buffer = new StringBuffer();
147-
String line = "";
148-
BufferedReader in = new BufferedReader(new InputStreamReader(this.getClass().getClassLoader().getResourceAsStream(pluginPackageRelativePath + txtFileName + ".txt"), encoding));
149-
while ((line = in.readLine()) != null) {
150-
buffer.append(line);
151-
buffer.append("\r\n");
152-
}
153-
String input = buffer.toString();
154-
155-
Files.write(Paths.get(targetClassFilePath.toString(), txtFileName + ".java"), ("package " + context.getJavaClientGeneratorConfiguration().getTargetPackage() + ";\r\n").getBytes(encoding), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
156-
157-
Files.write(Paths.get(targetClassFilePath.toString(), txtFileName + ".java"), input.getBytes(encoding), StandardOpenOption.CREATE, StandardOpenOption.APPEND);
158-
159-
} catch (Exception e) {
160-
log.error("读取、写入文件错误:", e);
161-
}
162-
163-
}
164-
165137

166138
}

src/main/java/com/revengemission/plugins/mybatis/MatchAnyLanguageDriver.txt

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/main/java/com/revengemission/plugins/mybatis/XMLScriptBuilderX.txt

Lines changed: 0 additions & 24 deletions
This file was deleted.

0 commit comments

Comments
 (0)