Mybatis類型轉換介紹

1.介紹:java

  創建TypeHandler來對特殊的javaType和jdbcType的轉換sql

2.經常使用的類型轉化:mybatis

 1 register(Boolean.class, new BooleanTypeHandler());  
 2 register(boolean.class, new BooleanTypeHandler());  
 3 register(Byte.class, new ByteTypeHandler());  
 4 register(byte.class, new ByteTypeHandler());  
 5 register(Short.class, new ShortTypeHandler());  
 6 register(short.class, new ShortTypeHandler());  
 7 register(Integer.class, new IntegerTypeHandler());  
 8 register(int.class, new IntegerTypeHandler());  
 9 register(Long.class, new LongTypeHandler());  
10 register(long.class, new LongTypeHandler());  
11 register(Float.class, new FloatTypeHandler());  
12 register(float.class, new FloatTypeHandler());  
13 register(Double.class, new DoubleTypeHandler());  
14 register(double.class, new DoubleTypeHandler());  
15 register(String.class, new StringTypeHandler());  
16 register(String.class, JdbcType.CHAR, new StringTypeHandler());  
17 register(String.class, JdbcType.CLOB, new ClobTypeHandler());  
18 register(String.class, JdbcType.VARCHAR, new StringTypeHandler());  
19 register(String.class, JdbcType.LONGVARCHAR, new ClobTypeHandler());  
20 register(String.class, JdbcType.NVARCHAR, new NStringTypeHandler());  
21 register(String.class, JdbcType.NCHAR, new NStringTypeHandler());  
22 register(String.class, JdbcType.NCLOB, new NClobTypeHandler());  
23 register(Object.class, JdbcType.ARRAY, new ArrayTypeHandler());  
24 register(BigInteger.class, new BigIntegerTypeHandler());  
25 register(BigDecimal.class, new BigDecimalTypeHandler());  
26 register(Byte[].class, new ByteObjectArrayTypeHandler());  
27 register(Byte[].class, JdbcType.BLOB, new BlobByteObjectArrayTypeHandler());  
28 register(Byte[].class, JdbcType.LONGVARBINARY, new BlobByteObjectArrayTypeHandler());  
29 register(byte[].class, new ByteArrayTypeHandler());  
30 register(byte[].class, JdbcType.BLOB, new BlobTypeHandler());  
31 register(byte[].class, JdbcType.LONGVARBINARY, new BlobTypeHandler());  
32 register(Object.class, UNKNOWN_TYPE_HANDLER);  
33 register(Object.class, JdbcType.OTHER, UNKNOWN_TYPE_HANDLER);  
34 register(Date.class, new DateTypeHandler());  
35 register(Date.class, JdbcType.DATE, new DateOnlyTypeHandler());  
36 register(Date.class, JdbcType.TIME, new TimeOnlyTypeHandler());  
37 register(java.sql.Date.class, new SqlDateTypeHandler());  
38 register(java.sql.Time.class, new SqlTimeTypeHandler());  
39 register(java.sql.Timestamp.class, new SqlTimestampTypeHandler());  
40 register(Character.class, new CharacterTypeHandler());  
41 register(char.class, new CharacterTypeHandler());  

3.實現app

  3.1 實現TypeHandler接口或者繼承BaseTypeHandler抽象類spa

  •   BaseTypeHandler主要是在TypeHandler接口的基礎上,對數據進行判空處理,而且繼承了抽象類TypeReference。
  •   TypeReference能夠經過getRawType()方法能夠獲取到當前TypeHandler所使用泛型的原始類型

  3.2 簡單介紹code

    TypeHandler<T>接口 : xml

      setParameter(PreparedStatement ps, int i, T parameter,JdbcType jdbcType)//實現對配置的javaType類型轉換成jdbcType類型的處理blog

      getResult(ResultSet rs, String columnName)//如下三種方式對應 jdbcType類型轉換成javaType類型的處理繼承

      getResult(ResultSet rs, int columnIndex)接口

      getResult(CallableStatement cs, int columnIndex)

    BaseTypeHandler<T>抽象類:(推薦使用此種方式)

      setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType)//實現對配置的javaType類型轉換成jdbcType類型的處理

      getNullableResult(ResultSet rs, String columnName)//如下三種方式對應 jdbcType類型轉換成javaType類型的處理

      getNullableResult(ResultSet rs, int columnIndex)

      getNullableResult(CallableStatement cs, int columnIndex)

  3.3 配置方式

    3.3.1 純xml

<!-- mybatis.xml配置 -->
<typeHandlers>  
       <typeHandler handler="com.*.StringArrayTypeHandler" javaType="[Ljava.lang.String;" jdbcType="VARCHAR"/>  
</typeHandlers> 

    3.3.2 註解與xml配置

<typeHandlers>  
   <typeHandler handler="com.*.StringArrayTypeHandler"/>  
</typeHandlers>

@MappedTypes({String[].class})  
@MappedJdbcTypes({JdbcType.VARCHAR})  
public class StringArrayTypeHandler implements BaseTypeHandler<String[]>   

    3.3.3 掃描包下的註解,實現類須要包含@MappedJdbcTypes和@MappedTypes

<typeHandlers>  
   <package name="com.*"/>  
</typeHandlers>
相關文章
相關標籤/搜索