service层

/**
* 获取所有部门
* @return
*/
@Override
public List<Department> getAllDepartments() {
return departmentMapper.getAllDepartments();
}

mapper层

/**
* 查询所有部门
* @return
*/
@Select("select * from t_department where parent_id = 0")
@Results(id = "departmentMap",value = {
@Result(property = "id",column = "id"),
@Result(property = "name",column = "name"),
@Result(property = "parentId",column = "parent_id"),
@Result(property = "depPath",column = "dep_path"),
@Result(property = "enabled",column = "enabled"),
@Result(property = "isParent",column = "is_parent"),
@Result(property = "children",column = "id",many = @Many(select = "getAllDepartmentsId")),//这里表示一对多 一里面包含多
})
List<Department> getAllDepartments();

@Select("select * from t_department where parent_id = #{parent_id}")
@ResultMap("departmentMap")
List<Department> getAllDepartmentsId();

mybatis-plus使用注解方式实现递归_实体类
mybatis-plus使用注解方式实现递归_实体类_02


children: 它是在实体类定义的字段 private List children;
id = "departmentMap": 通过注解@Results来指定对应关系 指定@ResultMap("departmentMap")
column指定数据库字段的名称,property指定实体类属性的名称,jdbcType数据库字段类型,@Result里的id值为true表明主键,默认false