1、@TableField注解
实体的常用注解实例:
//get,set方法自动生成
@Data
//生成equals()和hashCode()方法,false表示不考虑父类的属性
@EqualsAndHashCode(callSuper = false)
//使用可以连续set
@Accessors(chain = true)
//对应数据库表名
@TableName("CO_CFM_COM_FILE")
public class CoCfmComFile {
//自动生成一个基于 UUID 的唯一标识作为主键值
@TableId(type= IdType.ASSIGN_UUID)
private String fileId;
@TableField("F_GUID")
private String fGuid;
@TableField("FILE_NAME")
private String fileName;
//创建的时候自动插入数据
@TableField(fill = FieldFill.INSERT)
private Timestamp fCrdate;
//@JsonFormat(shape = JsonFormat.Shape.ANY,
// locale = "zh_CN",pattern = "yyyy-MM-dd HH:mm:ss",
// timezone = "GMT")
@JSONField(format = "yyyy-MM-dd HH:mm:ss")
private Timestamp applyEndTime;
//创建和更新的时候自动更新数据
@TableField(fill = FieldFill.INSERT_UPDATE)
private Timestamp fChdate;
@TableField(value = "USER_NAME", fill = FieldFill.INSERT)
private String userName;
//表中无这个字段时使用
@TableField(exist = false)
private List<CoCfmFileEmpower> empowers;
}
字段填充策略 FieldFill
一般在项目的config包下新建自动填充处理类使其实现接口MetaObjectHandler,自动赋值配置类,并重写其方法:
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.itlamp.common.UserInfo;
import org.itlamp.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
/**
* @author Administrator 实体属性自动填充
*/
@Log4j2
@Component
public class MybatisPlusMetaObjectHandler implements MetaObjectHandler {
@Autowired
HttpServletRequest request;
@Value("${mybatis-plus.global-config.db-config.logic-delete-field:}")
private String delField;
@Value("${mybatis-plus.global-config.db-config.logic-not-delete-value:}")
private String delFieldDefVal;
@Override
public void insertFill(MetaObject metaObject) {
UserInfo userInfo = null;
try {
userInfo = (UserInfo) request.getSession().getAttribute(com.pansoft.config.SystemConstant.SESSION_USERINFO_KEY);
} catch (Exception e) {
log.error(e.getMessage());
}
if (userInfo != null) {
this.strictInsertFill(metaObject, "userId", String.class, userInfo.getYhid());
this.strictInsertFill(metaObject, "userName", String.class, userInfo.getYhxm());
this.strictInsertFill(metaObject, "unitId", String.class, userInfo.getDwbm());
this.strictInsertFill(metaObject, "unitPath", String.class, userInfo.getDwid());
this.strictInsertFill(metaObject, "unitName", String.class, userInfo.getDwmc());
}
this.strictInsertFill(metaObject, "createTime", Timestamp.class, DateUtils.getCurrentTimestamp());
this.strictInsertFill(metaObject, "firstTime", Timestamp.class, DateUtils.getCurrentTimestamp());
this.strictInsertFill(metaObject, "fCrdate", Timestamp.class, DateUtils.getCurrentTimestamp());
if (StringUtils.isNotBlank(delField) && metaObject.hasGetter(delField)) { // 填充逻辑删除字段
Class fieldClass = metaObject.getGetterType(delField);
if (Integer.class.equals(fieldClass)) {
this.setFieldValByName(delField, Integer.valueOf(delFieldDefVal), metaObject);
} else if (Long.class.equals(fieldClass)) {
this.setFieldValByName(delField, Long.valueOf(delFieldDefVal), metaObject);
} else {
this.setFieldValByName(delField, delFieldDefVal, metaObject);
}
}
}
@Override
public void updateFill(MetaObject metaObject) {
UserInfo userInfo = null;
try {
userInfo = (UserInfo) request.getSession().getAttribute(com.pansoft.config.SystemConstant.SESSION_USERINFO_KEY);
} catch (Exception e) {
log.error(e.getMessage());
}
if (userInfo != null) {
this.strictUpdateFill(metaObject, "update_user_id", String.class, userInfo.getYhid());
this.strictUpdateFill(metaObject, "update_user_name", String.class, userInfo.getYhxm());
this.strictUpdateFill(metaObject, "update_org_code", String.class, userInfo.getDwbm());
this.strictUpdateFill(metaObject, "update_org_name", String.class, userInfo.getDwmc());
}
this.strictUpdateFill(metaObject, "updateTime", Timestamp.class, DateUtils.getCurrentTimestamp());
this.strictInsertFill(metaObject, "lastTime", Timestamp.class, DateUtils.getCurrentTimestamp());
this.strictUpdateFill(metaObject, "fChdate", Timestamp.class, DateUtils.getCurrentTimestamp());
}
}
2、LocalDateTime格式化日期和@JsonFormat注解用法
(1)LocalDateTime
LocalDate:只含年月日的日期对象,只有精度大于或等于日的加减,如年、月、日;
LocalTime:只含时分秒的时间对象,只有精度小于或等于时的加减,如时、分、秒、纳秒;
LocalDateTime:同时含年月日时分秒的日期对象,可以进行任意精度的时间相加减;
(2)@JsonFormat:
shap: 表示序列化后的一种类型,默认为JsonFormat.Shape.ANY
pattern: 表示日期的格式,默认为序列化以后的格式,比如:2020-09-08T15:19:09.000+00:00
timezone: 默认是GMT,中国需要GMT+8
locale: 根据位置序列化的一种格式,默认为本地的语言简称,中国的语言简称为zh_CN
因为jackson在序列化时间时是按照国际标准时间GMT进行格式化的,而在国内默认时区使用的是CST时区,两者相差8小时,因为我们是东八区(北京时间),所以我们在格式化的时候要指定时区