package com.hx.mybatis.date.handler; 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 java.sql.*; import java.time.LocalDateTime; /** * @author CJH * @Date 2021-01-02 * // @MappedTypes注解中的类代表此转换器可以自动转换为的java对象,@MappedJdbcTypes注解中设置的是对应的jdbctype,mysql的json对象对应的jdbctype为VARCHAR。 */ @MappedTypes(value = {LocalDateTime.class}) @MappedJdbcTypes(value = {JdbcType.TIMESTAMP}, includeNullJdbcType = true) public class GenericDateHandler extends BaseTypeHandler { public GenericDateHandler() { } public void setNonNullParameter(PreparedStatement ps, int i, Timestamp parameter, JdbcType jdbcType) throws SQLException { ps.setTimestamp(i, parameter); } public Timestamp getNullableResult(ResultSet rs, String columnName) throws SQLException { return rs.getTimestamp(columnName); } public Timestamp getNullableResult(ResultSet rs, int columnIndex) throws SQLException { return rs.getTimestamp(columnIndex); } public Timestamp getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { return cs.getTimestamp(columnIndex); } }