mysql日期存储为int,mybatis做ORM映射与java.util.Date的转换问题

在mysql做数据库的应用中,日期类型经常回存储为int(10)类型。方便排序和计算。但是在java中用Date.getTime返回的是13位的Long。并且在实体中我们如果用long来存储会有诸多不便。所以涉及到了转换问题。在我的项目中,用的是mybatis做持久性框架。对于这个问题用了以下方法处理。

  1. 用mybatis generate时,配置实体中用date来覆盖数据库中的int类型。
<columnOverride column="birthday" javaType="java.util.Date" jdbcType="INTEGER" />  
  1. 自定义TypeHandler实现TypeHandler接口
@MappedTypes({ Date.class })  
@MappedJdbcTypes({ JdbcType.INTEGER })  
public class DateIntTypeHandler implements TypeHandler<Date> {  

    @Override  
    public void setParameter(PreparedStatement ps, int i, Date parameter,  
            JdbcType jdbcType) throws SQLException {  
        if (parameter == null) {  
            if (jdbcType == null) {  
                throw new TypeException(  
                        "JDBC requires that the JdbcType must be specified for all nullable parameters.");  
            }  
            try {  
                ps.setNull(i, jdbcType.TYPE_CODE);  
            } catch (SQLException e) {  
                throw new TypeException(  
                        "Error setting null for parameter #"  
                                + i  
                                + " with JdbcType "  
                                + jdbcType  
                                + " . "  
                                + "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "  
                                + "Cause: " + e, e);  
            }  
        } else {  
            int time = (int) (parameter.getTime() / 1000);  
            ps.setInt(i, time);  
        }  
    }  

    @Override  
    public Date getResult(ResultSet rs, String columnName) throws SQLException {  
        int res = rs.getInt(columnName);  
        long time = res * 1000L;  
        return new Date(time);  
    }  

    @Override  
    public Date getResult(ResultSet rs, int columnIndex) throws SQLException {  
        int res = rs.getInt(columnIndex);  
        long time = res * 1000L;  
        return new Date(time);  
    }  

    @Override  
    public Date getResult(CallableStatement cs, int columnIndex)  
            throws SQLException {  
        int res = cs.getInt(columnIndex);  
        long time = res * 1000L;  
        return new Date(time);  
    }  
}  
  1. 在mybatis的配置中引用自定义的转换处理类
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:sqlmap/mysql/module/*/*.xml"></property>  
        <strong><property name="typeHandlers">  
            <array>  
                <bean name="dateIntTypeHandler" class="*.utils.mybatis.DateIntTypeHandler" />  
            </array>  
        </property></strong>  
    </bean>  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值