标签:mod png 比较 integer 父类 entity creat out util
新增、修改对象时对公共属性进行操作
操作枚举
public enum OperatotType { /** * Create */ Create(1), /** * Update */ Update(2), /** * Delete */ Delete(3),; Integer value; OperatotType(Integer value) { this.value = value; } public Integer getValue() { return value; } }
常量枚举
public enum EntityEnum { /** 正常 */ Normal(1), /** 删除 */ Deleted(0); Integer value;
公共的父类
public class DTOIsDeleted { private String createUser; private Integer isDelete; private Date createTime;
工具类
/* * 新增,修改和删除操作时,给公共字段赋值 */ public static void setDTO(DTOIsDeleted dto, OperatotType operatotType, String userId) { Date currentTime = new Date(); if (operatotType == OperatotType.Create) { if (EmptyUtils.isNotEmpty(userId)) { dto.setCreateUser(userId); } dto.setCreateTime(currentTime); dto.setIsDelete(EntityEnum.Normal.getValue()); } else if (operatotType == OperatotType.Update) { /* * 修改 * if (EmptyUtils.isNotEmpty(userId)) { dto.setModifyUser(userId); } dto.setModifyTime(currentTime);*/ } else if (operatotType == OperatotType.Delete) { /* * 修改用户没添加 * if (EmptyUtils.isNotEmpty(userId)) { dto.setModifyUser(userId); }*/ //dto.setModifyTime(currentTime); dto.setCreateTime(currentTime); dto.setIsDelete(EntityEnum.Deleted.getValue()); } }
二、测试
继承公共父类
public class Car extends DTOIsDeleted { private Integer id; private String name; private String createUser; private Integer isDelete; private Date createTime;
@Controller public class OperatoController { @Autowired private OperatoService operatoService; /** 测试操作 */ @GetMapping("/operato/add") @ResponseBody public R add() { operatoService.add(); return R.ok(); } /** 测试操作 */ @GetMapping("/operato/delete/{id}") @ResponseBody public R delete(@PathVariable("id") int id) { operatoService.delete(id); return R.ok(); }
service
@Service public class OperatoService { @Autowired CarMapper carMapper; public void add() { Car car = new Car(); car.setName("梅赛德斯"+RandomUtils.nextInt()); CommonUtils.setDTO(car, OperatotType.Create, "123"); carMapper.insert(car); } public void delete(int id) { Car car = carMapper.selectByPrimaryKey(id); if(car != null){ CommonUtils.setDTO(car, OperatotType.Delete, "123"); carMapper.updateByPrimaryKeySelective(car); }else{ System.out.println("没有查询到car"); } }
结果 字段名应该叫deleted 比较好
标签:mod png 比较 integer 父类 entity creat out util
原文地址:https://www.cnblogs.com/lyon91/p/9305862.html