@Accessors(fluent=true)
public class Parent {
private int parentProperty;
}
@Data
public class Child extends Parent {
private int childProperty;
}
Child child = new Child();
child.setParentProperty(10);
child.setChildProperty(20);
System.out.println(child); // prints Child{parentProperty=10, childProperty=20}
@Data
public class Parent {
private String name;
}
@Data
@Accessors(fluent=true)
public class Child extends Parent {
private String age;
}
Child child = new Child();
child.setName("John").setAge("18");
Parent parent = child.toParent();
@Data
public class Parent {
private String name;
}
@Data
@Accessors(prefix = "p")
public class Child extends Parent {
private String age;
}
Child child = new Child();
child.setName("John").setAge("18");
Parent parent = child.toParent();
在MyBatis Plus中,可以使用@FillInsert和@FillUpdate注解来自动填充公共参数。 @FillInsert:用于填充insert操作的公共参数,默认情况下,会将注解所在的字段自动添加到插入语句中。 @FillUpdate:用于填充update操作的公共参数,默认情况下,会将注解所在的字段自动添加到更新语句中。 示例代码如下:
public class BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 搜索值
*/
@JsonIgnore
@TableField(exist = false)
private String searchValue;
/**
* 创建者
*/
@TableField(fill = FieldFill.INSERT)
private String createBy;
/**
* 创建时间
*/
@TableField(fill = FieldFill.INSERT)
private Date createTime;
/**
* 更新者
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private String updateBy;
/**
* 更新时间
*/
@TableField(fill = FieldFill.INSERT_UPDATE)
private Date updateTime;
/**
* 请求参数
*/
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@TableField(exist = false)
private Map<String, Object> params = new HashMap<>();
}
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import java.util.Date;
/**
* MP注入处理器
*
* @Author: water
* @Date: 2023/11/15 8:51
* @Version: 1.0.0
*/
@Slf4j
public class CreateAndUpdateMetaObjectHandler implements MetaObjectHandler {
@Override
public void insertFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
Date createTime = ObjectUtil.isNotNull(baseEntity.getCreateTime())
? baseEntity.getCreateTime() : new Date();
Date updateTime = ObjectUtil.isNotNull(baseEntity.getUpdateTime())
? baseEntity.getUpdateTime() : new Date();
// 解析了有数据填充的问题
baseEntity.setCreateTime(createTime);
baseEntity.setUpdateTime(updateTime);
String username = StringUtils.isNotBlank(baseEntity.getCreateBy())
? baseEntity.getCreateBy() : getLoginUsername();
// 当前已登录 且 创建人为空 则填充
baseEntity.setCreateBy(username);
// 当前已登录 且 更新人为空 则填充
baseEntity.setUpdateBy(username);
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
@Override
public void updateFill(MetaObject metaObject) {
try {
if (ObjectUtil.isNotNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
BaseEntity baseEntity = (BaseEntity) metaObject.getOriginalObject();
Date current = new Date();
// 更新时间填充(不管为不为空)
baseEntity.setUpdateTime(current);
String username = getLoginUsername();
// 当前已登录 更新人填充(不管为不为空)
if (StringUtils.isNotBlank(username)) {
baseEntity.setUpdateBy(username);
}
}
} catch (Exception e) {
throw new ServiceException("自动注入异常 => " + e.getMessage(), HttpStatus.HTTP_UNAUTHORIZED);
}
}
/**
* 获取登录用户名
*/
private String getLoginUsername() {
LoginUser loginUser;
try {
loginUser = LoginHelper.getLoginUser();
} catch (Exception e) {
log.warn("自动注入警告 => 用户未登录");
return null;
}
return ObjectUtil.isNotNull(loginUser) ? loginUser.getUsername() : null;
}
}