Dalam proses pembangunan sebenar, selalunya perlu untuk menanyakan pepohon nod dan mendapatkan senarai nod anak berdasarkan nod yang ditentukan Operasi mendapatkan pepohon nod direkodkan di bawah , sekiranya berlaku kecemasan.
boleh digunakan untuk struktur data dengan perhubungan hierarki seperti organisasi jabatan sistem, klasifikasi produk, perhubungan bandar, dsb.
ialah nod akar, nod cawangan dan nod daun Model data adalah seperti berikut:
id | code | name | parent_code |
---|---|---|---|
1 | 10000 | 电脑 | 0 |
2 | 20000 | 手机 | 0 |
3 | 10001 | 联想笔记本 | 10000 |
4 | 10002 | 惠普笔记本 | 10000 |
5 | 1000101 | 联想拯救者 | 10001 |
6 | 1000102 | 联想小新系列 | 10001 |
Struktur jadual
CREATE TABLE `tree_table` ( `id` int NOT NULL AUTO_INCREMENT COMMENT "主键ID", `code` varchar(10) NOT NULL COMMENT "编码", `name` varchar(20) NOT NULL COMMENT "名称", `parent_code` varchar(10) NOT NULL COMMENT "父级编码", PRIMARY KEY (`id`) USING BTREE ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT="树形结构测试表";
Data jadual
INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10000", "电脑", "0"); INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10001", "联想笔记本", "10000"); INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("10002", "惠普笔记本", "10000"); INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("1000101", "联想拯救者", "10001"); INSERT INTO `tree_table`(`code`, `name`, `parent_code`) VALUES ("1000102", "联想小新系列", "10001");
Entiti
@Data @TableName("tree_table") @EqualsAndHashCode(callSuper = false) @Accessors(chain = true) public class TreeTable { /** * 主键ID */ @TableId(type = IdType.AUTO) private Integer id; /** * 编码 */ private String code; /** * 名称 */ private String name; /** * 父级编码 */ private String parentCode; /** * 子节点 */ @TableField(exist = false) private List<TreeTable> childNode; }
mybatis
pemeta
public interface TreeTableMapper extends BaseMapper<TreeTable> { /** * 获取树形结构数据 * * @return 树形结构 */ public List<TreeTable> noteTree(); }
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.springboot.example.mysqltree.mapper.TreeTableMapper"> <resultMap id="BaseResultMap" type="com.springboot.example.mysqltree.model.entity.TreeTable"> <result column="id" property="id"/> <result column="code" property="code"/> <result column="name" property="name"/> <result column="parent_code" property="parentCode"/> </resultMap> <resultMap id="NodeTreeResult" type="com.springboot.example.mysqltree.model.entity.TreeTable" extends="BaseResultMap"> <collection property="childNode" column="code" ofType="com.springboot.example.mysqltree.model.entity.TreeTable" javaType="java.util.ArrayList" select="nextNoteTree"> </collection> </resultMap> <sql id="Base_Column_List"> id, code, `name`, parent_code </sql> <select id="nextNoteTree" resultMap="NodeTreeResult"> select <include refid="Base_Column_List"/> from tree_table where parent_code=#[code] </select> <select id="noteTree" resultMap="NodeTreeResult"> select <include refid="Base_Column_List"/> from tree_table where parent_code="0" </select> </mapper>
noteTree: dapatkan semua data nod induk; 🎜>nextNoteTree: gelung untuk mendapatkan data nod anak sehingga hujung nod daun; >ofType: jenis kembali
Kelas permulaan
@Slf4j @Component public class TreeTableCommandLineRunner implements CommandLineRunner { @Resource private TreeTableMapper treeTableMapper; @Override public void run(String... args) throws Exception { log.info(JSONUtil.toJsonPrettyStr(treeTableMapper.noteTree())); } }
[ { "code": "10000", "childNode": [ { "code": "10001", "childNode": [ { "code": "1000101", "childNode": [ ], "parentCode": "10001", "name": "联想拯救者", "id": 5 }, { "code": "1000102", "childNode": [ ], "parentCode": "10001", "name": "联想小新系列", "id": 6 } ], "parentCode": "10000", "name": "联想笔记本", "id": 3 }, { "code": "10002", "childNode": [ ], "parentCode": "10000", "name": "惠普笔记本", "id": 4 } ], "parentCode": "0", "name": "电脑", "id": 1 } ]
<resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
Atas ialah kandungan terperinci Cara menggunakan springboot+mybatis plus untuk melaksanakan pertanyaan struktur pokok. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!