mysql插入和返回接口集日期转换
package com.grt.v3.lindorm.utils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
public class DateTypeHandler extends BaseTypeHandler<Date> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType)
throws SQLException {
ps.setLong(i, parameter.getTime());
}
@Override
public Date getNullableResult(ResultSet rs, String columnName)
throws SQLException {
//lindorm没实现这个方法?
// Object sqlTimestamp = rs.getObject(columnName,Long.class);
Object sqlTimestamp = rs.getObject(columnName);
if (sqlTimestamp != null) {
return new Date((Long) sqlTimestamp);
}
return null;
}
@Override
public Date getNullableResult(ResultSet rs, int columnIndex)
throws SQLException {
Object sqlTimestamp = rs.getObject(columnIndex);
if (sqlTimestamp != null) {
return new Date((Long) sqlTimestamp);
}
return null;
}
@Override
public Date getNullableResult(CallableStatement cs, int columnIndex)
throws SQLException {
Object sqlTimestamp = cs.getObject(columnIndex);
if (sqlTimestamp != null) {
return new Date((Long) sqlTimestamp);
}
return null;
}
}
在实体中引入
@TableField(value = "time",typeHandler = DateTypeHandler.class)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Date time;