mybatis localdatetime 사용하기
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <typeHandlers> <typeHandler handler="org.apache.ibatis.type.InstantTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalDateTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.LocalTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.OffsetDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.OffsetTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.ZonedDateTimeTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.YearTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.MonthTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.YearMonthTypeHandler" /> <typeHandler handler="org.apache.ibatis.type.JapaneseDateTypeHandler" /> </typeHandlers>
|
위 처럼 타입 핸들러를 셋팅 하면 된다고 하는데 안된다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| import java.sql.CallableStatement; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Timestamp; import java.time.LocalDateTime; import org.apache.ibatis.type.BaseTypeHandler; import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.MappedTypes;
@MappedTypes(LocalDateTime.class) public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime> {
@Override public void setNonNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JdbcType jdbcType) throws SQLException { ps.setTimestamp(i, Timestamp.valueOf(parameter)); }
@Override public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException { Timestamp timestamp = rs.getTimestamp(columnName); return getLocalDateTime(timestamp); }
@Override public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException { Timestamp timestamp = rs.getTimestamp(columnIndex); return getLocalDateTime(timestamp); }
@Override public LocalDateTime getNullableResult(CallableStatement cs, int columnIndex) throws SQLException { Timestamp timestamp = cs.getTimestamp(columnIndex); return getLocalDateTime(timestamp); }
private static LocalDateTime getLocalDateTime(Timestamp timestamp) { if (timestamp != null) { return timestamp.toLocalDateTime(); } return null; } }
|
위 처럼 @MappedTypes(LocalDateTime.class)
선언을 해주니 정상적으로 잘된다.
참조