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 { + @Override + protected void setNonNullParameter(PreparedStatement ps, int i, Object parameter) throws SQLException { + ps.setObject(i, parameter); + } +} diff --git a/src/main/java/org/apache/ibatis/type/StringTypeHandler.java b/src/main/java/org/apache/ibatis/type/StringTypeHandler.java new file mode 100644 index 0000000..861b81e --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/StringTypeHandler.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 StringTypeHandler extends BaseTypeHandler { + @Override + protected void setNonNullParameter(PreparedStatement ps, int i, String parameter) throws SQLException { + ps.setString(i, parameter); + } +} diff --git a/src/main/java/org/apache/ibatis/type/TypeHandler.java b/src/main/java/org/apache/ibatis/type/TypeHandler.java new file mode 100644 index 0000000..ec0a233 --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/TypeHandler.java @@ -0,0 +1,14 @@ +package org.apache.ibatis.type; + +import java.sql.PreparedStatement; +import java.sql.SQLException; + +/** + * 参数类型处理接口 + * + * @author furious 2024/4/9 + */ +public interface TypeHandler { + + void setParameter(PreparedStatement ps, int i, T parameter) throws SQLException; +} diff --git a/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java b/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java new file mode 100644 index 0000000..8f640fe --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java @@ -0,0 +1,35 @@ +package org.apache.ibatis.type; + +import java.util.HashMap; +import java.util.Map; + +/** + * @author furious 2024/4/9 + */ +public class TypeHandlerRegistry { + + private final TypeHandler unknownTypeHandler = new UnknownTypeHandler(this); + private final Map, TypeHandler> allTypeHandlersMap = new HashMap<>(); + + public TypeHandlerRegistry() { + register(String.class, new StringTypeHandler()); + + register(Integer.class, new IntegerTypeHandler()); + register(int.class, new IntegerTypeHandler()); + + register(Object.class, unknownTypeHandler); + } + + public void register(Class javaType, TypeHandler typeHandler) { + allTypeHandlersMap.put(javaType, typeHandler); + } + + @SuppressWarnings("unchecked") + public TypeHandler getTypeHandler(Class type) { + return (TypeHandler) allTypeHandlersMap.get(type); + } + + public TypeHandler getUnknownTypeHandler() { + return unknownTypeHandler; + } +} diff --git a/src/main/java/org/apache/ibatis/type/UnknownTypeHandler.java b/src/main/java/org/apache/ibatis/type/UnknownTypeHandler.java new file mode 100644 index 0000000..95492ed --- /dev/null +++ b/src/main/java/org/apache/ibatis/type/UnknownTypeHandler.java @@ -0,0 +1,29 @@ +package org.apache.ibatis.type; + +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Objects; + +/** + * @author furious 2024/4/9 + */ +public class UnknownTypeHandler extends BaseTypeHandler { + + private static final ObjectTypeHandler OBJECT_TYPE_HANDLER = new ObjectTypeHandler(); + + private final TypeHandlerRegistry typeHandlerRegistry; + + public UnknownTypeHandler(TypeHandlerRegistry typeHandlerRegistry) { + this.typeHandlerRegistry = typeHandlerRegistry; + } + + @Override + @SuppressWarnings({"rawtypes", "unchecked"}) + protected void setNonNullParameter(PreparedStatement ps, int i, Object parameter) throws SQLException { + TypeHandler handler = typeHandlerRegistry.getTypeHandler(parameter.getClass()); + if (Objects.isNull(handler) || handler instanceof UnknownTypeHandler) { + handler = OBJECT_TYPE_HANDLER; + } + handler.setParameter(ps, i, parameter); + } +}