From 144c93f24b179819dec9976a04f601e6f43291b6 Mon Sep 17 00:00:00 2001
From: FuriousPws002 <1938485828@qq.com>
Date: Fri, 12 Apr 2024 21:32:47 +0800
Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=9F=A5=E8=AF=A2=E7=BB=93?=
=?UTF-8?q?=E6=9E=9C=E9=9B=86resultType=E7=9A=84=E5=A4=84=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README.md | 3 +-
.../apache/ibatis/binding/MapperMethod.java | 15 +-
.../ibatis/builder/xml/XMLMapperBuilder.java | 2 +-
.../org/apache/ibatis/executor/Executor.java | 3 +
.../ibatis/executor/SimpleExecutor.java | 13 ++
.../resultset/DefaultResultSetHandler.java | 141 ++++++++++++++++++
.../executor/resultset/ResultSetWrapper.java | 10 +-
.../statement/PrepareStatementHandler.java | 11 ++
.../executor/statement/StatementHandler.java | 3 +
.../ibatis/mapping/MappedStatement.java | 4 +
.../apache/ibatis/session/Configuration.java | 6 +
.../org/apache/ibatis/session/SqlSession.java | 3 +
.../session/defaults/DefaultSqlSession.java | 17 ++-
.../ibatis/type/IntegerTypeHandler.java | 3 +-
.../org/apache/ibatis/dao/UserMapper.java | 4 +
.../java/org/apache/ibatis/entity/UserDO.java | 9 ++
.../apache/ibatis/session/SqlSessionTest.java | 18 +++
src/test/resources/mapper/UserMapper.xml | 4 +
18 files changed, 261 insertions(+), 8 deletions(-)
create mode 100644 src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
diff --git a/README.md b/README.md
index e4236fa..6c3c1b2 100644
--- a/README.md
+++ b/README.md
@@ -4,4 +4,5 @@
[1.注册Mapper接口](https://github.com/FuriousPws002/mini-mybatis/wiki/1.%E6%B3%A8%E5%86%8CMapper%E6%8E%A5%E5%8F%A3 "Markdown")
[2.解析xml中静态sql的mapper](https://github.com/FuriousPws002/mini-mybatis/wiki/2.%E8%A7%A3%E6%9E%90xml%E4%B8%AD%E9%9D%99%E6%80%81sql%E7%9A%84mapper "Markdown")
[3.执行静态sql](https://github.com/FuriousPws002/mini-mybatis/wiki/3.%E6%89%A7%E8%A1%8C%E9%9D%99%E6%80%81sql "Markdown")
-[4.参数绑定](https://github.com/FuriousPws002/mini-mybatis/wiki/4.%E5%8F%82%E6%95%B0%E7%BB%91%E5%AE%9A "Markdown")
\ No newline at end of file
+[4.参数绑定](https://github.com/FuriousPws002/mini-mybatis/wiki/4.%E5%8F%82%E6%95%B0%E7%BB%91%E5%AE%9A "Markdown")
+[5.resultType结果集处理](https://github.com/FuriousPws002/mini-mybatis/wiki/5.resultType%E7%BB%93%E6%9E%9C%E9%9B%86%E5%A4%84%E7%90%86 "Markdown")
\ No newline at end of file
diff --git a/src/main/java/org/apache/ibatis/binding/MapperMethod.java b/src/main/java/org/apache/ibatis/binding/MapperMethod.java
index ffaf2d4..196b320 100644
--- a/src/main/java/org/apache/ibatis/binding/MapperMethod.java
+++ b/src/main/java/org/apache/ibatis/binding/MapperMethod.java
@@ -1,6 +1,7 @@
package org.apache.ibatis.binding;
import java.lang.reflect.Method;
+import java.util.Collection;
import java.util.Objects;
import org.apache.ibatis.mapping.MappedStatement;
@@ -23,18 +24,28 @@ public MapperMethod(Class> mapperInterface, Method method, Configuration confi
}
public Object execute(SqlSession sqlSession, Object[] args) {
- Object result;
+ Object result = null;
switch (command.getType()) {
case INSERT: {
result = sqlSession.insert(command.getName(), method.convertArgsToSqlCommandParam(args));
break;
}
+ case SELECT: {
+ if (method.returnsMany) {
+ result = executeForMany(sqlSession, args);
+ }
+ break;
+ }
default:
throw new BindingException(command.getType() + " not support");
}
return result;
}
+ private Object executeForMany(SqlSession sqlSession, Object[] args) {
+ return sqlSession.selectList(command.getName(), method.convertArgsToSqlCommandParam(args));
+ }
+
public static class SqlCommand {
private final String name;
@@ -63,9 +74,11 @@ public SqlCommandType getType() {
public static class MethodSignature {
private final ParamNameResolver paramNameResolver;
+ private final boolean returnsMany;
public MethodSignature(Configuration configuration, Class> mapperInterface, Method method) {
this.paramNameResolver = new ParamNameResolver(method);
+ returnsMany = Collection.class.isAssignableFrom(method.getReturnType());
}
public Object convertArgsToSqlCommandParam(Object[] args) {
diff --git a/src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java b/src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java
index 42b88c3..8439100 100644
--- a/src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java
+++ b/src/main/java/org/apache/ibatis/builder/xml/XMLMapperBuilder.java
@@ -43,7 +43,7 @@ private void configurationElement(XNode context) {
if (Objects.isNull(namespace) || namespace.isEmpty()) {
throw new BuilderException("mapper namespace can not be empty");
}
- buildStatementFromContext(context.evalNodes("insert"));
+ buildStatementFromContext(context.evalNodes("insert|select"));
} catch (Exception e) {
throw new BuilderException("parse mapper xml error", e);
}
diff --git a/src/main/java/org/apache/ibatis/executor/Executor.java b/src/main/java/org/apache/ibatis/executor/Executor.java
index bd6e71d..4295a1e 100644
--- a/src/main/java/org/apache/ibatis/executor/Executor.java
+++ b/src/main/java/org/apache/ibatis/executor/Executor.java
@@ -1,6 +1,7 @@
package org.apache.ibatis.executor;
import java.sql.SQLException;
+import java.util.List;
import org.apache.ibatis.mapping.MappedStatement;
@@ -10,4 +11,6 @@
public interface Executor {
int update(MappedStatement ms, Object parameter) throws SQLException;
+
+ List query(MappedStatement ms, Object parameter) throws SQLException;
}
diff --git a/src/main/java/org/apache/ibatis/executor/SimpleExecutor.java b/src/main/java/org/apache/ibatis/executor/SimpleExecutor.java
index 46af77b..544b19b 100644
--- a/src/main/java/org/apache/ibatis/executor/SimpleExecutor.java
+++ b/src/main/java/org/apache/ibatis/executor/SimpleExecutor.java
@@ -3,6 +3,7 @@
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
+import java.util.List;
import java.util.Objects;
import org.apache.ibatis.executor.statement.StatementHandler;
@@ -36,6 +37,18 @@ public int update(MappedStatement ms, Object parameter) throws SQLException {
}
}
+ @Override
+ public List query(MappedStatement ms, Object parameter) throws SQLException {
+ Statement stmt = null;
+ try {
+ StatementHandler handler = configuration.newStatementHandler(this, ms, parameter, null);
+ stmt = prepareStatement(handler);
+ return handler.query(stmt);
+ } finally {
+ StatementUtil.closeStatement(stmt);
+ }
+ }
+
private Statement prepareStatement(StatementHandler handler) throws SQLException {
Statement stmt;
Connection connection = configuration.getDataSource().getConnection();
diff --git a/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
new file mode 100644
index 0000000..b5aca6c
--- /dev/null
+++ b/src/main/java/org/apache/ibatis/executor/resultset/DefaultResultSetHandler.java
@@ -0,0 +1,141 @@
+package org.apache.ibatis.executor.resultset;
+
+import java.lang.reflect.Field;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+import org.apache.commons.lang3.exception.ExceptionUtils;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.mapping.ResultMap;
+import org.apache.ibatis.session.Configuration;
+import org.apache.ibatis.type.TypeHandler;
+import org.apache.ibatis.type.TypeHandlerRegistry;
+
+/**
+ * @author furious 2024/4/12
+ */
+public class DefaultResultSetHandler implements ResultSetHandler {
+
+ private final Configuration configuration;
+ private final MappedStatement mappedStatement;
+ private final TypeHandlerRegistry typeHandlerRegistry;
+
+ public DefaultResultSetHandler(MappedStatement mappedStatement) {
+ this.configuration = mappedStatement.getConfiguration();
+ this.mappedStatement = mappedStatement;
+ this.typeHandlerRegistry = configuration.getTypeHandlerRegistry();
+ }
+
+ @Override
+ public List handleResultSets(Statement stmt) throws SQLException {
+ final List