mybatis查询返回map键值对的问题
业务场景:部门表数据批量导入,每条数据需要查表获取parentId,ancestors并赋值
目标:一次查出,键(部门名称)值(部门信息)已确认:部门名称不存在重复得情况
1、sql
<resultMap id="result" type="com.zhhs.project.system.vo.BeanVo"> <result column="dept_id" property="id"/> <result column="dept_name" property="name"/> <result column="ancestors" property="ancestors"/> </resultMap> <select id="selectDepts" resultMap="result"> select dept_id, dept_name,ancestors from sys_dept where del_flag = '0' and status = '0' </select>
2、定义key
/** * 返回部门键值对:键为部门名称 * @return */ @MapKey("name") Map<String, BeanVo> selectDepts();
3、对象
@Data public class BeanVo { private Long id; private String name; private String ancestors; }
4、使用:代码片段
for (DeptTemplate deptTemplate : deptList){
SysDept dept = new SysDept(); BeanUtils.copyProperties(deptTemplate,dept); dept.setParentId(deptMap.get(deptTemplate.getParentName()).getId()); dept.setAncestors(deptMap.get(deptTemplate.getParentName()).getAncestors() + ","+ deptMap.get(deptTemplate.getParentName()).getId());}
-----------
}
参考:https://blog.csdn.net/gongdileidechouzhu/article/details/121478926
浙公网安备 33010602011771号