Skip to content

Commit 0dcc0ff

Browse files
committed
预期把mybatis打包进来
1 parent 270def7 commit 0dcc0ff

File tree

4 files changed

+183
-0
lines changed

4 files changed

+183
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.baomidou.mybatisplus.code;
2+
3+
import lombok.Builder;
4+
import lombok.Data;
5+
import lombok.Singular;
6+
7+
import java.util.List;
8+
9+
/**
10+
* @author miemie
11+
* @since 2025/9/1
12+
*/
13+
@Data
14+
@Builder
15+
public class Overwrite {
16+
private String front;
17+
private String behind;
18+
private int interval;
19+
/* */
20+
@Singular
21+
private List<Content> contents;
22+
@Singular("addImport")
23+
private List<String> imports;
24+
25+
@Data
26+
@Builder
27+
public static class Content {
28+
@Builder.Default
29+
private Operate operate = Operate.INSERT;
30+
private int frontDown;
31+
private int behindUp;
32+
private String code;
33+
}
34+
35+
enum Operate {
36+
INSERT,
37+
DELETE
38+
}
39+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baomidou.mybatisplus.code;
2+
3+
import lombok.Data;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.function.Function;
8+
9+
/**
10+
* @author miemie
11+
* @since 2025/9/1
12+
*/
13+
@Data
14+
public abstract class OverwriteFile {
15+
16+
private List<Overwrite> steps = new ArrayList<>();
17+
18+
public void addStep(Function<Overwrite.OverwriteBuilder, Overwrite.OverwriteBuilder> function) {
19+
this.steps.add(function.apply(Overwrite.builder()).build());
20+
}
21+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baomidou.mybatisplus.code;
2+
3+
import com.baomidou.mybatisplus.code.sub.MapperMethod;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
import java.util.jar.JarEntry;
13+
import java.util.jar.JarFile;
14+
import java.util.regex.Matcher;
15+
import java.util.regex.Pattern;
16+
import java.util.stream.Collectors;
17+
18+
/**
19+
* @author miemie
20+
* @since 2025/9/1
21+
*/
22+
public class OverwriteRunner {
23+
24+
public static void main(String[] args) throws Exception {
25+
List<OverwriteFile> files = List.of(new MapperMethod());
26+
Map<String, OverwriteFile> map = finJar(files);
27+
map.forEach((k, v) -> {
28+
System.out.println(k);
29+
});
30+
}
31+
32+
private static Map<String, OverwriteFile> finJar(List<OverwriteFile> files) throws IOException {
33+
Map<String, OverwriteFile> map = files.stream().collect(Collectors.toMap(i -> i.getClass().getSimpleName(), i -> i));
34+
Map<String, OverwriteFile> result = new HashMap<>();
35+
String ver = findVer();
36+
Path jarPath = Paths.get(System.getProperty("user.home"), ".m2", "repository", "org", "mybatis", "mybatis",
37+
ver, "mybatis-" + ver + "-sources.jar");
38+
try (JarFile jarFile = new JarFile(jarPath.toFile())) {
39+
for (JarEntry entry : jarFile.stream().toList()) {
40+
String name = entry.getName();
41+
if (!name.endsWith(".java")) {
42+
continue;
43+
}
44+
String className = name.substring(name.lastIndexOf("/") + 1, name.length() - 5);
45+
if (map.containsKey(className)) {
46+
result.put(name, map.get(className));
47+
}
48+
}
49+
}
50+
return result;
51+
}
52+
53+
private static String findVer() throws IOException {
54+
String userDir = System.getProperty("user.dir");
55+
String content = new String(Files.readAllBytes(Paths.get(userDir + "/build.gradle")));
56+
// 定义正则表达式,匹配 mybatisVersion = '3.5.19'
57+
Pattern pattern = Pattern.compile("mybatisVersion\\s*=\\s*['\"]([\\d.]+)['\"]");
58+
Matcher matcher = pattern.matcher(content);
59+
if (matcher.find()) {
60+
return matcher.group(1);
61+
}
62+
throw new RuntimeException();
63+
}
64+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baomidou.mybatisplus.code.sub;
2+
3+
import com.baomidou.mybatisplus.code.Overwrite;
4+
import com.baomidou.mybatisplus.code.OverwriteFile;
5+
6+
/**
7+
* {@link org.apache.ibatis.binding.MapperMethod}
8+
*
9+
* @author miemie
10+
* @since 2025/9/1
11+
*/
12+
public class MapperMethod extends OverwriteFile {
13+
14+
public MapperMethod() {
15+
// import
16+
addStep(i -> i
17+
.addImport("com.baomidou.mybatisplus.core.metadata.IPage")
18+
.addImport("com.baomidou.mybatisplus.core.toolkit.Assert"));
19+
// execute
20+
addStep(i -> i
21+
.front("result = executeForCursor(sqlSession, args);")
22+
.interval(8)
23+
.behind("case FLUSH:")
24+
.content(Overwrite.Content.builder()
25+
.code("""
26+
if (IPage.class.isAssignableFrom(method.getReturnType())) {
27+
result = executeForIPage(sqlSession, args);
28+
} else {
29+
""")
30+
.frontDown(1).build())
31+
.content(Overwrite.Content.builder()
32+
.code("}")
33+
.behindUp(3)
34+
.build())
35+
);
36+
addStep(i -> i
37+
.behind("private Object rowCountResult(int rowCount) {")
38+
.content(Overwrite.Content.builder()
39+
.code("""
40+
@SuppressWarnings("all")
41+
private <E> Object executeForIPage(SqlSession sqlSession, Object[] args) {
42+
IPage<E> result = null;
43+
for (Object arg : args) {
44+
if (arg instanceof IPage) {
45+
result = (IPage<E>) arg;
46+
break;
47+
}
48+
}
49+
Assert.notNull(result, "can't found IPage for args!");
50+
Object param = method.convertArgsToSqlCommandParam(args);
51+
List<E> list = sqlSession.selectList(command.getName(), param);
52+
result.setRecords(list);
53+
return result;
54+
}
55+
""")
56+
.behindUp(2).build())
57+
);
58+
}
59+
}

0 commit comments

Comments
 (0)