camel-test3-17/camel-mybatis-yml/StringToTimestampTypeHandler.java

42 lines
1.5 KiB
Java

package com.example.processor;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.ibatis.type.TypeHandler;
import org.apache.ibatis.type.JdbcType;
public class StringToTimestampTypeHandler implements TypeHandler<String> {
private static final SimpleDateFormat FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
@Override
public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException {
try {
ps.setLong(i, FORMAT.parse(parameter).getTime());
} catch (ParseException e) {
throw new SQLException("日期格式解析错误", e);
}
}
@Override
public String getResult(ResultSet rs, String columnName) throws SQLException {
long timestamp = rs.getLong(columnName);
return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp));
}
@Override
public String getResult(ResultSet rs, int columnIndex) throws SQLException {
long timestamp = rs.getLong(columnIndex);
return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp));
}
@Override
public String getResult(CallableStatement cs, int columnIndex) throws SQLException {
long timestamp = cs.getLong(columnIndex);
return timestamp == 0 ? null : FORMAT.format(new java.util.Date(timestamp));
}
}