java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > mybatis3.4.0不支持LocalDateTime

mybatis3.4.0不支持LocalDateTime的解决方法(No typehandler found for property time)

作者:weixin_43833540

本文主要介绍了mybatis3.4.0不支持LocalDateTime的解决方法(No typehandler found for property time),具有一定的参考价值,感兴趣的可以了解一下

问题描述

报错:No typehandler found for property time
(注:time是LocalDateTime类型的字段)

LocalDateTimeTypeHandler

public class LocalDateTimeTypeHandler implements TypeHandler<LocalDateTime> {

    private DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Override
    public void setParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException {
        if (parameter != null) {
            ps.setString(i, formatter.format(parameter));
        } else {
            ps.setTimestamp(i, null);
        }
    }

    @Override
    public LocalDateTime getResult(ResultSet rs, String columnName) throws SQLException {
        String dateStr = rs.getString(columnName);
        return dateStr == null ? null : LocalDateTime.parse(dateStr, formatter);
    }

    @Override
    public LocalDateTime getResult(ResultSet rs, int columnIndex) throws SQLException {
        String dateStr = rs.getString(columnIndex);
        return dateStr == null ? null : LocalDateTime.parse(dateStr, formatter);
    }

    @Override
    public LocalDateTime getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String dateStr = cs.getString(columnIndex);
        return dateStr == null ? null : LocalDateTime.parse(dateStr, formatter);
    }
}

mybatis-config.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>

    </settings>
    <typeHandlers>
        <typeHandler handler="com.vanwardsmart.mybatis.LocalDateTimeTypeHandler" javaType="java.time.LocalDateTime"/>
    </typeHandlers>
</configuration>

到此这篇关于mybatis3.4.0不支持LocalDateTime的解决方法(No typehandler found for property time)的文章就介绍到这了,更多相关mybatis3.4.0不支持LocalDateTime内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家! 

您可能感兴趣的文章:
阅读全文