spring-boot-realworld-examp.../src/main/java/io/spring/infrastructure/mybatis/DateTimeHandler.java
2017-08-15 10:47:18 +08:00

45 lines
1.6 KiB
Java

package io.spring.infrastructure.mybatis;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;
import org.joda.time.DateTime;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.TimeZone;
@MappedTypes(DateTime.class)
public class DateTimeHandler implements TypeHandler<DateTime> {
private static final Calendar UTC_CALENDAR = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
@Override
public void setParameter(PreparedStatement ps, int i, DateTime parameter, JdbcType jdbcType) throws SQLException {
ps.setTimestamp(i, parameter != null ? new Timestamp(parameter.getMillis()) : null, UTC_CALENDAR);
}
@Override
public DateTime getResult(ResultSet rs, String columnName) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnName, UTC_CALENDAR);
return timestamp != null ? new DateTime(timestamp.getTime()) : null;
}
@Override
public DateTime getResult(ResultSet rs, int columnIndex) throws SQLException {
Timestamp timestamp = rs.getTimestamp(columnIndex, UTC_CALENDAR);
return timestamp != null ? new DateTime(timestamp.getTime()) : null;
}
@Override
public DateTime getResult(CallableStatement cs, int columnIndex) throws SQLException {
Timestamp ts = cs.getTimestamp(columnIndex, UTC_CALENDAR);
return ts != null ? new DateTime(ts.getTime()) : null;
}
}