MyBatis不能与joda DateTime一起工作



我正在使用MyBatis 3.2,并希望将Oracle DATE数据类型映射到Java org.joda.time.DateTime数据类型。

下面是我的配置:

<resultMap id="something" type="com.myself.SomeClass">
  <result property="creationDate" column="CREATION_DATE" javaType="org.joda.time.DateTime" />
</resultMap>

但我得到以下错误:

Caused by: org.apache.ibatis.builder.BuilderException
         : Error parsing SQL Mapper Configuration. 
Cause    : org.apache.ibatis.builder.BuilderException
         : Error parsing Mapper XML. 
Cause    : java.lang.IllegalStateException
         : No typehandler found for property creationDate

我的配置是否正确?还是因为我的Oracle数据类型是DATE而不是DATETIME ?MyBatis支持joda DateTime吗?

我认为你需要使用TypeResolver来处理JodaTime到Oracle DateTime。我在MyBatis和MySQL中有过这样的经验,你可能会使用这个指南来帮助你解决你的问题。

我使用这个github项目作为如何使用TypeResolver的指南:https://github.com/LukeL99/joda-time-mybatis

在其中一个映射器中,我有这样的代码:

<result column="expiryDate" property="expiryDate" javaType="org.joda.time.DateTime" typeHandler="org.joda.time.mybatis.handlers.DateTimeTypeHandler"/>

org.joda.time.mybatis.handlers.DateTimeTypeHandler是我发布的github项目中的类。

有组织。mybatis:mybatis-typehandlers-jsr310为java 8日期时间支持。
我已经将所需的类复制到我的项目中,并将它们移植到JodaTime中。之后,您可以像往常一样在选择语句和结果定义中使用类型处理程序。

@Select("select p.transaction_id from dly_mstr_curr_trd_prg p "
        + "where p.start_time < #{threshold, typeHandler=org.apache.ibatis.type.LocalDateTimeTypeHandler} "
        + "and failure_reason is null")
Collection<BigDecimal> selectStaleNotFailedTransactions(@Param("threshold") LocalDateTime threshold);

@Result(property = "transaction.transactionPostingDate", column = "TXN_PSTG_D", typeHandler=LocalDateTimeTypeHandler.class),
下面是移植类的例子:
/**
 * @author Tomas Rohovsky
 */
public class LocalDateTypeHandler extends BaseTypeHandler<LocalDate> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, LocalDate parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setDate(i, Date.valueOf(parameter.toString("yyyy-MM-dd HH:mm:ss")));
    }
    @Override
    public LocalDate getNullableResult(ResultSet rs, String columnName) throws SQLException {
        Date date = rs.getDate(columnName);
        return getLocalDate(date);
    }
    @Override
    public LocalDate getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        Date date = rs.getDate(columnIndex);
        return getLocalDate(date);
    }
    @Override
    public LocalDate getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        Date date = cs.getDate(columnIndex);
        return getLocalDate(date);
    }
    private static LocalDate getLocalDate(Date date) {
        return date == null ? null : LocalDate.parse(date.toLocalDate().toString());
    }
}

我想MyBatis不支持Joda DateTime。这可能是由于mysql无法将Joda DateTime对象序列化为JDBC sql的正确格式造成的。所以,你必须使用Java类型"Date"

最新更新