实体类:
1、TreeNode.class
package com.five.entity;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TreeNode {
/*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)
);*/
private Integer treeNodeId;
private String treeNodeName;
private Integer treeNodeType;
private Integer position;
private String url;
//当前节点与子节点的关联关系 一对多
// private Set children = new HashSet<>();
private List children = new ArrayList<>();
//当前节点与父节点 多对一
private TreeNode parent;
private Integer initChildren = 0;
/* public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}*/
public TreeNode getParent() {
return parent;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public void setParent(TreeNode parent) {
this.parent = parent;
}
public Integer getInitChildren() {
return initChildren;
}
public void setInitChildren(Integer initChildren) {
this.initChildren = initChildren;
}
public Integer getTreeNodeId() {
return treeNodeId;
}
public void setTreeNodeId(Integer treeNodeId) {
this.treeNodeId = treeNodeId;
}
public String getTreeNodeName() {
return treeNodeName;
}
public void setTreeNodeName(String treeNodeName) {
this.treeNodeName = treeNodeName;
}
public Integer getTreeNodeType() {
return treeNodeType;
}
public void setTreeNodeType(Integer treeNodeType) {
this.treeNodeType = treeNodeType;
}
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;
}
@Override
public String toString() {
return "TreeNode [treeNodeId=" + treeNodeId + ", treeNodeName=" + treeNodeName + ", treeNodeType="
+ treeNodeType + ", position=" + position + ", url=" + url + ", children=" + children + "]";
}
}
2、Book.class
package com.five.entity;
import java.util.HashSet;
import java.util.Set;
public class Book {
/*create table t_hibernate_book
(
book_id int primary key auto_increment,
book_name varchar(50) not null,
price float not null
);*/
private Integer bookId;
private String bookName;
private Float price;
private Set categories = new HashSet<>();
private Integer initCategories = 0;
public Integer getInitCategories() {
return initCategories;
}
public void setInitCategories(Integer initCategories) {
this.initCategories = initCategories;
}
public Set getCategories() {
return categories;
}
public void setCategories(Set categories) {
this.categories = categories;
}
public Integer getBookId() {
return bookId;
}
public void setBookId(Integer bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
}
3、Category.class
package com.five.entity;
import java.util.HashSet;
import java.util.Set;
public class Category {
/*create table t_hibernate_category
(
category_id int primary key auto_increment,
category_name varchar(50) not null
);*/
private Integer categoryId;
private String categoryName;
private Set books = new HashSet<>();
private Integer initBooks = 0;
public Integer getInitBooks() {
return initBooks;
}
public void setInitBooks(Integer initBooks) {
this.initBooks = initBooks;
}
public Set getBooks() {
return books;
}
public void setBooks(Set books) {
this.books = books;
}
public Integer getCategoryId() {
return categoryId;
}
public void setCategoryId(Integer categoryId) {
this.categoryId = categoryId;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
}
xml映射文件:
1、TreeNode.hbm.xml
2、Book.bhm.xml
3、Category.hbm.xml:
Dao类:
1、TreeNodeDao.class
package com.five.dao;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.five.entity.TreeNode;
import com.two.util.SessionFactoryUtil;
public class TreeNodeDao {
public TreeNode get(TreeNode treeNode) {
Session session = SessionFactoryUtil.getSession();
Transaction transaction = session.beginTransaction();
TreeNode tn = session.get(TreeNode.class, treeNode.getTreeNodeId());
if(tn != null && new Integer(1).equals(treeNode.getInitChildren())) {
Hibernate.initialize(tn.getChildren());
}
transaction.commit();
session.close();
return tn;
}
}
2、BookDao.class
package com.five.dao;
import java.io.Serializable;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.five.entity.Book;
import com.two.util.SessionFactoryUtil;
public class BookDao {
public Book get(Book book) {
Session session = SessionFactoryUtil.getSession();
Transaction transaction = session.beginTransaction();
Book b = session.get(Book.class, book.getBookId());
if(b != null && new Integer(1).equals(book.getInitCategories())) {
Hibernate.initialize(b.getCategories());
}
transaction.commit();
session.close();
return b;
}
3、CategoryDao.class
package com.five.dao;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.five.entity.Book;
import com.five.entity.Category;
import com.two.util.SessionFactoryUtil;
public class CategoryDao {
public Category get(Category category) {
Session session = SessionFactoryUtil.getSession();
Transaction transaction = session.beginTransaction();
Category c = session.get(Category.class, category.getCategoryId());
if(c != null && new Integer(1).equals(category.getInitBooks())) {
Hibernate.initialize(c.getBooks());
}
transaction.commit();
session.close();
return c;
}
}
JUnit测试类:
1、TreeNodeDaoTest.class
package com.five.dao;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.five.entity.TreeNode;
public class TreeNodeDaoTest {
private TreeNodeDao treeNodeDao = new TreeNodeDao();
@Test
public void testGet() {
/**
*自关联查询
*/
TreeNode treeNode = new TreeNode();
treeNode.setTreeNodeId(1);
treeNode.setInitChildren(1);
TreeNode tn = this.treeNodeDao.get(treeNode);
System.out.println(tn.getTreeNodeId()+","+tn.getTreeNodeName());
for (TreeNode tn2 : tn.getChildren()) {
System.out.println(tn2.getTreeNodeId()+","+tn2.getTreeNodeName());
}
}
}
2、BookDaoTest.class
package com.five.dao;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.five.entity.Book;
import com.five.entity.Category;
public class BookDaoTest {
private BookDao bookDao = new BookDao();
private CategoryDao categoryDao = new CategoryDao();
/**
* 通过一本书能够查询到多个类别
* jdbc:三表联查
* hibernate:只需要查询单个对象即可,它会自动关联查询,交给映射文件即可
*/
@Test
public void testGet1() {
Book book = new Book();
book.setBookId(5);
book.setInitCategories(1);
Book b = this.bookDao.get(book);
System.out.println(b.getBookName());
for (Category c : b.getCategories()) {
System.out.println(c.getCategoryName());
}
}
/**
* 通过一个类别查询多个书籍
*/
@Test
public void testGet2() {
Category category = new Category();
category.setCategoryId(2);
category.setInitBooks(1);
Category c = this.categoryDao.get(category);
System.out.println(c.getCategoryName());
for (Book b : c.getBooks()) {
System.out.println(b.getBookName());
}
}
}
级联查询总结:
当加载一级节点的时候没问题
加载二级的时候,由于设置了强韧加载,同样可以加载所有的二级节点。没问题
加载三级节点,这时session关闭了,并且默认查出来的节点,是默认采用的是懒加载权限菜单加载有两种方式
1、一次性将数据库表中的数据全部加载往浏览器返回(适用于菜单较少)
2、菜单表数据量较大,点出息浏览器卡顿的情况,第一种方式就不再使用了。
那么咱们就采用菜单逐级加载