mybatis涓璬atetime杞琩ate鎬庝箞瀹炵幇
鍦∕yBatis涓皢鏁版嵁搴撲腑鐨凞atetime绫诲瀷杞崲涓篋ate绫诲瀷鍙互閫氳繃浣跨敤TypeHandler鏉ュ疄鐜般€備互涓嬫槸涓€涓ず渚嬩唬鐮侊細
棣栧厛鍒涘缓涓€涓嚜瀹氫箟鐨凾ypeHandler绫伙紝缁ф壙鑷狟aseTypeHandler 鐒跺悗鍦∕yBatis鐨勯厤缃枃浠朵腑娉ㄥ唽璇ypeHandler锛?/p>
鏈€鍚庡湪瀵瑰簲鐨凪apper XML鏂囦欢涓娇鐢ㄨTypeHandler锛?/p>
杩欐牱灏卞彲浠ュ湪MyBatis涓皢鏁版嵁搴撲腑鐨凞atetime绫诲瀷鑷姩杞崲涓篋ate绫诲瀷銆?/p>
public class DateTimeToDateTypeHandler extends BaseTypeHandler<Date> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
ps.setTimestamp(i, new Timestamp(parameter.getTime()));
}
@Override
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName);
return timestamp != null ? new Date(timestamp.getTime()) : null;
}
@Override
public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex);
return timestamp != null ? new Date(timestamp.getTime()) : null;
}
@Override
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp timestamp = cs.getTimestamp(columnIndex);
return timestamp != null ? new Date(timestamp.getTime()) : null;
}
}
<typeHandlers>
<typeHandler handler="com.example.DateTimeToDateTypeHandler"/>
</typeHandlers>
<resultMap id="myResultMap" type="com.example.MyEntity">
<result property="date" column="datetime" javaType="java.util.Date" typeHandler="com.example.DateTimeToDateTypeHandler"/>
</resultMap>
相关问答