18 changed files with 176 additions and 47 deletions
@ -0,0 +1,85 @@ |
|||||
|
package com.bnyer.common.core.dto; |
||||
|
|
||||
|
import cn.hutool.core.util.IdUtil; |
||||
|
import com.bnyer.common.core.domain.BaseDomain; |
||||
|
import com.bnyer.common.core.utils.StringUtils; |
||||
|
import com.bnyer.common.core.utils.bean.ReflectUtils; |
||||
|
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
|
import lombok.Data; |
||||
|
import org.springframework.beans.BeanUtils; |
||||
|
|
||||
|
import java.io.Serializable; |
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
|
||||
|
/** |
||||
|
* @author :WXC |
||||
|
* @Date :2023/05/10 |
||||
|
* @description : |
||||
|
*/ |
||||
|
@Data |
||||
|
public class BaseDto <T extends BaseDomain> implements Serializable { |
||||
|
private static final long serialVersionUID = 320272502336434381L; |
||||
|
|
||||
|
/** |
||||
|
* 主键ID |
||||
|
*/ |
||||
|
private Long id; |
||||
|
|
||||
|
/** |
||||
|
* 数据创建时间 |
||||
|
*/ |
||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
|
private Date createTime; |
||||
|
|
||||
|
/** |
||||
|
* 数据更新时间 |
||||
|
*/ |
||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
|
private Date updateTime; |
||||
|
|
||||
|
/** |
||||
|
* 是否显示 |
||||
|
*/ |
||||
|
private String isShow; |
||||
|
|
||||
|
/** |
||||
|
* 排序 |
||||
|
*/ |
||||
|
private Integer sort; |
||||
|
|
||||
|
/** |
||||
|
* 主键ID集合 |
||||
|
*/ |
||||
|
private List<Long> ids; |
||||
|
|
||||
|
/** |
||||
|
* 转为实体类 |
||||
|
* |
||||
|
* @return 实体类 |
||||
|
*/ |
||||
|
public T toEntity() { |
||||
|
Class<T> entity = ReflectUtils.getParameterizedType(this.getClass()); |
||||
|
T entityInstance = ReflectUtils.newInstance(entity); |
||||
|
BeanUtils.copyProperties(this, entityInstance); |
||||
|
if (this.id == null) { |
||||
|
entityInstance.setId(IdUtil.getSnowflakeNextId()); |
||||
|
entityInstance.setIsShow("1"); |
||||
|
entityInstance.setCreateTime(new Date()); |
||||
|
entityInstance.setUpdateTime(null); |
||||
|
if (StringUtils.isNotBlank(this.isShow)){ |
||||
|
entityInstance.setIsShow(this.isShow); |
||||
|
}else { |
||||
|
entityInstance.setIsShow("1"); |
||||
|
} |
||||
|
if (this.sort != null){ |
||||
|
entityInstance.setSort(this.sort); |
||||
|
}else { |
||||
|
entityInstance.setSort(0); |
||||
|
} |
||||
|
} else { |
||||
|
entityInstance.setUpdateTime(new Date()); |
||||
|
} |
||||
|
return entityInstance; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,45 @@ |
|||||
|
package com.bnyer.common.core.utils.bean; |
||||
|
|
||||
|
import org.springframework.beans.BeanInstantiationException; |
||||
|
|
||||
|
import java.lang.reflect.ParameterizedType; |
||||
|
import java.lang.reflect.Type; |
||||
|
|
||||
|
/** |
||||
|
* @author :WXC |
||||
|
* @Date :2023/05/10 |
||||
|
* @description :反射工具 |
||||
|
*/ |
||||
|
public class ReflectUtils { |
||||
|
|
||||
|
/** |
||||
|
* 得到传入类中当前指定得泛型 |
||||
|
* |
||||
|
* @param cls 待解析得类 |
||||
|
* @return 显示指定的泛型 |
||||
|
*/ |
||||
|
@SuppressWarnings("unchecked") |
||||
|
public static <T> Class<T> getParameterizedType(Class cls) { |
||||
|
Type genType = cls.getGenericSuperclass(); |
||||
|
Type[] params = ((ParameterizedType) genType).getActualTypeArguments(); |
||||
|
return (Class<T>) params[0]; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 实例化class 对象 |
||||
|
* |
||||
|
* @param cls 待实例化类 |
||||
|
* @param <T> 类类型 |
||||
|
* @return 实例化对象 |
||||
|
*/ |
||||
|
public static <T> T newInstance(Class<T> cls) { |
||||
|
try { |
||||
|
return cls.newInstance(); |
||||
|
} catch (Exception e) { |
||||
|
throw new BeanInstantiationException(cls, e.getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue