Java 递归查询部门树形结构数据的实践
短信预约 -IT技能 免费直播动态提醒
说明:在开发中,我们经常使用树形结构来展示菜单选项,如图:
那么我们在后端怎么去实现这样的一个功能呢?
1、数据库表:department
2、编写sql映射语句
<select id="selectDepartmentTrees" resultType="com.welb.entity.Department">
select * from department
<where>
<if test="updepartmentcode!=null">
and UpDepartmentCode=#{updepartmentcode}
</if>
</where>
</select>
3、创建实体类
public class Department {
private String departmentcode;//部门code
private String departmentname;//部门名称
private String updepartmentcode="0";//上级部门
private String departmentdesc;//部门描述
private List<Department> childDept =new ArrayList();//部门信息树
public String getDepartmentcode() {
return departmentcode;
}
public void setDepartmentcode(String departmentcode) {
this.departmentcode = departmentcode == null ? null : departmentcode.trim();
}
public String getDepartmentname() {
return departmentname;
}
public void setDepartmentname(String departmentname) {
this.departmentname = departmentname == null ? null : departmentname.trim();
}
public String getUpdepartmentcode() {
return updepartmentcode;
}
public void setUpdepartmentcode(String updepartmentcode) {
this.updepartmentcode = updepartmentcode == null ? null : updepartmentcode.trim();
}
public String getDepartmentdesc() {
return departmentdesc;
}
public void setDepartmentdesc(String departmentdesc) {
this.departmentdesc = departmentdesc == null ? null : departmentdesc.trim();
}
public List<Department> getChildDept() {
return childDept;
}
public void setChildDept(List<Department> childDept) {
this.childDept = childDept;
}
}
4、定义mapper
@Mapper
public interface DepartmentMapper {
List<Department>selectDepartmentTrees(Department department);
}
5、定义service业务层实现
@Service
@Transactional
public class DepartmentImpl implements IDepartmentService {
@Resource
DepartmentMapper departmentMapper;
@Override
public List<Department> selectDepartmentTrees(Department department) {
return departmentMapper.selectDepartmentTrees(department);
}
}
6、控制层实现
@RestController
@RequestMapping("department")
public class DepartmentController {
@Resource
IDepartmentService departmentService;
@Resource
IStationService stationService;
@RequestMapping("/treelist")
public Object selectAll(Department department) {
ModelMap map = new ModelMap();
try {
List<Department> departmentDtos = getDepartmentList(department);
map.put("msg", "查询部门成功");
map.put("data", departmentDtos);
map.put("code", 0);
} catch (Exception e) {
e.printStackTrace();
map.put("msg", "查询部门失败");
map.put("code", 1);
}
return map;
}
private List<Department> getDepartmentList(Department department) {
List<Department> departments =departmentService.selectDepartmentTrees(department);
if (departments.size()>0){
Department department1=new Department();
for (int i = 0; i <departments.size() ; i++) {
department1.setUpdepartmentcode(departments.get(i).getDepartmentcode());
List<Department>dtos=getDepartmentList(department1);
departments.get(i).setChildDept(dtos);
}
}
return departments;
}
}
7、进行测试,这里我用postmain工具测试,结果如下图
到这里,递归查询部门树形结构数据就完成了
到此这篇关于Java 递归查询部门树形结构数据的实践的文章就介绍到这了,更多相关Java 递归查询树形结构内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341