扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

mybatis涓璬atetime杞琩ate鎬庝箞瀹炵幇

扬州沐宇科技
2024-06-06 16:27:24
mybatis

鍦∕yBatis涓皢鏁版嵁搴撲腑鐨凞atetime绫诲瀷杞崲涓篋ate绫诲瀷鍙互閫氳繃浣跨敤TypeHandler鏉ュ疄鐜般€備互涓嬫槸涓€涓ず渚嬩唬鐮侊細

棣栧厛鍒涘缓涓€涓嚜瀹氫箟鐨凾ypeHandler绫伙紝缁ф壙鑷狟aseTypeHandler锛屽疄鐜拌嚜瀹氫箟鐨勮浆鎹㈤€昏緫锛?/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;
    }
}

鐒跺悗鍦∕yBatis鐨勯厤缃枃浠朵腑娉ㄥ唽璇ypeHandler锛?/p>

<typeHandlers>
    <typeHandler handler="com.example.DateTimeToDateTypeHandler"/>
</typeHandlers>

鏈€鍚庡湪瀵瑰簲鐨凪apper XML鏂囦欢涓娇鐢ㄨTypeHandler锛?/p>

<resultMap id="myResultMap" type="com.example.MyEntity">
    <result property="date" column="datetime" javaType="java.util.Date" typeHandler="com.example.DateTimeToDateTypeHandler"/>
</resultMap>

杩欐牱灏卞彲浠ュ湪MyBatis涓皢鏁版嵁搴撲腑鐨凞atetime绫诲瀷鑷姩杞崲涓篋ate绫诲瀷銆?/p>

扫码添加客服微信