将数据写入到 Model
中
@RequestMapping("/menulist")
public String MenuManagerList(Model model){
List<MenuManagerDO> menuManagerList = menuManagerService.MenuManagerList();
model.addAttribute("menuManagerList", menuManagerList);
return "menulist";
}
在html文件中设置html 头
<html xmlns:th="http://www.thymeleaf.org">
<th:block th:fragment="row(menuManagers)">
<ul th:each="menu,index:${menuManagers}">
<li th:if="${#lists.isEmpty(menu.subMenuManager)}">
<p th:text="${menu.menu_name}"></p>
</li>
<li th:if="not ${#lists.isEmpty(menu.subMenuManager)}" >
<p th:text="${menu.menu_name}"></p>
<th:block th:include="this::row(${menu.subMenuManager})"></th:block>
</li>
</ul>
</th:block>
<div th:include="this::row(${menuManagerList})"/>
提示:this::
表示递归模板在本页其他文件则需要指定路径
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel="stylesheet" href="css/reset.css" />
<link rel="stylesheet" href="css/login.css" />
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/login.js"></script>
</head>
<body>
<div th:include="this::row(${menuManagerList})"/>
<th:block th:fragment="row(menuManagers)">
<ul th:each="menu,index:${menuManagers}">
<li th:if="${#lists.isEmpty(menu.subMenuManager)}">
<p th:text="${menu.menu_name}"></p>
</li>
<li th:if="not ${#lists.isEmpty(menu.subMenuManager)}" >
<p th:text="${menu.menu_name}"></p>
<th:block th:include="this::row(${menu.subMenuManager})"></th:block>
</li>
</ul>
</th:block>
</body>
</html>
public class MenuManagerDO {
private Integer id;
private Integer pid;
private String menu_name;
private List<MenuManagerDO> subMenuManager = new ArrayList<>();
public List<MenuManagerDO> getSubMenuManager() {
return subMenuManager;
}
public void setSubMenuManager(List<MenuManagerDO> subMenuManager) {
this.subMenuManager = subMenuManager;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getMenu_name() {
return menu_name;
}
public void setMenu_name(String menu_name) {
this.menu_name = menu_name;
}
}
xml
配置<?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.demo.demoproject.mapper.MenuManager">
<resultMap id="baseMenuManagerResultMap" type="com.demo.demoproject.pojo.MenuManagerDO">
<id column="id" property="id" />
<result column="pid" property="pid" />
<result column="menu_name" property="menu_name"/>
<collection property="subMenuManager" ofType="com.demo.demoproject.pojo.MenuManagerDO" column="{pid=id}" select="selectAll"/>
</resultMap>
<select id="selectAll" resultMap="baseMenuManagerResultMap">
select * from `menu_manager` where `pid` = #{pid}
</select>
</mapper>
public interface MenuManager {
List<MenuManagerDO> selectAll(@Param("pid") Integer pid);
}
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for menu_manager
-- ----------------------------
DROP TABLE IF EXISTS `menu_manager`;
CREATE TABLE `menu_manager` (
`id` int(11) NOT NULL,
`menu_name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`pid` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
https://www.leftso.com/article/2408061245091060.html