cn.hutool
hutool-all
5.8.0
package com.lezu.springboot.common.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
import java.util.List;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Accessors(chain = true)
public class UserDto {
private String name;
private Integer qty;
private BigDecimal money;
private List role;
}
package com.lezu.springboot.test.lian;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.lezu.springboot.common.dto.UserDto;
import java.util.HashMap;
import java.util.Map;
public class Test2 {
public static void main(String[] args) {
UserDto userDto = new UserDto();
Map map = new HashMap<>();
map.put("name", "张三");
map.put("qty", "111");
map.put("money", "10000");
BeanUtil.copyProperties(map, userDto);
System.out.println(userDto);
System.out.println(JSON.toJSONString(userDto));
}
Map里面放List
package com.lezu.springboot.test.lian;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.lezu.springboot.common.dto.UserDto;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Test2 {
public static void main(String[] args) {
UserDto userDto = new UserDto();
Map map = new HashMap<>();
List role = new ArrayList<>();
role.add("100");
role.add("200");
role.add("300");
map.put("role", role);
BeanUtil.copyProperties(map, userDto);
System.out.println(userDto);
System.out.println(JSON.toJSONString(userDto));
}
}
package com.lezu.springboot.common.dto.in;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.math.BigDecimal;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Accessors(chain = true)
public class InUserDto {
private String name;
private Integer qty;
private BigDecimal money;
}
package com.lezu.springboot.test.lian;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.lezu.springboot.common.dto.UserDto;
import com.lezu.springboot.common.dto.in.InUserDto;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add(new InUserDto("张三", 111, new BigDecimal("10000")));
list.add(new InUserDto("李四", 222, new BigDecimal("10000")));
list.add(new InUserDto("小王", 333, new BigDecimal("10000")));
List userDtoList = BeanUtil.copyToList(list, UserDto.class);
System.out.println(userDtoList);
System.out.println(JSON.toJSONString(userDtoList));
}
}
复制Map
Map中放List进行复制
复制List
package com.lezu.springboot.test.lian;
import com.alibaba.fastjson.JSON;
import com.lezu.springboot.common.dto.UserDto;
import com.lezu.springboot.common.dto.in.InUserDto;
import org.springframework.beans.BeanUtils;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
public class Test2 {
public static void main(String[] args) {
List userDtoList = new ArrayList<>();
List list = new ArrayList<>();
list.add(new InUserDto("张三", 111, new BigDecimal("10000")));
list.add(new InUserDto("李四", 222, new BigDecimal("10000")));
list.add(new InUserDto("小王", 333, new BigDecimal("10000")));
BeanUtils.copyProperties(list, userDtoList);
System.out.println(userDtoList);
System.out.println(JSON.toJSONString(userDtoList));
}
}
很显然使用org.springframework.beans.BeanUtils复制Map和List是不行的
PS: