集成mybatis-plus这个就不多说了,本文重点描述自动填充字段
第一步,确保你的实体类字段有注解,本文主要用到了三种,ID、新增时插入、修改时插入
@TableId(value = "id", type = IdType.ASSIGN_ID) @TableField(fill = FieldFill.INSERT) @TableField(fill = FieldFill.UPDATE)
直接上代码,对原有的Entity做了一些自己的改造
package com.sbing.guitar.common.core.web.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
/**
* Entity基类
*
* @author ruoyi
*/
@Data
public class BaseEntity implements Serializable
{
private static final long serialVersionUID = 1L;
/** id */
@TableId(value = "id", type = IdType.ASSIGN_ID)
/** 解决ASSIGN_ID超过19位精度失效问题 */
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
@TableField(exist = false)
/** 搜索值 */
@JsonIgnore
private String searchValue;
@TableField(fill = FieldFill.INSERT)
/** 创建者 */
private String createBy;
/** 创建时间 */
@TableField(fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date createTime;
@TableField(fill = FieldFill.UPDATE)
/** 更新者 */
private String updateBy;
/** 删除时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date deleteTime;
@TableField(fill = FieldFill.INSERT)
/** 删除标识 */
private String deleteFlag;
@TableField(fill = FieldFill.UPDATE)
/** 更新时间 */
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date updateTime;
@TableField(exist = false)
/** 备注 */
private String remark;
@TableField(exist = false)
/** 请求参数 */
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Map<String, Object> params;
public Map<String, Object> getParams()
{
if (params == null)
{
params = new HashMap<>();
}
return params;
}
public void setParams(Map<String, Object> params)
{
this.params = params;
}
}
第二步,实现 MetaObjectHandler,编写自己的插入逻辑
这里要说明的是,ruoyi项目可以通过以下方式获得用户名、用户Id和用户key等信息,直接上代码
@Component
public class MyMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
this.strictInsertFill(metaObject, "createBy", String.class, getUserName());
this.strictInsertFill(metaObject, "createTime", Date.class, new Date()); // 创建时间
this.strictInsertFill(metaObject,"deleteFlag", String.class,"0");
}
@Override
public void updateFill(MetaObject metaObject) {
this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
this.strictUpdateFill(metaObject, "updateBy", String.class, getUserName());
}
// 这里获取用户名,也可以获取用户Id等,详情见SecurityConstants类
private String getUserName(){
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
return ServletUtils.getHeader(request, SecurityConstants.DETAILS_USERNAME);
}
}