mybatis学习-树形案例(三)

目录

一、部门树(自关联查询)

1.1 数据表设计

1.2 业务代码实现

 二 部门和员工关系树(一对多案例)

2.1 数据库设计

 2.2 业务代码实现


 

 

一、部门树(自关联查询)

        最终效果如下:

{
    "code":0,
    "msg":"Success",
    "data":[
        {
            "id":"1",
            "deptName":"总经理部",
            "deptCode":"111",
            "parentId":"0",
            "children":[
                {
                    "id":"2",
                    "deptName":"技术部",
                    "deptCode":"22",
                    "parentId":"1",
                    "children":[
                        {
                            "id":"33",
                            "deptName":"技术分部",
                            "deptCode":"3423",
                            "parentId":"2",
                            "children":[

                            ]
                        }
                    ]
                },
                {
                    "id":"3",
                    "deptName":"大数据部",
                    "deptCode":"23",
                    "parentId":"1",
                    "children":[

                    ]
                }
            ]
        }
    ]
}

 实现步骤

1.1 数据表设计

        部门表和员工表(一个部门对应多个员工,一个员工只能属于一个部门 属于一对多的关系)

部门表:

084697d69c3b45fc85e5839173078040.png

1.2 业务代码实现

DeptMapper.xml文件:
<mapper namespace="com.test.dao.DeptMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.test.entity.Dept">
        <id column="id" property="id" />
        <result column="dept_name" property="deptName" />
        <result column="dept_code" property="deptCode" />
        <result column="parent_id" property="parentId" />
        <collection property="children" ofType="com.test.entity.Dept" column="id"
                    select="com.test.dao.DeptMapper.listDeptByParentId" />
    </resultMap>

    <select id="listDeptByParentId" resultMap="BaseResultMap">
        SELECT
            *
        FROM
            sys_dept
        WHERE
            parent_id = #{parentId}
    </select>
</mapper>
DeptMapper.java文件:
List<Dept> listDeptByParentId(String parentId);

controller入口:

@GetMapping("tree")
public R tree(String id){
    List<Dept> list = deptMapper.listDeptByParentId(id);
    return R.ok(list);
}

 二 部门和员工关系树(一对多案例)

         一个部门对应多个员工,一个员工只能属于一个部门 属于一对多的关系

效果图:最终返回数据格式:

{
    "code":0,
    "msg":"Success",
    "data":[
        {
            "id":"2",
            "deptName":"技术部",
            "parentId":"1",
            "children":[
                {
                    "id":"33",
                    "deptName":"技术分部",
                    "parentId":"2",
                    "children":[

                    ],
                    "userchildren":[

                    ]
                }
            ],
            "userchildren":[
                {
                    "username":"查三",
                    "userId":"1",
                    "deptId":"2"
                },
                {
                    "username":"李四",
                    "userId":"2",
                    "deptId":"2"
                }
            ]
        },
        {
            "id":"3",
            "deptName":"大数据部",
            "parentId":"1",
            "children":[

            ],
            "userchildren":[

            ]
        }
    ]
}

2.1 数据库设计

部门表数据:

c9105d2ef8014c79a26d1abceb56304c.png

员工表数据:

cabbbe7fe6fe47c0a7d3c54c43f15dd5.png

 

 2.2 业务代码实现

实体类:

 

部门实体:

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName("sys_dept")
public class Dept extends Model<Dept> {

    private static final long serialVersionUID = 1L;

    @TableId(type = IdType.ID_WORKER)
    private String id;

    private String deptName;

    private String parentId;

    @TableField(exist = false)
    private List<Dept> children;

    @TableField(exist = false)
    private List<UserVo> userchildren;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }
}

用户实体:

@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class UserVo {

    private static final long serialVersionUID = 1L;

    private String username;

    private String userId;

    private String deptId;
}

xml文件:

73772b00f4794db595bb1f50597b1496.png

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.dao.DeptMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.test.entity.Dept">
        <id column="id" property="id" />
        <result column="dept_name" property="deptName" />
        <result column="parent_id" property="parentId" />
        <collection property="children" ofType="com.test.entity.Dept" column="id"
                    select="com.test.dao.DeptMapper.listDeptByParentId" />
        <collection property="userchildren" resultMap="userResultMap"/>
    </resultMap>

    <resultMap id="userResultMap" type="com.test.entity.UserVo">
        <id column="userId" property="userId" />
        <result column="username" property="username" />
        <result column="deptId" property="deptId" />
    </resultMap>

    <select id="listDeptByParentId" resultMap="BaseResultMap">
        SELECT
            a.*,
            b.id as userId,
            b.username as username,
            b.dept_id as deptId
        FROM
            sys_dept a left join sys_user b on a.id = b.dept_id
        WHERE
            a.parent_id = #{parentId}
    </select>
</mapper>
 

 


 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
MyBatis-Plus是一个MyBatis的增强工具,提供了许多实用的功能,其中就包括树形结构查询的支持。 在使用MyBatis-Plus进行树形结构查询时,需要使用到两个重要的注解: - @TableName:用于指定实体类对应的数据表名。 - @TableField:用于指定实体类中的属性对应的数据表中的列名。 假设我们有一个表t_menu,它的结构如下: | id | parent_id | name | |----|-----------|----------| | 1 | 0 | 菜单管理 | | 2 | 1 | 添加菜单 | | 3 | 1 | 修改菜单 | | 4 | 2 | 添加一级菜单 | | 5 | 2 | 添加二级菜单 | 我们需要查询出所有的菜单,并按照树形结构进行展示。代码如下: 首先,我们需要定义一个实体类Menu,用于映射t_menu表的数据。 ```java @Data @TableName("t_menu") public class Menu implements Serializable { private static final long serialVersionUID = 1L; @TableId("id") private Long id; @TableField("name") private String name; @TableField("parent_id") private Long parentId; @TableField(exist = false) private List<Menu> children; } ``` 在这个实体类中,我们使用了@TableName和@TableField注解来指定实体类对应的数据表名以及属性对应的数据表中的列名。其中,@TableField(exist = false)表示这个属性不是数据表中的列,不需要映射。 接下来,我们可以使用MyBatis-Plus提供的Wrapper接口来构建查询条件,并使用selectList方法查询出所有的菜单。 ```java @Service public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService { @Override public List<Menu> getMenuTree() { List<Menu> menuList = baseMapper.selectList(null); Map<Long, List<Menu>> menuMap = menuList.stream().collect(Collectors.groupingBy(Menu::getParentId)); List<Menu> rootMenuList = menuMap.getOrDefault(0L, Collections.emptyList()); for (Menu rootMenu : rootMenuList) { setChildren(rootMenu, menuMap); } return rootMenuList; } private void setChildren(Menu menu, Map<Long, List<Menu>> menuMap) { List<Menu> children = menuMap.getOrDefault(menu.getId(), Collections.emptyList()); menu.setChildren(children); for (Menu child : children) { setChildren(child, menuMap); } } } ``` 在getMenuTree方法中,我们首先调用baseMapper.selectList(null)方法查询出所有的菜单。然后,使用Java 8的Stream API将菜单按照parentId进行分组。接着,从分组后的结果中取出parentId为0的菜单,这些菜单就是根菜单。最后,我们通过递归的方式将每个菜单的子菜单设置进去,从而构建出完整的树形结构。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天雨编程

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值