mybatis实现读取树结构数据实例代码
mybatis实现读取树结构数据详细介绍如下所示:
表结构
CREATETABLE`lscrm_function_privilege`( `id`int(10)unsignedNOTNULLAUTO_INCREMENTCOMMENT'编号', `create_id`varchar(30)NOTNULLDEFAULT'sys', `update_id`varchar(30)NOTNULLDEFAULT'sys', `create_time`timestampNOTNULLDEFAULTCURRENT_TIMESTAMPCOMMENT'创建时间', `update_time`timestampNOTNULLDEFAULTCURRENT_TIMESTAMPONUPDATECURRENT_TIMESTAMPCOMMENT'修改时间', `validity`tinyint(3)unsignedNOTNULLDEFAULT'1'COMMENT'有效性1.有效0.无效', `code`varchar(50)NOTNULLDEFAULT''COMMENT'编码', `function_name`varchar(50)NOTNULLDEFAULT''COMMENT'名称', `parent_id`tinyint(3)unsignedNOTNULLDEFAULT'0'COMMENT'父节点', `is_leaf_node`tinyint(3)unsignedNOTNULLDEFAULT'1'COMMENT'是否叶子节点(叶子结点就是度为0的结点就是没有子结点的结点),在添加子节点时,需要将parent_idis_leaf_node设置成0', `sub_system_id`int(10)unsignedNOTNULLDEFAULT'0'COMMENT'所属子系统', `is_hidden`tinyint(3)unsignedNOTNULLDEFAULT'0'COMMENT'UI是否隐藏,ui上不展示', PRIMARYKEY(`id`) )ENGINE=InnoDBAUTO_INCREMENT=22DEFAULTCHARSET=utf8COMMENT='子code按照父code来创建\r\n如:customer.add';
构造数据
INSERTINTO`lscrm_function_privilege`VALUES('1','sys','sys','2016-09-0715:20:40','2016-09-0715:21:17','1','WEB.PERMISSION','权限管理','0','0','1','0');
INSERTINTO`lscrm_function_privilege`VALUES('2','sys','sys','2016-09-0715:21:12','2016-09-0715:21:40','1','WEB.PERMISSION.USER-MGMT','用户管理','1','0','1','0');
INSERTINTO`lscrm_function_privilege`VALUES('3','sys','sys','2016-09-0715:22:16','2016-09-0715:22:35','1','WEB.PERMISSION.USER-MGMT.ADD','新增用户','2','1','1','0');
INSERTINTO`lscrm_function_privilege`VALUES('4','sys','sys','2016-09-0715:23:17','2016-09-0715:23:17','1','WEB.PERMISSION.USER-MGMT.MODIFY','修改用户','2','1','1','0');
INSERTINTO`lscrm_function_privilege`VALUES('5','sys','sys','2016-09-0716:10:37','2016-09-0716:10:42','1','APP.CUSTOMER','客户管理','0','0','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('6','sys','sys','2016-09-0716:11:06','2016-09-0716:11:20','1','APP.CUSTOMER.ADD','添加客户','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('7','sys','sys','2016-09-0716:11:06','2016-09-0716:11:20','1','APP.CUSTOMER.MODIFY','修改客户','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('8','sys','sys','2016-09-0716:12:33','2016-09-0716:14:10','1','APP.CUSTOMER.VIEWDETAIL','查看客户详情','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('9','sys','sys','2016-09-0716:12:33','2016-09-0716:12:33','1','APP.CUSTOMER.ADDVISIT','添加拜访','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('10','sys','sys','2016-09-0716:13:59','2016-09-0716:14:17','1','APP.CUSTOMER.VIEWDEMAND','查看客户采购需求','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('11','sys','sys','2016-09-0716:13:59','2016-09-0716:14:17','1','APP.CUSTOMER.VIEWORDER','查看客户订单','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('12','sys','sys','2016-09-0716:13:59','2016-09-0716:14:17','1','APP.CUSTOMER.VIEWSHOP','查看客户订单','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('13','sys','sys','2016-09-0716:17:28','2016-09-0716:18:39','1','APP.CUSTOMER.VIEWREPLY','查看抢单列表','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('14','sys','sys','2016-09-0716:19:32','2016-09-0716:23:51','1','APP.CUSTOMER.FASTOPENSHOP','快捷开店','5','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('15','sys','sys','2016-09-0716:22:33','2016-09-0716:22:33','1','APP.DEMAND','需求管理','0','0','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('17','sys','sys','2016-09-0716:23:21','2016-09-0716:23:21','1','APP.DEMAND.PRIVATE','私海需求','15','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('18','sys','sys','2016-09-0716:23:58','2016-09-0716:23:58','1','APP.DEMAND.FEEDBACK','找版反馈','15','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('19','sys','sys','2016-09-0716:24:47','2016-09-0716:24:47','1','APP.DEMAND.PUSHSEARCH','推送卖家搜索','15','1','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('20','sys','sys','2016-09-0716:25:56','2016-09-0716:25:56','1','APP.KPI','绩效管理','0','0','2','0');
INSERTINTO`lscrm_function_privilege`VALUES('21','sys','sys','2016-09-0716:27:02','2016-09-0716:27:02','1','APP.KPI.VIEWDATA','查看绩效数据','20','1','2','0');
实体bean
packagecom.lianshang.crm.biz.entity;
importcom.google.common.base.MoreObjects;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
publicclassLscrmFunctionPrivilegeEntity{
/**编号**/
privateintid;
privateStringcreateId;
privateStringupdateId;
/**创建时间**/
privateDatecreateTime;
/**修改时间**/
privateDateupdateTime=newDate();
/**有效性1.有效0.无效**/
privateintvalidity=1;
/**编码**/
privateStringcode;
/**名称**/
privateStringfunctionName;
/**父节点**/
privateintparentId;
/**是否叶子节点(叶子结点就是度为0的结点就是没有子结点的结点),在添加子节点时,需要将parent_idis_leaf_node设置成0**/
privateintisLeafNode;
/**所属子系统**/
privateintsubSystemId;
/**UI是否隐藏,ui上不展示**/
privateintisHidden=1;
privateList<LscrmFunctionPrivilegeEntity>privilegeList=newArrayList<>();
//get()/set()省略。。。
}
packagecom.lianshang.crm.biz.entity;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
/**
*Createdbyzhenyuon2016/9/8.
*/
publicclassLscrmFunctionPrivilegeTree{
/**编号**/
privateintid;
privateStringcreateId;
privateStringupdateId;
/**创建时间**/
privateDatecreateTime;
/**修改时间**/
privateDateupdateTime=newDate();
/**有效性1.有效0.无效**/
privateintvalidity=1;
/**编码**/
privateStringcode;
/**名称**/
privateStringfunctionName;
/**父节点**/
privateintparentId;
/**是否叶子节点(叶子结点就是度为0的结点就是没有子结点的结点),在添加子节点时,需要将parent_idis_leaf_node设置成0**/
privateintisLeafNode;
/**所属子系统**/
privateintsubSystemId;
/**UI是否隐藏,ui上不展示**/
privateintisHidden=1;
privateList<LscrmFunctionPrivilegeEntity>privilegeList=newArrayList<>();
}
mapper配置
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.lianshang.crm.biz.dao.LscrmFunctionPrivilegeDao">
<cache
eviction="FIFO"
flushInterval="60000"
size="1024"
readOnly="true"/>
<resultMapid="LscrmFunctionPrivilegeResult"type="LscrmFunctionPrivilegeEntity">
<idproperty="id"column="id"/>
<resultproperty="createId"column="create_id"/>
<resultproperty="updateId"column="update_id"/>
<resultproperty="createTime"column="create_time"/>
<resultproperty="updateTime"column="update_time"/>
<resultproperty="validity"column="validity"/>
<resultproperty="code"column="code"/>
<resultproperty="functionName"column="function_name"/>
<resultproperty="parentId"column="parent_id"/>
<resultproperty="isLeafNode"column="is_leaf_node"/>
<resultproperty="subSystemId"column="sub_system_id"/>
<resultproperty="isHidden"column="is_hidden"/>
</resultMap>
<resultMapid="SubPrivilegesResult"type="LscrmFunctionPrivilegeEntity"extends="LscrmFunctionPrivilegeResult">
<collectionproperty="privilegeList"javaType="java.util.ArrayList"column="id"
ofType="LscrmFunctionPrivilegeEntity"select="selectSubPrivileges"></collection>
</resultMap>
<resultMapid="LscrmFunctionPrivilegeTreeResult"type="LscrmFunctionPrivilegeTree"
extends="SubPrivilegesResult">
<!--<associationproperty="functionPrivilege"column="id"javaType="LscrmFunctionPrivilegeEntity"resultMap="LscrmFunctionPrivilegeResult"/>-->
<collectionproperty="privilegeList"javaType="java.util.ArrayList"column="id"
ofType="LscrmFunctionPrivilegeEntity"select="selectSubPrivileges"></collection>
</resultMap>
<sqlid="tbl_name">
lscrm_function_privilege
</sql>
<sqlid="role_privilege_tbl_name">
lscrm_role_privilege
</sql>
<sqlid="select_sql">
selectid,create_id,update_id,create_time,update_time,validity,code,function_name,parent_id,
is_leaf_node,sub_system_id,is_hiddenfrom
<includerefid="tbl_name"/>
</sql>
<selectid="readAllPrivileges"resultMap="LscrmFunctionPrivilegeTreeResult"useCache="true">
<includerefid="select_sql"/>
WHEREparent_id=0
<iftest="subSystemId>0">
ANDsub_system_id=#{subSystemId}
</if>
</select>
<selectid="selectSubPrivileges"resultMap="SubPrivilegesResult">
<includerefid="select_sql"/>
WHEREparent_id=#{id}
</select>
</mapper>
packagecom.lianshang.crm.biz.dao;
importcom.lianshang.crm.api.dto.LscrmFunctionPrivilege;
importcom.lianshang.crm.biz.entity.LscrmFunctionPrivilegeEntity;
importcom.lianshang.crm.biz.entity.LscrmFunctionPrivilegeTree;
importorg.apache.ibatis.annotations.Param;
importjava.util.List;
publicinterfaceLscrmFunctionPrivilegeDao{
List<LscrmFunctionPrivilegeTree>readAllPrivileges(@Param("subSystemId")intsubSystemId);
}
service
packagecom.lianshang.crm.biz.service.impl;
importcom.alibaba.fastjson.JSON;
importcom.google.common.base.Function;
importcom.lianshang.common.utils.general.GeneralResult;
importcom.lianshang.common.utils.general.GuavaUtil;
importcom.lianshang.common.utils.general.StringUtil;
importcom.lianshang.crm.api.dto.LscrmFunctionPrivilege;
importcom.lianshang.crm.api.service.LscrmFunctionPrivilegeService;
importcom.lianshang.crm.biz.dao.LscrmFunctionPrivilegeDao;
importcom.lianshang.crm.biz.entity.LscrmFunctionPrivilegeEntity;
importcom.lianshang.crm.biz.entity.LscrmFunctionPrivilegeTree;
importorg.springframework.beans.factory.annotation.Autowired;
importjava.util.ArrayList;
importjava.util.Date;
importjava.util.List;
importstaticcom.google.common.collect.FluentIterable.from;
publicclassLscrmFunctionPrivilegeServiceImplimplementsLscrmFunctionPrivilegeService{
@Autowired
privateLscrmFunctionPrivilegeDaolscrmFunctionPrivilegeDao;
@Override
publicGeneralResult<String>readAllPrivileges(intsubSystemId){
List<LscrmFunctionPrivilegeTree>privilegeTrees=lscrmFunctionPrivilegeDao.readAllPrivileges(subSystemId);
returnnewGeneralResult<>(JSON.toJSONString(privilegeTrees));
}
}
返回结果
[
{
"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
}
]
以上所述是小编给大家介绍的mybatis实现读取树结构数据实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!