使用getMapper方式对自定义持久层框架进行优化
# 问题分析
1、Dao层使用持久层框架,存在代码重复,整个操作过程模板重复(加载配置文件、创建SqlSessionFactory、生产SqlSession)
2、存在硬编码(statementId)
# 解决思路
# getMapper+动态代理方式优化
使用代理模式生成Dao层接口的实现类
SqlSession接口新增getMapper方法
fun <T> getMapper(mapperClass: Class<*>?): T
1
public <T> T getMapper(Class<?> mapperClass);
1
// Make sure to add code blocks to your code group
DefaultSqlSession中getMapper方法的实现
override fun <T> getMapper(mapperClass: Class<*>): T {
// 使用JDK动态代理为Dao接口生成代理对象
return Proxy.newProxyInstance(
DefaultSqlSession::class.java.classLoader,
arrayOf(mapperClass)
) { proxy, method, args -> // 底层还是执行JDBC
// 准备参数1
// statemendid=namespace.id,获取不到
// statemendid=接口全限定名.方法名
val clazzName = method.declaringClass.name
val methodName = method.name
val statementId = "$clazzName.$methodName"
// 准备参数2:params=args
// 获取被调用方法的返回类型
val genericReturnType = method.genericReturnType
// 判断是否进行了泛型类型参数化
if (genericReturnType is ParameterizedType) {
selectList<Any>(statementId, *args)
} else selectOne<Any>(statementId, *args)
} as T
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
@Override
public <T> T getMapper(Class<?> mapperClass) {
// 使用JDK动态代理为Dao接口生成代理对象
return (T) Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
// 底层还是执行JDBC
// 准备参数1
// statemendid=namespace.id,获取不到
// statemendid=接口全限定名.方法名
String clazzName = method.getDeclaringClass().getName();
String methodName = method.getName();
String statementId = clazzName + "." + methodName;
// 准备参数2:params=args
// 获取被调用方法的返回类型
Type genericReturnType = method.getGenericReturnType();
// 判断是否进行了泛型类型参数化
if (genericReturnType instanceof ParameterizedType) {
return selectList(statementId, args);
}
return selectOne(statementId, args);
}
});
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Make sure to add code blocks to your code group
# 优化版代码
mybatis-proxy/custom-persistence (opens new window)
# 默认实现方式
mybatis-normal/custom-persistence (opens new window)
文章更新历史
2022/05/08 feat:增加Kotlin实现。
编辑 (opens new window)
上次更新: 2023/02/22, 13:47:25
- 01
- 解决css部分border被圆角切掉之后圆角的边框消失问题03-18
- 02
- 使用TypeScript开发一个自定义的Node-js前端开发脚手架03-08
- 03
- Github-Actions使用release-please实现自动发版03-06