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