Bean copy and tool class encapsulation in SpringBoot project

In the development of our SpringBoot project, we often need to copy and transform objects. For example, we need to convert the DTO objects passed to us by the front end into POJO objects and store them in the database. When returning front-end data, we need to convert POJO objects into VO. This article The article talks about the copying of objects in SpringBoot and the encapsulation of its tool classes.

1. UseSpring BeanUtils

There is a tool class for us in the or Springproject .SpringBootBeanUtils

The method of use is as follows, the first parameter is the source object, and the second parameter is the target object:

BeanUtils.copyProperties(Object source, Object target);
复制代码

You can also continue to pass in parameters to ignore parameter copying:

BeanUtils.copyProperties(Object source, Object target, String... ignoreProperties);
复制代码

Example usage:

Here we create a UserDto, User实体类, to realize Dto对象the conversion of the front-end incoming实体类对象

domain.entity.User

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private Long id;
    private String username;
    private Integer age;
    private Date createTime;
    private Date updateTime;
    private Integer deleted;
}
复制代码

domain.dto.AddUserDto

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class AddUserDto {
    private String username;
    private Integer age;
}
复制代码

Implement Bean copy:

public ResponseResult addUser(AddUserDto addUserDto) {
    // 先新建一个user对象
    User user = new User();
    // 实现把addUserDto拷贝到user对象
    BeanUtils.copyProperties(addUserDto, user);

    userMapper.insert(user);
    return ResponseResult.okResult();
}
复制代码

2. Bean copy tool class encapsulation

Because the basic BeanUtilscopy is very inconvenient when using it, we need to create a new Usercopy by ourselves, Listand we need to traverse the copy of the collection by ourselves. Here we encapsulate the tool class to realize these functions

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

public class BeanCopyUtils {
    private BeanCopyUtils() {
    }

    public static <V> V copyBean(Object source, Class<V> clazz) {
        // 创建目标对象
        V result = null;
        try {
            result = clazz.newInstance();
            // 实现属性拷贝
            BeanUtils.copyProperties(source, result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //返回
        return result;
    }

    public static <O, V> List<V> copyBeanList(List<O> list, Class<V> clazz) {
        return list.stream()
                .map(o -> copyBean(o, clazz))
                .collect(Collectors.toList());
    }
}
复制代码

Example of use

  1. common Beanobject copy
public ResponseResult addUser(AddUserDto addUserDto) {
    User user = BeanCopyUtils.copyBean(addUserDto, User.class);

    userMapper.insert(user);
    return ResponseResult.okResult();
}
复制代码
  1. Listcopy
public ResponseResult getUser(Integer pageNum, Integer pageSize) {
    Page<User> userPage = new Page<>(pageNum, pageSize);
    userMapper.selectPage(userPage, null);

    List<UserVo> userVos = BeanCopyUtils.copyBeanList(userPage.getRecords(), UserVo.class);
    PageVo pageVo = new PageVo(userVos, userPage.getTotal());
    return ResponseResult.okResult(pageVo);
}
复制代码

The bean copy in the project generally uses our tool classes directly, and there are also some open source tool classes, hutool, etc., and the principle is the same.

Guess you like

Origin blog.csdn.net/ww2651071028/article/details/130593456