ibatis返回Date不带时分秒

ibatis从oracle查询的时候,返回的类型是java.sql.Date,这个类型是不带时分秒的,我们只有在写sql的时候用to_char来转换。

我在项目中是返回的HashMap

<select id="" parameterClass="map" resultClass="java.util.HashMap">


实际上是可以解决的,通过配置一个typeHandler,让TypeHandler在转换的时候把java.sql.Date转换成java.sql.Timestamp,这样就能返回精确的时间了

在sqlMapConfig中配一下:


<typeHandler javaType="object" callback="xxx.xxx.OracleObjectTypeHandler"/>

代码如下

public class OracleObjectTypeHandler extends BaseTypeHandler implements
		TypeHandler {
	public Object getResult(ResultSet rs, String columnName)
			throws SQLException {
		Object object = rs.getObject(columnName);
		if (rs.wasNull()) {
			return null;
		} else {
			boolean b = object instanceof java.sql.Date;
			if (b)
				object = new Date(rs.getTimestamp(columnName).getTime());
			return object;
		}
	}

	public Object getResult(ResultSet rs, int columnIndex) throws SQLException {
		Object object = rs.getObject(columnIndex);
		if (rs.wasNull()) {
			return null;
		} else {
			boolean b = object instanceof java.sql.Date;
			if (b)
				object = new Date(rs.getTimestamp(columnIndex).getTime());
			return object;
		}
	}

	public Object getResult(CallableStatement cs, int columnIndex)
			throws SQLException {
		Object object = cs.getObject(columnIndex);
		if (cs.wasNull()) {
			return null;
		} else {
			boolean b = object instanceof java.sql.Date;
			if (b)
				object = new Date(cs.getTimestamp(columnIndex).getTime());
			return object;
		}
	}

	public Object valueOf(String s) {
		return s;
	}

	public void setParameter(PreparedStatement ps, int i, Object parameter,
			String jdbcType) throws SQLException {
		ps.setObject(i, parameter);
	}
}


你可能感兴趣的:(oracle,Date,ibatis,HashMap,map)