-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
efc849e
commit 94cf9bb
Showing
9 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/org/apache/ibatis/type/IntegerTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/org/apache/ibatis/type/ObjectTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
src/main/java/org/apache/ibatis/type/StringTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
src/main/java/org/apache/ibatis/type/UnknownTypeHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |