实体是Date类型,数据库是Long类型,
通过转换,保存Date转成Long存入,查询从Long转成Date
可以是任意类型的转换
import com.baomidou.mybatisplus.annotation.*;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import java.util.Date;
import com.handler.DateLongTypeHandler;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* <p>
* 运行计划
* </p>
* @since 2023-11-08
*/
@Data
@Accessors(chain = true)
@TableName("hss_plan")
public class HssPlanEntity extends Model<HssPlanEntity> implements Serializable {
@TableId(value = "id", type = IdType.AUTO)
private Long id;
/**
* hss_type.id
*/
private Long typeId;
/**
* hss_equipment.id
*/
private Long equipmentId;
/**
* 生效时间:0表示没有生效时间限制
*/
@TableField(typeHandler = DateLongTypeHandler.class)
private Date startTime;
}
转换handler类
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ArrayUtil;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.tecloman.cloud.common.redis.model.RedisUserModel;
import com.tecloman.cloud.common.server.utils.DateUtils;
import org.apache.ibatis.type.*;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import java.util.TimeZone;
import static org.apache.logging.log4j.message.MapMessage.MapFormat.JSON;
@Component
@MappedTypes({Date.class})
@MappedJdbcTypes({JdbcType.BIGINT})
public class DateLongTypeHandler extends BaseTypeHandler<Date> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Date parameter, JdbcType jdbcType) throws SQLException {
if (parameter == null) {
if (jdbcType == null) {
throw new TypeException(
"JDBC requires that the JdbcType must be specified for all nullable parameters.");
}
try {
ps.setNull(i, jdbcType.TYPE_CODE);
} catch (SQLException e) {
throw new TypeException(
"Error setting null for parameter #"
+ i
+ " with JdbcType "
+ jdbcType
+ " . "
+ "Try setting a different JdbcType for this parameter or a different jdbcTypeForNull configuration property. "
+ "Cause: " + e, e);
}
} else {
ps.setLong(i, parameter.getTime() / 1000);
}
}
// 需要转成用户时区的实体字段
private final String[] userTimezoneColumn = {"create_time"};
@Override
public Date getNullableResult(ResultSet rs, String columnName) throws SQLException {
long res = rs.getLong(columnName);
if (res == 0) {
return null;
}
Date d;
try {
d = new Date(res * 1000);
} catch (Exception ex) {
d = new Date(res);
}
return d;
}
@Override
public Date getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
long res = rs.getLong(columnIndex);
if (res == 0) {
return null;
}
Date d;
try {
d = new Date(res * 1000);
} catch (Exception ex) {
d = new Date(res);
}
return d;
}
@Override
public Date getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
long res = cs.getLong(columnIndex);
if (res == 0) {
return null;
}
Date d;
try {
d = new Date(res * 1000);
} catch (Exception ex) {
d = new Date(res);
}
return d;
}
}