ibatis时间转换源码分析

package com.ibatis.sqlmap.engine.mapping.parameter

setParameters()中setParameter()加载了TypeHandler

对于java.util.date:

 public void setParameter(PreparedStatement ps, int i, Object parameter, String jdbcType)
      throws SQLException {
    ps.setTimestamp(i, new java.sql.Timestamp(((Date) parameter).getTime()));
  }

  public Object getResult(ResultSet rs, String columnName)
      throws SQLException {
    java.sql.Timestamp sqlTimestamp = rs.getTimestamp(columnName);
    if (rs.wasNull()) {
      return null;
    } else {
      return new java.util.Date(sqlTimestamp.getTime());
    }
  }

 

对于java.sql.Timestamp

public class SqlTimestampTypeHandler extends BaseTypeHandler implements TypeHandler {

  private static final String DATE_FORMAT = "yyyy/MM/dd hh:mm:ss";

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

  public Object getResult(ResultSet rs, String columnName)
      throws SQLException {
    Object sqlTimestamp = rs.getTimestamp(columnName);
    if (rs.wasNull()) {
      return null;
    } else {
      return sqlTimestamp;
    }
  }
...
}

 

posted @ 2018-11-26 00:59  五岳  阅读(190)  评论(0编辑  收藏  举报
回到顶部