Home Backend Development PHP Tutorial Mybatis implements reading tree structure data example code

Mybatis implements reading tree structure data example code

Jan 17, 2017 pm 01:44 PM

The detailed introduction of mybatis implementation of reading tree structure data is as follows:

Table structure

CREATE TABLE `lscrm_function_privilege` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '编号',
`create_id` varchar(30) NOT NULL DEFAULT 'sys',
`update_id` varchar(30) NOT NULL DEFAULT 'sys',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
`validity` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '有效性 1.有效 0. 无效',
`code` varchar(50) NOT NULL DEFAULT '' COMMENT '编码',
`function_name` varchar(50) NOT NULL DEFAULT '' COMMENT '名称',
`parent_id` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '父节点',
`is_leaf_node` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '是否叶子节点(叶子结点 就是度为0的结点 就是没有子结点的结点),在添加子节点时,需要将parent_id is_leaf_node 设置成0',
`sub_system_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '所属子系统',
`is_hidden` tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT 'UI是否隐藏,ui上不展示',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8 COMMENT='子code 按照父code 来创建\r\n如: customer.add';
Copy after login

Constructed data

INSERT INTO `lscrm_function_privilege` VALUES ('1', 'sys', 'sys', '2016-09-07 15:20:40', '2016-09-07 15:21:17', '1', 'WEB.PERMISSION', '权限管理', '0', '0', '1', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('2', 'sys', 'sys', '2016-09-07 15:21:12', '2016-09-07 15:21:40', '1', 'WEB.PERMISSION.USER-MGMT', '用户管理', '1', '0', '1', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('3', 'sys', 'sys', '2016-09-07 15:22:16', '2016-09-07 15:22:35', '1', 'WEB.PERMISSION.USER-MGMT.ADD', '新增用户', '2', '1', '1', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('4', 'sys', 'sys', '2016-09-07 15:23:17', '2016-09-07 15:23:17', '1', 'WEB.PERMISSION.USER-MGMT.MODIFY', '修改用户', '2', '1', '1', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('5', 'sys', 'sys', '2016-09-07 16:10:37', '2016-09-07 16:10:42', '1', 'APP.CUSTOMER', '客户管理', '0', '0', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('6', 'sys', 'sys', '2016-09-07 16:11:06', '2016-09-07 16:11:20', '1', 'APP.CUSTOMER.ADD', '添加客户', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('7', 'sys', 'sys', '2016-09-07 16:11:06', '2016-09-07 16:11:20', '1', 'APP.CUSTOMER.MODIFY', '修改客户', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('8', 'sys', 'sys', '2016-09-07 16:12:33', '2016-09-07 16:14:10', '1', 'APP.CUSTOMER.VIEWDETAIL', '查看客户详情', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('9', 'sys', 'sys', '2016-09-07 16:12:33', '2016-09-07 16:12:33', '1', 'APP.CUSTOMER.ADDVISIT', '添加拜访', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('10', 'sys', 'sys', '2016-09-07 16:13:59', '2016-09-07 16:14:17', '1', 'APP.CUSTOMER.VIEWDEMAND', '查看客户采购需求', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('11', 'sys', 'sys', '2016-09-07 16:13:59', '2016-09-07 16:14:17', '1', 'APP.CUSTOMER.VIEWORDER', '查看客户订单', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('12', 'sys', 'sys', '2016-09-07 16:13:59', '2016-09-07 16:14:17', '1', 'APP.CUSTOMER.VIEWSHOP', '查看客户订单', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('13', 'sys', 'sys', '2016-09-07 16:17:28', '2016-09-07 16:18:39', '1', 'APP.CUSTOMER.VIEWREPLY', '查看抢单列表', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('14', 'sys', 'sys', '2016-09-07 16:19:32', '2016-09-07 16:23:51', '1', 'APP.CUSTOMER.FASTOPENSHOP', '快捷开店', '5', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('15', 'sys', 'sys', '2016-09-07 16:22:33', '2016-09-07 16:22:33', '1', 'APP.DEMAND', '需求管理', '0', '0', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('17', 'sys', 'sys', '2016-09-07 16:23:21', '2016-09-07 16:23:21', '1', 'APP.DEMAND.PRIVATE', '私海需求', '15', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('18', 'sys', 'sys', '2016-09-07 16:23:58', '2016-09-07 16:23:58', '1', 'APP.DEMAND.FEEDBACK', '找版反馈', '15', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('19', 'sys', 'sys', '2016-09-07 16:24:47', '2016-09-07 16:24:47', '1', 'APP.DEMAND.PUSHSEARCH', '推送卖家搜索', '15', '1', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('20', 'sys', 'sys', '2016-09-07 16:25:56', '2016-09-07 16:25:56', '1', 'APP.KPI', '绩效管理', '0', '0', '2', '0');
INSERT INTO `lscrm_function_privilege` VALUES ('21', 'sys', 'sys', '2016-09-07 16:27:02', '2016-09-07 16:27:02', '1', 'APP.KPI.VIEWDATA', '查看绩效数据', '20', '1', '2', '0');
Copy after login

Entity bean

package com.lianshang.crm.biz.entity;
import com.google.common.base.MoreObjects;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class LscrmFunctionPrivilegeEntity {
/**编号**/
private int id;
private String createId;
private String updateId;
/**创建时间**/
private Date createTime;
/**修改时间**/
private Date updateTime=new Date();
/**有效性 1.有效 0. 无效**/
private int validity=1;
/**编码**/
private String code;
/**名称**/
private String functionName;
/**父节点**/
private int parentId;
/**是否叶子节点(叶子结点 就是度为0的结点 就是没有子结点的结点),在添加子节点时,需要将parent_id is_leaf_node 设置成0**/
private int isLeafNode;
/**所属子系统**/
private int subSystemId;
/**UI是否隐藏,ui上不展示**/
private int isHidden=1;
private List<LscrmFunctionPrivilegeEntity> privilegeList = new ArrayList<>();
//get()/set()省略。。。
}
package com.lianshang.crm.biz.entity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* Created by zhenyu on 2016/9/8.
*/
public class LscrmFunctionPrivilegeTree {
/**编号**/
private int id;
private String createId;
private String updateId;
/**创建时间**/
private Date createTime;
/**修改时间**/
private Date updateTime=new Date();
/**有效性 1.有效 0. 无效**/
private int validity=1;
/**编码**/
private String code;
/**名称**/
private String functionName;
/**父节点**/
private int parentId;
/**是否叶子节点(叶子结点 就是度为0的结点 就是没有子结点的结点),在添加子节点时,需要将parent_id is_leaf_node 设置成0**/
private int isLeafNode;
/**所属子系统**/
private int subSystemId;
/**UI是否隐藏,ui上不展示**/
private int isHidden=1;
private List<LscrmFunctionPrivilegeEntity> privilegeList = new ArrayList<>();
}
Copy after login

mapper configuration

<?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.lianshang.crm.biz.dao.LscrmFunctionPrivilegeDao">
<cache
eviction="FIFO"
flushInterval="60000"
size="1024"
readOnly="true"/>
<resultMap id="LscrmFunctionPrivilegeResult" type="LscrmFunctionPrivilegeEntity">
<id property="id" column="id"/>
<result property="createId" column="create_id"/>
<result property="updateId" column="update_id"/>
<result property="createTime" column="create_time"/>
<result property="updateTime" column="update_time"/>
<result property="validity" column="validity"/>
<result property="code" column="code"/>
<result property="functionName" column="function_name"/>
<result property="parentId" column="parent_id"/>
<result property="isLeafNode" column="is_leaf_node"/>
<result property="subSystemId" column="sub_system_id"/>
<result property="isHidden" column="is_hidden"/>
</resultMap>
<resultMap id="SubPrivilegesResult" type="LscrmFunctionPrivilegeEntity" extends="LscrmFunctionPrivilegeResult">
<collection property="privilegeList" javaType="java.util.ArrayList" column="id"
ofType="LscrmFunctionPrivilegeEntity" select="selectSubPrivileges"></collection>
</resultMap>
<resultMap id="LscrmFunctionPrivilegeTreeResult" type="LscrmFunctionPrivilegeTree"
extends="SubPrivilegesResult">
<!--<association property="functionPrivilege" column="id" javaType="LscrmFunctionPrivilegeEntity" resultMap="LscrmFunctionPrivilegeResult"/>-->
<collection property="privilegeList" javaType="java.util.ArrayList" column="id"
ofType="LscrmFunctionPrivilegeEntity" select="selectSubPrivileges"></collection>
</resultMap>
<sql id="tbl_name">
lscrm_function_privilege
</sql>
<sql id="role_privilege_tbl_name">
lscrm_role_privilege
</sql>
<sql id="select_sql">
select id, create_id, update_id, create_time, update_time, validity, code, function_name, parent_id,
is_leaf_node, sub_system_id, is_hidden from
<include refid="tbl_name"/>
</sql>
<select id="readAllPrivileges" resultMap="LscrmFunctionPrivilegeTreeResult" useCache="true">
<include refid="select_sql"/>
WHERE parent_id = 0
<if test="subSystemId > 0 ">
AND sub_system_id= #{subSystemId}
</if>
</select>
<select id="selectSubPrivileges" resultMap="SubPrivilegesResult">
<include refid="select_sql"/>
WHERE parent_id = #{id}
</select>
</mapper>
package com.lianshang.crm.biz.dao;
import com.lianshang.crm.api.dto.LscrmFunctionPrivilege;
import com.lianshang.crm.biz.entity.LscrmFunctionPrivilegeEntity;
import com.lianshang.crm.biz.entity.LscrmFunctionPrivilegeTree;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface LscrmFunctionPrivilegeDao {
List<LscrmFunctionPrivilegeTree> readAllPrivileges(@Param("subSystemId")int subSystemId);
}
Copy after login

service

package com.lianshang.crm.biz.service.impl;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Function;
import com.lianshang.common.utils.general.GeneralResult;
import com.lianshang.common.utils.general.GuavaUtil;
import com.lianshang.common.utils.general.StringUtil;
import com.lianshang.crm.api.dto.LscrmFunctionPrivilege;
import com.lianshang.crm.api.service.LscrmFunctionPrivilegeService;
import com.lianshang.crm.biz.dao.LscrmFunctionPrivilegeDao;
import com.lianshang.crm.biz.entity.LscrmFunctionPrivilegeEntity;
import com.lianshang.crm.biz.entity.LscrmFunctionPrivilegeTree;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static com.google.common.collect.FluentIterable.from;
public class LscrmFunctionPrivilegeServiceImpl implements LscrmFunctionPrivilegeService {
@Autowired
private LscrmFunctionPrivilegeDao lscrmFunctionPrivilegeDao;
@Override
public GeneralResult<String> readAllPrivileges(int subSystemId) {
List<LscrmFunctionPrivilegeTree> privilegeTrees = lscrmFunctionPrivilegeDao.readAllPrivileges(subSystemId);
return new GeneralResult<>(JSON.toJSONString(privilegeTrees));
}
}
Copy after login

Return results

[
{
"code": "WEB.PERMISSION",
"createId": "sys",
"createTime": 1473232840000,
"functionName": "权限管理",
"hidden": 0,
"id": 1,
"leafNode": 0,
"parentId": 0,
"privilegeList": [
{
"code": "WEB.PERMISSION.USER-MGMT",
"createId": "sys",
"createTime": 1473232872000,
"functionName": "用户管理",
"hidden": 0,
"id": 2,
"leafNode": 0,
"parentId": 1,
"privilegeList": [
{
"code": "WEB.PERMISSION.USER-MGMT.ADD",
"createId": "sys",
"createTime": 1473232936000,
"functionName": "新增用户",
"hidden": 0,
"id": 3,
"leafNode": 1,
"parentId": 2,
"privilegeList": [],
"subSystemId": 1,
"updateId": "sys",
"updateTime": 1473232955000,
"validity": 1
},
{
"code": "WEB.PERMISSION.USER-MGMT.MODIFY",
"createId": "sys",
"createTime": 1473232997000,
"functionName": "修改用户",
"hidden": 0,
"id": 4,
"leafNode": 1,
"parentId": 2,
"privilegeList": [],
"subSystemId": 1,
"updateId": "sys",
"updateTime": 1473232997000,
"validity": 1
}
],
"subSystemId": 1,
"updateId": "sys",
"updateTime": 1473232900000,
"validity": 1
}
],
"subSystemId": 1,
"updateId": "sys",
"updateTime": 1473232877000,
"validity": 1
}
]
Copy after login

The above is the example code of mybatis implemented by the editor to read tree structure data. I hope it will be helpful to you. If you have any questions, please leave me a message. Editor Will reply to everyone promptly. I would also like to thank you all for your support of the PHP Chinese website!

For more articles related to mybatis implementation of reading tree structure data example code, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

What are Enumerations (Enums) in PHP 8.1? What are Enumerations (Enums) in PHP 8.1? Apr 03, 2025 am 12:05 AM

The enumeration function in PHP8.1 enhances the clarity and type safety of the code by defining named constants. 1) Enumerations can be integers, strings or objects, improving code readability and type safety. 2) Enumeration is based on class and supports object-oriented features such as traversal and reflection. 3) Enumeration can be used for comparison and assignment to ensure type safety. 4) Enumeration supports adding methods to implement complex logic. 5) Strict type checking and error handling can avoid common errors. 6) Enumeration reduces magic value and improves maintainability, but pay attention to performance optimization.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

See all articles