Skip to content

Commit

Permalink
添加TypeHandler处理器
Browse files Browse the repository at this point in the history
  • Loading branch information
FuriousPws002 committed Apr 9, 2024
1 parent efc849e commit 94cf9bb
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<artifactId>HikariCP</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.14.0</version>
</dependency>
</dependencies>

<build>
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/apache/ibatis/session/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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全局配置类
Expand All @@ -27,6 +28,7 @@ public class Configuration {
private final MapperRegistry mapperRegistry = new MapperRegistry(this);
private final Set<String> loadedResources = new HashSet<>();
private final Map<String, MappedStatement> mappedStatements = new HashMap<>();
private final TypeHandlerRegistry typeHandlerRegistry = new TypeHandlerRegistry();

public Configuration() {
}
Expand Down Expand Up @@ -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;
}
}
31 changes: 31 additions & 0 deletions src/main/java/org/apache/ibatis/type/BaseTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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<T> implements TypeHandler<T> {

@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;
}
14 changes: 14 additions & 0 deletions src/main/java/org/apache/ibatis/type/IntegerTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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<Integer> {
@Override
protected void setNonNullParameter(PreparedStatement ps, int i, Integer parameter) throws SQLException {
ps.setInt(i, parameter);
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/apache/ibatis/type/ObjectTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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<Object> {
@Override
protected void setNonNullParameter(PreparedStatement ps, int i, Object parameter) throws SQLException {
ps.setObject(i, parameter);
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/apache/ibatis/type/StringTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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<String> {
@Override
protected void setNonNullParameter(PreparedStatement ps, int i, String parameter) throws SQLException {
ps.setString(i, parameter);
}
}
14 changes: 14 additions & 0 deletions src/main/java/org/apache/ibatis/type/TypeHandler.java
Original file line number Diff line number Diff line change
@@ -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<T> {

void setParameter(PreparedStatement ps, int i, T parameter) throws SQLException;
}
35 changes: 35 additions & 0 deletions src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
Original file line number Diff line number Diff line change
@@ -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<Object> unknownTypeHandler = new UnknownTypeHandler(this);
private final Map<Class<?>, 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 <T> void register(Class<T> javaType, TypeHandler<? extends T> typeHandler) {
allTypeHandlersMap.put(javaType, typeHandler);
}

@SuppressWarnings("unchecked")
public <T> TypeHandler<T> getTypeHandler(Class<T> type) {
return (TypeHandler<T>) allTypeHandlersMap.get(type);
}

public TypeHandler<Object> getUnknownTypeHandler() {
return unknownTypeHandler;
}
}
29 changes: 29 additions & 0 deletions src/main/java/org/apache/ibatis/type/UnknownTypeHandler.java
Original file line number Diff line number Diff line change
@@ -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<Object> {

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);
}
}

0 comments on commit 94cf9bb

Please sign in to comment.