Search in sources :

Example 1 with CopyOptions

use of cn.hutool.core.bean.copier.CopyOptions in project hutool by looly.

the class BeanSheetReader method read.

@Override
@SuppressWarnings("unchecked")
public List<T> read(Sheet sheet) {
    final List<Map<String, Object>> mapList = mapSheetReader.read(sheet);
    if (Map.class.isAssignableFrom(this.beanClass)) {
        return (List<T>) mapList;
    }
    final List<T> beanList = new ArrayList<>(mapList.size());
    final CopyOptions copyOptions = CopyOptions.create().setIgnoreError(true);
    for (Map<String, Object> map : mapList) {
        beanList.add(BeanUtil.toBean(map, this.beanClass, copyOptions));
    }
    return beanList;
}
Also used : ArrayList(java.util.ArrayList) CopyOptions(cn.hutool.core.bean.copier.CopyOptions) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 2 with CopyOptions

use of cn.hutool.core.bean.copier.CopyOptions in project hutool by looly.

the class BeanUtilTest method copyBeanPropertiesFilterTest.

@Test
public void copyBeanPropertiesFilterTest() {
    Food info = new Food();
    info.setBookID("0");
    info.setCode("");
    Food newFood = new Food();
    CopyOptions copyOptions = CopyOptions.create().setPropertiesFilter((f, v) -> !(v instanceof CharSequence) || StrUtil.isNotBlank(v.toString()));
    BeanUtil.copyProperties(info, newFood, copyOptions);
    Assert.assertEquals(info.getBookID(), newFood.getBookID());
    Assert.assertNull(newFood.getCode());
}
Also used : CopyOptions(cn.hutool.core.bean.copier.CopyOptions) Test(org.junit.Test)

Example 3 with CopyOptions

use of cn.hutool.core.bean.copier.CopyOptions in project hutool by looly.

the class BeanUtilTest method beanToBeanCopyOptionsTest.

/**
 * @author dazer
 *  copyProperties(Object source, Object target, CopyOptions copyOptions)
 *  当:copyOptions的 setFieldNameEditor 不为空的时候,有bug,这里进行修复;
 */
@Test
public void beanToBeanCopyOptionsTest() {
    ChildVo1 childVo1 = new ChildVo1();
    childVo1.setChild_address("中国北京五道口");
    childVo1.setChild_name("张三");
    childVo1.setChild_father_name("张无忌");
    childVo1.setChild_mother_name("赵敏敏");
    CopyOptions copyOptions = CopyOptions.create().setFieldNameEditor(StrUtil::toUnderlineCase);
    ChildVo2 childVo2 = new ChildVo2();
    BeanUtil.copyProperties(childVo1, childVo2, copyOptions);
    Assert.assertEquals(childVo1.getChild_address(), childVo2.getChildAddress());
    Assert.assertEquals(childVo1.getChild_name(), childVo2.getChildName());
    Assert.assertEquals(childVo1.getChild_father_name(), childVo2.getChildFatherName());
    Assert.assertEquals(childVo1.getChild_mother_name(), childVo2.getChildMotherName());
}
Also used : StrUtil(cn.hutool.core.util.StrUtil) CopyOptions(cn.hutool.core.bean.copier.CopyOptions) Test(org.junit.Test)

Example 4 with CopyOptions

use of cn.hutool.core.bean.copier.CopyOptions in project hutool by looly.

the class BeanCopyMappingTest method copyPropertiesTest.

/**
 * https://gitee.com/dromara/hutool/issues/I4C48U <br>
 * 传递复制不要用注解别名,应该用动态映射
 */
@Test
public void copyPropertiesTest() {
    final CopyOptions copyOptions = CopyOptions.create().setFieldMapping(MapUtil.of("car", "carNo"));
    B b = B.builder().car("12312312").build();
    A a = A.builder().build();
    C c = C.builder().build();
    BeanUtil.copyProperties(b, a, copyOptions);
    BeanUtil.copyProperties(a, c);
    Assert.assertEquals("12312312", c.getCarNo());
}
Also used : CopyOptions(cn.hutool.core.bean.copier.CopyOptions) Test(org.junit.Test)

Example 5 with CopyOptions

use of cn.hutool.core.bean.copier.CopyOptions in project hutool by looly.

the class Issue1687Test method toBeanTest2.

@Test
public void toBeanTest2() {
    final SysUserFb sysUserFb = new SysUserFb();
    sysUserFb.setDepId("123");
    sysUserFb.setCustomerId("456");
    // 补救别名错位
    final CopyOptions copyOptions = CopyOptions.create().setFieldMapping(MapUtil.builder("depart", "depId").build());
    final SysUser sysUser = BeanUtil.toBean(sysUserFb, SysUser.class, copyOptions);
    Assert.assertEquals(new Long(123L), sysUser.getDepart());
    Assert.assertEquals(new Long(456L), sysUser.getOrgId());
}
Also used : CopyOptions(cn.hutool.core.bean.copier.CopyOptions) Test(org.junit.Test)

Aggregations

CopyOptions (cn.hutool.core.bean.copier.CopyOptions)5 Test (org.junit.Test)4 StrUtil (cn.hutool.core.util.StrUtil)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1