优米格
分享有营养的

Mybatis使用自定义TypeHandler解析XMLGregorianCalendar日期格式为java.sql.Date

BraUndress.png

使用Mybatis将Java实体类映射到Oracle数据库,实体类中属性createdatejavax.xml.datatype.XMLGregorianCalendar类型。项目启动是对Mapper文件解析时报错,提示如下:

17:23:04.445 [main] [] ERROR c.s.c.u.m.SqlSessionFactoryBean - Failed to parse mapping resource: 'file [C:\workspace\SD_IERP\target\classes\com\sinoprof\sscm\importShipAdviceInfoRouteSrv\dao\oracle\ImportShipAdviceInfoRouteSrvDaoMapper.xml]'
17:23:06.679 [main] [] ERROR c.s.c.utils.myBatis.XMLMapperBuilder - Error parsing Mapper XML. Cause: java.lang.IllegalStateException: Type handler was null on parameter mapping for property 'createdate'.  It was either not specified and/or could not be found for the javaType / jdbcType combination specified.

默认的类型处理器无法实现对javax.xml.datatype.XMLGregorianCalendar类型到java.sql.Timestamp或者java.sql.Date直接的映射。其中javax.xml.datatype.XMLGregorianCalendar类型的传参格式为2013-05-31T14:58:31.609+08:00

自定义TypeHandler的方式如下:

1.代码

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.sql.*;
import java.util.Date;
import java.util.GregorianCalendar;

@MappedJdbcTypes(JdbcType.TIMESTAMP)
@MappedTypes(Timestamp.class)
public class GregorianCalendarToDateTypeHandler extends BaseTypeHandler<XMLGregorianCalendar> {
    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, XMLGregorianCalendar xmlGregorianCalendar, JdbcType jdbcType) throws SQLException {
        long time = xmlGregorianCalendar.toGregorianCalendar().getTime().getTime();
        java.sql.Timestamp sqlDate = new java.sql.Timestamp(time);
        preparedStatement.setTimestamp(i, sqlDate);
    }

    @Override
    public XMLGregorianCalendar getNullableResult(ResultSet resultSet, String s) throws SQLException {
        java.sql.Timestamp sqlDate = resultSet.getTimestamp(s);
        if (sqlDate != null) {
            return date2XMLGregorianCalendar(sqlDate);
        }
        return null;
    }

    @Override
    public XMLGregorianCalendar getNullableResult(ResultSet resultSet, int i) throws SQLException {
        java.sql.Timestamp sqlDate = resultSet.getTimestamp(i);

        if (sqlDate != null) {
            return date2XMLGregorianCalendar(sqlDate);
        }
        return null;
    }

    @Override
    public XMLGregorianCalendar getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        java.sql.Timestamp sqlDate = callableStatement.getTimestamp(i);
        if (sqlDate != null) {
            return date2XMLGregorianCalendar(sqlDate);
        }
        return null;
    }

    /**
     * java.sql.Timestamp格式转换为javax.xml.datatype.XMLGregorianCalendar格式
     * @param sqlDate
     * @return
     */
    private XMLGregorianCalendar date2XMLGregorianCalendar(java.sql.Timestamp sqlDate) {
        Date date = new Date(sqlDate.getTime());
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTime(date);
        XMLGregorianCalendar xmlDate = null;
        try {
            xmlDate = DatatypeFactory.newInstance().newXMLGregorianCalendar(gc);
        } catch (DatatypeConfigurationException e) {
            throw new RuntimeException(e);
        }
        return xmlDate;
    }
}

2.自定义TypeHandler使用

插入数据:

<insert id="insertShipAdviceInfoRouteSrv" parameterType="wsstub.sscm.importShipAdviceInfoRouteSrv.INPUTCOLLECTIONITEM" >
    insert into SOA_SHIP_ADVICE_INFO_ROUTE
    (pri_key,
     create_date,
     send_time,
     expect_time,
     suggest_time)
    values
        (#{prikey},
         #{createdate, typeHandler=com.sinoprof.core.myBatis.GregorianCalendarToDateTypeHandler},
         #{sendtime, typeHandler=com.sinoprof.core.myBatis.GregorianCalendarToDateTypeHandler},
         #{expecttime, typeHandler=com.sinoprof.core.myBatis.GregorianCalendarToDateTypeHandler},
         #{suggesttime, typeHandler=com.sinoprof.core.myBatis.GregorianCalendarToDateTypeHandler}
</insert>
赞(1)
未经允许禁止转载:优米格 » Mybatis使用自定义TypeHandler解析XMLGregorianCalendar日期格式为java.sql.Date

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系