首页 > 猿辅导-内容服务后端-java实习-一面凉经
头像
诨号无敌鸭
编辑于 08-15 18:00
+ 关注

猿辅导-内容服务后端-java实习-一面凉经

无自我介绍,看着你在白板上写题 限时30min 挂了嘻嘻😁

 题目:
 // 1. 设计一个部门 MySQL 表

// 2. 实现接口:通过 中间部门ids 获取这些节点下的叶子部门ids

// 3. 设计一个 Java 数据对象,数据对象中包含子节点列表,通过 ids 批量获取该对象

应该大概是这样吧 重点把数据库表 & sql & 多叉树递归 写出来应该可以
建表语句:
create table debt(

    debt_id bigint primary key,

    parent_id bigint
);

service层:
//通过部门id查询叶子部门id列表
public List<Long> getDebtById(Long id){
    if(id == null){
        return new ArrayList<>();
    }
    return debetMapper.getDebtById(id);    
}

//通过部门id列表查所有叶子部门id
public List<Long> getDebtByIds(List<Long> ids){
    if(ids == null || ids.isEmpty()){
        return new ArrayList<>();
    }
    List<Long> leafIds = new ArrayList<>();
    for (Long id : ids) {
        getLeafDebtByIdRecursive(id, leafIds);
    }
    return leafIds;
}
 private void getLeafDebtByIdRecursive(Long id, List<Long> leafIds) {
        List<DebetDO> children = debetMapper.getChildrenByParentId(id);
        if (children == null || children.isEmpty()) {
            // 没有子部门,说明是叶子部门
            leafIds.add(id);
            return;
        }
        // 有子部门,递归往下找
        for (DebetDO child : children) {
            getLeafDebtByIdRecursive(child.getDebtId(), leafIds);
        }
    }

java对象:
@Data
class DebetDO{

    Long debtId;

    Long parentId;

    List<DebetDO> children = new ArrayList<>();
}

mapper层:
public interfece DebetMapper{

    List<Long> getDebtById(@Param("id")Long id);

    List<Long> getDebtByIds(@Param("ids")List<Long> ids);
}

DebetMapper.xml:、
// select debet_id from debt where parent_id = {};
// 如果是间接的部门怎么去查询
// 子查询:先把二级部门查出来,然后去查三级部门 ,这个要是所有的子部门全部查出来怎么查
//select debet_id from debt where parent_id = {id}
<select id="getDebtByIds" resultType="long" parameterType="list">
    SELECT debt_id FROM debt
    WHERE parent_id = #{parentId}
</select>

// select debet_id from debt where parent_id in {};
<select id="getDebtByIds" resultType="long" parameterType="list">
    SELECT debt_id FROM debt
    WHERE parent_id IN
    <foreach collection="ids" item="id" open="(" close=")" separator=",">
        #{id}
    </foreach>
</select>



全部评论

(5) 回帖
加载中...
话题 回帖

近期热帖

热门推荐