From 94cf9bbaeb459d6aa06fb8d0a648e5c9f4d667e2 Mon Sep 17 00:00:00 2001
From: FuriousPws002 <1938485828@qq.com>
Date: Tue, 9 Apr 2024 23:18:58 +0800
Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0TypeHandler=E5=A4=84?=
=?UTF-8?q?=E7=90=86=E5=99=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
pom.xml | 5 +++
.../apache/ibatis/session/Configuration.java | 6 ++++
.../apache/ibatis/type/BaseTypeHandler.java | 31 ++++++++++++++++
.../ibatis/type/IntegerTypeHandler.java | 14 ++++++++
.../apache/ibatis/type/ObjectTypeHandler.java | 14 ++++++++
.../apache/ibatis/type/StringTypeHandler.java | 14 ++++++++
.../org/apache/ibatis/type/TypeHandler.java | 14 ++++++++
.../ibatis/type/TypeHandlerRegistry.java | 35 +++++++++++++++++++
.../ibatis/type/UnknownTypeHandler.java | 29 +++++++++++++++
9 files changed, 162 insertions(+)
create mode 100644 src/main/java/org/apache/ibatis/type/BaseTypeHandler.java
create mode 100644 src/main/java/org/apache/ibatis/type/IntegerTypeHandler.java
create mode 100644 src/main/java/org/apache/ibatis/type/ObjectTypeHandler.java
create mode 100644 src/main/java/org/apache/ibatis/type/StringTypeHandler.java
create mode 100644 src/main/java/org/apache/ibatis/type/TypeHandler.java
create mode 100644 src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
create mode 100644 src/main/java/org/apache/ibatis/type/UnknownTypeHandler.java
diff --git a/pom.xml b/pom.xml
index d0141f3..1c8135f 100755
--- a/pom.xml
+++ b/pom.xml
@@ -29,6 +29,11 @@
HikariCP
3.4.5
+
+ org.apache.commons
+ commons-lang3
+ 3.14.0
+
diff --git a/src/main/java/org/apache/ibatis/session/Configuration.java b/src/main/java/org/apache/ibatis/session/Configuration.java
index a98e2ba..7417149 100644
--- a/src/main/java/org/apache/ibatis/session/Configuration.java
+++ b/src/main/java/org/apache/ibatis/session/Configuration.java
@@ -14,6 +14,7 @@
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
+import org.apache.ibatis.type.TypeHandlerRegistry;
/**
* mybatis全局配置类
@@ -27,6 +28,7 @@ public class Configuration {
private final MapperRegistry mapperRegistry = new MapperRegistry(this);
private final Set loadedResources = new HashSet<>();
private final Map mappedStatements = new HashMap<>();
+ private final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();
public Configuration() {
}
@@ -74,4 +76,8 @@ public void setDataSource(DataSource dataSource) {
public StatementHandler newStatementHandler(Executor executor, MappedStatement mappedStatement, Object parameterObject, BoundSql boundSql) {
return new PrepareStatementHandler(executor, mappedStatement, parameterObject, boundSql);
}
+
+ public TypeHandlerRegistry getTypeHandlerRegistry() {
+ return typeHandlerRegistry;
+ }
}
diff --git a/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java b/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java
new file mode 100644
index 0000000..ee09939
--- /dev/null
+++ b/src/main/java/org/apache/ibatis/type/BaseTypeHandler.java
@@ -0,0 +1,31 @@
+package org.apache.ibatis.type;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+import java.sql.Types;
+import java.util.Objects;
+
+/**
+ * @author furious 2024/4/9
+ */
+public abstract class BaseTypeHandler implements TypeHandler {
+
+ @Override
+ public void setParameter(PreparedStatement ps, int i, T parameter) throws SQLException {
+ if (Objects.isNull(parameter)) {
+ ps.setNull(i, Types.NULL);
+ return;
+ }
+ setNonNullParameter(ps, i, parameter);
+ }
+
+ /**
+ * 设置非空值
+ *
+ * @param ps PreparedStatement
+ * @param i 1开始
+ * @param parameter 参数值
+ * @throws SQLException SQLException
+ */
+ protected abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter) throws SQLException;
+}
diff --git a/src/main/java/org/apache/ibatis/type/IntegerTypeHandler.java b/src/main/java/org/apache/ibatis/type/IntegerTypeHandler.java
new file mode 100644
index 0000000..3c86824
--- /dev/null
+++ b/src/main/java/org/apache/ibatis/type/IntegerTypeHandler.java
@@ -0,0 +1,14 @@
+package org.apache.ibatis.type;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+/**
+ * @author furious 2024/4/9
+ */
+public class IntegerTypeHandler extends BaseTypeHandler {
+ @Override
+ protected void setNonNullParameter(PreparedStatement ps, int i, Integer parameter) throws SQLException {
+ ps.setInt(i, parameter);
+ }
+}
diff --git a/src/main/java/org/apache/ibatis/type/ObjectTypeHandler.java b/src/main/java/org/apache/ibatis/type/ObjectTypeHandler.java
new file mode 100644
index 0000000..8e5a302
--- /dev/null
+++ b/src/main/java/org/apache/ibatis/type/ObjectTypeHandler.java
@@ -0,0 +1,14 @@
+package org.apache.ibatis.type;
+
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
+
+/**
+ * @author furious 2024/4/9
+ */
+public class ObjectTypeHandler extends BaseTypeHandler