首先:答应我不要去自己重复造轮子,写Beanutil了,为什么不去使用现成的工具类呢?现成的工具类简单又安全,自己写的万一哪天出了问题呢。 这里的beanUtil 我首推hutool,它的工具类的真的太丰富了,真的感觉它太懂中国的程序员了。 Result<>(null, "error", null); System.out.println(successResult + " " + errorResult); BeanUtil.copyProperties null时,可选择忽略而不注入此值,否则注入null 目标对象有值不覆盖:原对象为null,则覆盖 是不是感觉原来的方法行不通了,是的,原来方法太单一了,但是我们调用api的时候稍微注意一下就会发现: beanUtil .setOverride(true); public static Object merge(Object sourceBean, Object targetBean) { BeanUtil.copyProperties
import com.yung.ppapi.integration.dto.YCPFExpressDTO; /** * * @author lisheng4 * */ public class BeanUtil
import net.sf.cglib.beans.BeanMap; import org.springframework.cglib.beans.BeanCopier; import org.springframework.objenesis.ObjenesisStd;
所以,就看B的对象是如何创建的,发现B的对象是A批对象通过Spring的BeanUtil的copyProperties方法进行赋值的,虽然,B是new出来后,然后进行拷贝,而不是直接使用A批对象的引用。
在Java编程中,BeanUtil工具类是一种强大且便捷的工具,用于简化对象之间的属性复制和操作。本文将介绍BeanUtil的基本功能,通过详细的代码示例展示其应用,并与其他类似工具进行对比。 本文还将探讨BeanUtil在实际开发中的优势和使用场景,帮助开发者更好地理解和应用这一工具类。一、BeanUtil 工具类介绍1. BeanUtil 简述BeanUtil是一个广泛使用的Java工具类,提供了一系列方法来简化JavaBean对象之间的属性复制和操作。 二、BeanUtil 工具类使用示例1. 属性复制属性复制是BeanUtil最常用的功能之一,可以将一个对象的所有属性值复制到另一个对象中。 三、BeanUtil 工具类总结BeanUtil工具类为Java开发者提供了一种简便的方法来操作JavaBean对象的属性。
虽然Copy的工具有很多,如ModelMapper、Dozer、Orika,都可以实现自动映射,但是小编最推荐的还是最原始的set、get方式。于是就面临一个问题:字段太多了怎么办?
Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanUtil.copyProperties Override public UserDTO sourceToTarget(UserVO var) { UserDTO userDTO = new UserDTO(); BeanUtil.copyProperties
把两个属性合成到一个上面,详情请参考 《比BeanCopy好用的BeanMerge(java合并对象属性,把非空属性合成到目标对象上)》 解决方案 hutool开源库为我们提供了更为强大的Bean工具-BeanUtil BeanUtil.copyProperties(oldDetail.get(),userDetail,true, CopyOptions.create().setIgnoreNullValue(true return ApiReturnUtil.error("userId不能为空"); }else{ //复制一个新的用于保存 UserDetail newDetail=new UserDetail(); BeanUtil.copyProperties oldDetail.isPresent()){ System.out.println("copying"); //复制旧的属性过来,忽略null属性,忽略null值,有值的以新的为主,null的则以旧为主 BeanUtil.copyProperties 使用方法如下: BeanUtil.copyProperties(oldObject,newObject,true, CopyOptions.create().setXXXX(true)); editable
1.234"; //判断是否为数字、整数、浮点数 NumberUtil.isNumber(n3); NumberUtil.isInteger(n3); NumberUtil.isDouble(n3); BeanUtil brand.setId(1L); brand.setName("小米"); brand.setShowStatus(0); //Bean转Map Map<String, Object> map = BeanUtil.beanToMap (brand); LOGGER.info("beanUtil bean to map:{}", map); //Map转Bean PmsBrand mapBrand = BeanUtil.mapToBean (map, PmsBrand.class, false); LOGGER.info("beanUtil map to bean:{}", mapBrand); //Bean属性拷贝 PmsBrand copyBrand = new PmsBrand(); BeanUtil.copyProperties(brand, copyBrand); LOGGER.info("beanUtil copy properties:{
public boolean updateById(Application application) { Application app = getById(application.getId()); BeanUtil.copyIgnoreNull cn.hutool.core.bean.copier.CopyOptions; import cn.hutool.core.lang.func.Func1; /** Util class for bean */ public class BeanUtil copyIgnoreNull( Object source, Object target, Func1<P, R>... ignoreProperties) { cn.hutool.core.bean.BeanUtil.copyProperties
1.234"; //判断是否为数字、整数、浮点数 NumberUtil.isNumber(n3); NumberUtil.isInteger(n3); NumberUtil.isDouble(n3); BeanUtil brand.setId(1L); brand.setName("小米"); brand.setShowStatus(0); //Bean转Map Map<String, Object> map = BeanUtil.beanToMap (brand); LOGGER.info("beanUtil bean to map:{}", map); //Map转Bean PmsBrand mapBrand = BeanUtil.mapToBean (map, PmsBrand.class, false); LOGGER.info("beanUtil map to bean:{}", mapBrand); //Bean属性拷贝 PmsBrand copyBrand = new PmsBrand(); BeanUtil.copyProperties(brand, copyBrand); LOGGER.info("beanUtil copy properties:{
1.234"; //判断是否为数字、整数、浮点数 NumberUtil.isNumber(n3); NumberUtil.isInteger(n3); NumberUtil.isDouble(n3); BeanUtil brand.setId(1L); brand.setName("小米"); brand.setShowStatus(0); //Bean转Map Map<String, Object> map = BeanUtil.beanToMap (brand); LOGGER.info("beanUtil bean to map:{}", map); //Map转Bean PmsBrand mapBrand = BeanUtil.mapToBean (map, PmsBrand.class, false); LOGGER.info("beanUtil map to bean:{}", mapBrand); //Bean属性拷贝 PmsBrand copyBrand = new PmsBrand(); BeanUtil.copyProperties(brand, copyBrand); LOGGER.info("beanUtil copy properties:{
BeanUtils.copyProperties(source, target); } catch (BeansException e) { LOGGER.error("BeanUtil property copy failed :BeansException", e); } catch (Exception e) { LOGGER.error("BeanUtil for (E source : input) { T target = BeanUtils.instantiate(clzz); BeanUtil.copyProperties 21,"it"); Employee ee2=new Employee("B",23,"account"); User user=new User(); BeanUtil.copyProperties User> output=new ArrayList<>(); List<Employee> source= Arrays.asList(ee1,ee2); output=BeanUtil.convertList2List
1.234"; //判断是否为数字、整数、浮点数 NumberUtil.isNumber(n3); NumberUtil.isInteger(n3); NumberUtil.isDouble(n3); BeanUtil brand.setId(1L); brand.setName("小米"); brand.setShowStatus(0); //Bean转Map Map<String, Object> map = BeanUtil.beanToMap (brand); LOGGER.info("beanUtil bean to map:{}", map); //Map转Bean PmsBrand mapBrand = BeanUtil.mapToBean (map, PmsBrand.class, false); LOGGER.info("beanUtil map to bean:{}", mapBrand); //Bean属性拷贝 PmsBrand copyBrand = new PmsBrand(); BeanUtil.copyProperties(brand, copyBrand); LOGGER.info("beanUtil copy properties:{
我们来开发NeedSetValue注解的功能 @Component @Aspect public class SetFieldValueAspect { @Autowired BeanUtil beanUtil; @Around("@annotation(com.lvshen.demo.annotation.NeedSetValueField)") public Object Throwable { Object o = point.proceed(); if (o instanceof Collection) { this.beanUtil.setFieldValueForCol } else { //返回结果不是集合的逻辑 } return o; } } 使用AOP,执行方法后会执行后置处理this.beanUtil.setFieldValueForCol
1.234"; //判断是否为数字、整数、浮点数 NumberUtil.isNumber(n3); NumberUtil.isInteger(n3); NumberUtil.isDouble(n3); BeanUtil brand.setId(1L); brand.setName("小米"); brand.setShowStatus(0); //Bean转Map Map<String, Object> map = BeanUtil.beanToMap (brand); LOGGER.info("beanUtil bean to map:{}", map); //Map转Bean PmsBrand mapBrand = BeanUtil.mapToBean (map, PmsBrand.class, false); LOGGER.info("beanUtil map to bean:{}", mapBrand); //Bean属性拷贝 PmsBrand copyBrand = new PmsBrand(); BeanUtil.copyProperties(brand, copyBrand); LOGGER.info("beanUtil copy properties:{
@Test(description = "BeanUtil使用:JavaBean的工具类") public void beanUtil() { Dog dog = new Dog(); dog.setName ("beanUtil bean to map:{}", map); //Map转Bean Dog mapDog = BeanUtil.mapToBean(map, Dog.class, false) ; log.info("beanUtil map to bean:{}", mapDog); //Bean属性拷贝 Dog copyDog = new Dog(); BeanUtil.copyProperties (dog, copyDog); log.info("beanUtil copy properties:{}", copyDog); } 运行结果: beanUtil bean to map:{name =大黄, weight=5.14} beanUtil map to bean:Dog(name=大黄, weight=5.14) beanUtil copy properties:Dog(name=大黄
target = JSON.parseObject(JSON.toJSONString(source), clazz); //把源对象属性赋值给目标对象 BeanUtil.copyProperties T target = JSON.parseObject(JSON.toJSONString(obj), clazz); //把源对象属性赋值给目标对象 BeanUtil.copyProperties
——海子 使用hutool实现 package com.ruben.simplescaffold; import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.util.ClassUtil > type) { return Arrays.stream(ReflectUtil.getFields(type)).map(field -> { if (BeanUtil.isBean Class<Object> aClass = ClassUtil.loadClass(argument.getTypeName()); if (BeanUtil.isBean
1.234"; //判断是否为数字、整数、浮点数 NumberUtil.isNumber(n3); NumberUtil.isInteger(n3); NumberUtil.isDouble(n3); BeanUtil brand.setId(1L); brand.setName("小米"); brand.setShowStatus(0); //Bean转Map Map<String, Object> map = BeanUtil.beanToMap (brand); LOGGER.info("beanUtil bean to map:{}", map); //Map转Bean PmsBrand mapBrand = BeanUtil.mapToBean (map, PmsBrand.class, false); LOGGER.info("beanUtil map to bean:{}", mapBrand); //Bean属性拷贝 PmsBrand copyBrand = new PmsBrand(); BeanUtil.copyProperties(brand, copyBrand); LOGGER.info("beanUtil copy properties:{