hibernate 一对多自关联查询(如菜单表)

hibernate 有自带的菜单查询功能,当查询的节点有几千条时,hibernate自带的菜单查询会造成卡顿的情况,但一般情况下hibernate自带的菜单查询减少程序员的代码量,
简化了代码

相关测试数据(MySQL)

-- 一对多双向自关联
-- 菜单表
-- t_hibernate_sys_tree_node
-- t:表
-- sys:模块名缩写(system)
-- tree_noe:表名
create table t_hibernate_sys_tree_node
(
  tree_node_id int primary key auto_increment,                                                 -- ID
  tree_node_name varchar(50) not null,                                                         -- 名字
  tree_node_type int not null check(tree_node_type = 1 or tree_node_type = 2),                 -- 节点类型:1 枝节点 2 叶节点

  position int,                                                                             -- 位置
  parent_node_id int,                                                                         -- 父节点ID
  url varchar(1024),                                                                           -- URL
  foreign key(parent_node_id) references t_hibernate_sys_tree_node(tree_node_id)
);


-- drop table t_hibernate_sys_tree_node
-- select * from t_hibernate_sys_tree_node


select * from t_hibernate_sys_tree_node;
truncate table t_hibernate_sys_tree_node;


insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(1,'系统管理',1, 1,null,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(2,'市场管理',1, 2,null,null);

insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(3,'字典管理',2, 3,1,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(4,'用户管理',2, 4,1,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(5,'角色管理',2, 5,1,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(6,'权限管理',1, 6,1,null);

insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(7,'进货管理',2, 7,2,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(8,'销售管理',2, 8,2,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(9,'库存管理',2, 9,2,null);

insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(10,'用户分配角色',2, 10,6,null);
insert into t_hibernate_sys_tree_node(tree_node_id, tree_node_name, tree_node_type, position, parent_node_id, url)
  values(11,'角色授予用户',2, 11,6,null);
);

配置菜单表的映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="com.zking.entity.TreeNode" table="t_hibernate_sys_tree_node">
		<id name="treeNode_id" type="java.lang.Integer" column="tree_node_id">
			<generator class="increment" />
		</id>
		<property name="treeNode_name" type="java.lang.String" column="tree_node_name"></property>
		<property name="treeNode_type" type="java.lang.Integer" column="tree_node_type"></property>
		<property name="position" type="java.lang.Integer" column="position"></property>
		<property name="url" type="java.lang.String" column="url"></property>
		<many-to-one name="treeNode" class="com.zking.entity.TreeNode" column="parent_node_id"></many-to-one>
		<!-- <set name="treeNodes" cascade="save-update" inverse="true" >
			<key column="parent_node_id"></key>
			<one-to-many class="com.zking.entity.TreeNode"/>
		</set> -->
		<!-- 
			list 集合配置也有list标签,小编比较喜欢用当前这个
			order-by:填的表的列段,按照所填的列段排列
		 -->
		<bag order-by="position" name="treeNodes" cascade="save-update" inverse="true" >
			<key column="parent_node_id"></key>
			<one-to-many class="com.zking.entity.TreeNode"/>
		</bag>
	</class>
</hibernate-mapping>

菜单实体类

package com.zking.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class TreeNode implements Serializable {
	/**
	 * 2018年10月25日下午6:30:23
	 */
	private static final long serialVersionUID = -5329689980623631639L;
	private Integer treeNode_id;
	private String treeNode_name;
	private Integer treeNode_type;
	private Integer position;
	private String url;
	//父节点
	private TreeNode treeNode;
	//加载权限是无序的
//	private Set<TreeNode> treeNodes = new HashSet<>();
	//加载权限是有序的
	//子节点
	private List<TreeNode> treeNodes = new ArrayList<>();
	private Integer initTreeNode;

	@Override
	public String toString() {
		return "TreeNode [treeNode_id="+ treeNode_id +",treeNode_name="+ treeNode_name +",treeNode_type="+ treeNode_type +",position="+ position +",url="+ url +",treeNode="+ treeNode +",treeNodes="+ treeNodes +",initTreeNode="+ initTreeNode +"]";
	}

	public Integer getTreeNode_id() {
		return treeNode_id;
	}

	public void setTreeNode_id(Integer treeNode_id) {
		this.treeNode_id = treeNode_id;

	}

	public String getTreeNode_name() {
		return treeNode_name;
	}

	public void setTreeNode_name(String treeNode_name) {
		this.treeNode_name = treeNode_name;

	}

	public Integer getTreeNode_type() {
		return treeNode_type;
	}

	public void setTreeNode_type(Integer treeNode_type) {
		this.treeNode_type = treeNode_type;

	}

	public Integer getPosition() {
		return position;
	}

	public void setPosition(Integer position) {
		this.position = position;

	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;

	}

	public TreeNode getTreeNode() {
		return treeNode;
	}

	public void setTreeNode(TreeNode treeNode) {
		this.treeNode = treeNode;

	}

//	public Set<TreeNode> getTreeNodes() {
//		return treeNodes;
//	}
//
//	public void setTreeNodes(Set<TreeNode> treeNodes) {
//		this.treeNodes = treeNodes;
//
//	}

	public Integer getInitTreeNode() {
		return initTreeNode;
	}

	public List<TreeNode> getTreeNodes() {
		return treeNodes;
	}

	public void setTreeNodes(List<TreeNode> treeNodes) {
		this.treeNodes = treeNodes;
	}

	public void setInitTreeNode(Integer initTreeNode) {
		this.initTreeNode = initTreeNode;

	}

	public TreeNode() {
		super();
	}

	public TreeNode(Integer treeNode_id, String treeNode_name, Integer treeNode_type, Integer position, String url,
			TreeNode treeNode, List<TreeNode> treeNodes, Integer initTreeNode) {
		super();
		this.treeNode_id = treeNode_id;
		this.treeNode_name = treeNode_name;
		this.treeNode_type = treeNode_type;
		this.position = position;
		this.url = url;
		this.treeNode = treeNode;
		this.treeNodes = treeNodes;
		this.initTreeNode = initTreeNode;
	}

	
}

菜单表权限查询

package com.zking.dao.daoImpl;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zking.entity.TreeNode;
import com.zking.util.HibernateUtli;

public class TreeNodeDaoImpl implements ITreeNodeDaoImpl {
	@Override
	public TreeNode getTreeNode(TreeNode treeNode) {
		Session session = HibernateUtli.getCurrentSession();
		Transaction transaction = session.beginTransaction();
		TreeNode t = null;
		try {
			t = session.get(TreeNode.class, treeNode.getTreeNode_id());
			if (null != t && new Integer(1).equals(treeNode.getInitTreeNode())) {
				Hibernate.initialize(t.getTreeNodes());
			}
			transaction.commit();
		} catch (Exception e) {
			e.printStackTrace();
			transaction.rollback();
			throw new RuntimeException("查询出现异常");
		}
		HibernateUtli.closeSession();
		return t;
	}

测试

package com.zking.test;

import org.junit.Before;
import org.junit.Test;

import com.zking.dao.daoImpl.TreeNodeDaoImpl;
import com.zking.entity.TreeNode;

public class TreeNodeDaoTest {
	private TreeNodeDaoImpl daoImpl = null;
	private TreeNode node = null;
	
	@Before
	public void setUp() throws Exception {
		daoImpl = new TreeNodeDaoImpl();
		node = new TreeNode();
	}
	@Test
	public void testget() {
		this.node.setTreeNode_id(1);
		this.node.setInitTreeNode(1);
		TreeNode treeNode = this.daoImpl.getTreeNode(this.node);
		System.out.println(treeNode.getTreeNode_name());
		for (TreeNode s : treeNode.getTreeNodes()) {
			System.out.println(s.getTreeNode_id() + "," + s.getTreeNode_name());
		}
		/*
		 * 	第二种权限加载方案
		 *	类似于hibernate的懒加载
		 *	第一加载一级节点和二级节点
		 *	当点击二级节点再去向获取后台用于展示
		 */
		
	}

}

  • 1
    点赞
  • 0
    评论
  • 2
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页

打赏作者

f_1314520

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值