binder增加单对象绑定接口

This commit is contained in:
mazhicheng 2020-02-29 12:31:14 +08:00
parent 4bf89f5989
commit 95ed4ebbaf
2 changed files with 43 additions and 4 deletions

View File

@ -38,9 +38,24 @@ public class RelationsBinder {
private static final Logger log = LoggerFactory.getLogger(RelationsBinder.class);
/**
* 自动转换和绑定VO中的注解关联
* @param entityList
* @param voClass
* 自动转换和绑定单个VO中的注解关联禁止循环调用多个对象请调用convertAndBind(voList, voClass)
* @param voClass 需要转换的VO class
* @param <E>
* @param <VO>
* @return
*/
public static <E, VO> VO convertAndBind(E entity, Class<VO> voClass){
// 转换为VO列表
VO vo = BeanUtils.convert(entity, voClass);
// 自动绑定关联对象
bind(vo);
return vo;
}
/**
* 自动转换和绑定多个VO中的注解关联
* @param entityList 需要转换的VO list
* @param voClass VO class
* @param <E>
* @param <VO>
* @return
@ -54,7 +69,20 @@ public class RelationsBinder {
}
/**
* 自动绑定关联对象
* 自动绑定单个VO的关联对象禁止循环调用多个对象请调用bind(voList)
* @param vo 需要注解绑定的对象
* @return
* @throws Exception
*/
public static <VO> void bind(VO vo){
List<VO> voList = new ArrayList<>(1);
voList.add(vo);
bind(voList);
}
/**
* 自动绑定多个VO集合的关联对象
* @param voList 需要注解绑定的对象集合
* @return
* @throws Exception
*/

View File

@ -2,6 +2,7 @@ package diboot.core.test.binder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.diboot.core.binding.RelationsBinder;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.JSON;
import com.diboot.core.util.V;
import diboot.core.test.StartupApplication;
@ -52,6 +53,16 @@ public class TestEntityBinder {
System.out.println(JSON.stringify(vo.getOrganizationVO()));
System.out.println(JSON.stringify(vo));
}
// 单个entity接口测试
EntityBinderVO singleVO = BeanUtils.convert(userList.get(0), EntityBinderVO.class);
RelationsBinder.bind(singleVO);
// 验证直接关联和通过中间表间接关联的绑定
Assert.assertEquals(singleVO.getDepartmentId(), singleVO.getDepartment().getId());
Assert.assertNotNull(singleVO.getDepartment().getOrgId());
// 测试绑定VO
Assert.assertNotNull(singleVO.getOrganizationVO());
System.out.println(JSON.stringify(singleVO.getOrganizationVO()));
System.out.println(JSON.stringify(singleVO));
}
}