MyBatis中的类型转换机制

自定义了一 个将Date存为毫秒时间的VARCHAR类型的TypeHandler 
1)新建类型转换类,实现TypeHandler接口,接口的泛型指定参数类型 ,重写了setNonNullParameter,getNullableResult方法。
public class CustomTimeStampHandler extends BaseTypeHandler
@Override 
public void setNonNullParameter(PreparedStatement ps, int i, 
Date parameter, JdbcType jdbcType) throws SQLException { 
ps.setString(i, String.valueOf(parameter.getTime())); 
@Override 
public Date getNullableResult(ResultSet rs, String columnName) 
throws SQLException { 
String sqlTimestamp = rs.getString(columnName); 
if (sqlTimestamp != null) { 
return new Date(Long.parseLong(sqlTimestamp)); 
return null; 
@Override 
public Date getNullableResult(ResultSet rs, int columnIndex) 
throws SQLException { 
String sqlTimestamp = rs.getString(columnIndex); 
if (sqlTimestamp != null) { 
return new Date(Long.parseLong(sqlTimestamp)); 
return null; 
@Override 
public Date getNullableResult(CallableStatement cs, int columnIndex) 
throws SQLException { 
String sqlTimestamp = cs.getString(columnIndex); 
if (sqlTimestamp != null) { 
return new Date(Long.parseLong(sqlTimestamp)); 
return null; 
2)在Mybatis 的主配置文件中注册上述定义的类型转换类 
其中jdbcType可以指定的类型在Mybatis的枚举类org.apache.ibatis.type.JdbcType中有明确的定义,不能为该 枚举以外的值, 
不然会出错。如果没有我们需要的类型,可指定为UNDEFINED。(也可以不指定具体的类型,在使用时用typeHandler指定具 
体的类即可): 
 
javaType="java.util.Date" jdbcType="VARCHAR"/> 
 
3)在mapper映射文件中使用自定义的类型转换器 
a) 在 resultMap的定义中对对应列定义typeHandler: 
 
 
typeHandler=""×××.CustomTimeStampHandler"/> 
 
b) 在 update使用则需要在sql定义中添加相应的内容 
 
update note 
set update_time=#{updateTime, javaType=java.Date, jdbcType=VARCHAR} 
where id=#{id} 
 

自定义了一 个将Date存为毫秒时间的VARCHAR类型的TypeHandler 

1)新建类型转换类,实现TypeHandler接口,接口的泛型指定参数类型 ,重写了setNonNullParameter,getNullableResult方法。

public class CustomerTimeHandler extends BaseTypeHandler


@Override 

public void setNonNullParameter(PreparedStatement ps, int i, 

Date parameter, JdbcType jdbcType) throws SQLException { 

ps.setString(i, String.valueOf(parameter.getTime())); 



@Override 

public Date getNullableResult(ResultSet rs, String columnName) 

throws SQLException { 

String sqlTimestamp = rs.getString(columnName); 

if (sqlTimestamp != null) { 

return new Date(Long.parseLong(sqlTimestamp)); 

return null; 


@Override 

public Date getNullableResult(ResultSet rs, int columnIndex) 

throws SQLException { 

String sqlTimestamp = rs.getString(columnIndex); 

if (sqlTimestamp != null) { 

return new Date(Long.parseLong(sqlTimestamp)); 

return null; 


@Override 

public Date getNullableResult(CallableStatement cs, int columnIndex) 

throws SQLException { 

String sqlTimestamp = cs.getString(columnIndex); 

if (sqlTimestamp != null) { 

return new Date(Long.parseLong(sqlTimestamp)); 

return null; 



2)在Mybatis 的主配置文件中注册上述定义的类型转换类 

其中jdbcType可以指定的类型在Mybatis的枚举类org.apache.ibatis.type.JdbcType中有明确的定义,不能为该 枚举以外的值, 

不然会出错。如果没有我们需要的类型,可指定为UNDEFINED。(也可以不指定具体的类型,在使用时用typeHandler指定具 

体的类即可): 

 

javaType="java.util.Date" jdbcType="VARCHAR"/> 

 


3)在mapper映射文件中使用自定义的类型转换器 

a) 在 resultMap的定义中对对应列定义typeHandler: 

 

 

typeHandler=""×××.CustomerTimeHandler"/> 

 


b) 在 update使用则需要在sql定义中添加相应的内容 

 

update customer

set update_time=#{customerTime, javaType=java.Date, jdbcType=VARCHAR} 

where id=#{id} 

 


你可能感兴趣的:(spring&mybatis)