• 7月14 Enity对象的抽取


    新增、修改对象时对公共属性进行操作

    操作枚举

    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 比较好

  • 相关阅读:
    【CF932F】Escape Through Leaf 启发式合并set维护凸包
    【CF932E】Team Work/【BZOJ5093】图的价值 数学+NTT
    【CF917D】Stranger Trees 树形DP+Prufer序列
    【CF914G】Sum the Fibonacci 快速??变换模板
    【CF772D】Varying Kibibits FWT
    【CF802C】Heidi and Library (hard) 费用流
    【CF802L】Send the Fool Further! (hard) 高斯消元
    【CF809D】Hitchhiking in the Baltic States Splay
    【CF815D】Karen and Cards 单调栈+扫描线
    【CF819D】Mister B and Astronomers EXGCD
  • 原文地址:https://www.cnblogs.com/lyon91/p/9305862.html
Copyright © 2020-2023  润新知