BeanUtils.copyProperties实现po,vo,dto之间的转换

缘由:项目中各类bean对象转换,在老项目里看到一堆的从A类get出来,再set到B类...简直看的头大

BeanUtils.copyProperties的作用

把A类中与B类中相同的变量名称的值复制到B类。

举个例子,将接收到的userDto转换成po

    @PostMapping("/user")
    public AjaxResult addUser(@RequestBody UserDto userDto) {
        UserEntity user = new UserEntity();
        BeanUtils.copyProperties(userDto, user);
        String salt = UUID.randomUUID().toString().replace("-", "");
        user.setSalt(salt);
        return AjaxResult.success(userService.save(user), Constant.Rest.INSERT_SUCCESS);
    }

猜你喜欢

转载自www.cnblogs.com/jarjune/p/9229236.html
今日推荐