一、概述:

1、前提:系统中必须导入EasyUI文件,搭建了SSH框架

2、如图:

SSH 使用EasyUI实现ZTee树状结构菜单_entity

二、创建实体类Emenu

@Entity
@Table(name = "easyui_menu")
public class EMenu implements Serializable{

/**
*
*/
private static final long serialVersionUID = 1L;
private Long id;// 主键
private EMenu parent;
private String url;
private String name;
private String icon;
private Set<EMenu> children = new HashSet<EMenu>(0);
private String memo;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "parentId")
public EMenu getParent() {
return parent;
}

public void setParent(EMenu parent) {
this.parent = parent;
}

public String getUrl() {
return url;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@OneToMany(cascade = { CascadeType.ALL }, fetch = FetchType.EAGER, mappedBy = "parent")
@OrderBy("id")
public Set<EMenu> getChildren() {
return children;
}

public void setChildren(Set<EMenu> children) {
this.children = children;
}

public String getMemo() {
return memo;
}

public void setMemo(String memo) {
this.memo = memo;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

}

三、创建在前台要转换成Json数据对应的类JsonMenu

**
* 返回前台的json数据
* @author Administrator
*
*/
public class JsonMenu {

private Long id;// 主键
private Long pId;// 父主键
private String pname;//父名称
private String url;
private String name;
private String icon;
private String memo;
private String state;//open页,close显示文件夹
private Map<String, Object> attributes;

public Map<String, Object> getAttributes() {
return attributes;
}

public void setAttributes(Map<String, Object> attributes) {
this.attributes = attributes;
}

public String getState() {
return state;
}

public void setState(String state) {
this.state = state;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getUrl() {
return url;
}

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

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getMemo() {
return memo;
}

public void setMemo(String memo) {
this.memo = memo;
}

public String getIcon() {
return icon;
}

public void setIcon(String icon) {
this.icon = icon;
}

public Long getpId() {
return pId;
}

public void setpId(Long pId) {
this.pId = pId;
}

public String getPname() {
return pname;
}

public void setPname(String pname) {
this.pname = pname;
}



}

四、菜单页面:
注意必须多添加参数parentField : ‘pId’,

<%@ page language="java" pageEncoding="UTF-8"%>
<div class="easyui-panel" data-options="title:'功能导航',border:false,fit:true">
<div class="easyui-accordion" data-options="fit:true,border:false">
<div title="系统菜单" data-options="iconCls:'icon-save'">
<ul id="layout_west_tree" class="easyui-tree" data-options="
url : '${pageContext.request.contextPath}/menuAction!getAllTreeNode.action',
parentField : 'pId',//
lines : true,//显示+号
onClick : function(node) {
if (node.attributes.url) {
var url = '${pageContext.request.contextPath}' + node.attributes.url;
addTab({
title : node.text,
closable : true,
href : url
});
}
}
"></ul>
</div>
<div title="Title2" data-options="iconCls:'icon-reload'"></div>
</div>
</div>

五、引入扩展jslib.js

$.fn.tree.defaults.loadFilter = function(data, parent) {
var opt = $(this).data().tree.options;
var idFiled, textFiled, parentField;
if (opt.parentField) {
idFiled = opt.idFiled || 'id';
textFiled = opt.textFiled || 'text';
parentField = opt.parentField;
var i, l, treeData = [], tmpMap = [];
for (i = 0, l = data.length; i < l; i++) {
tmpMap[data[i][idFiled]] = data[i];
}
for (i = 0, l = data.length; i < l; i++) {
if (tmpMap[data[i][parentField]] && data[i][idFiled] != data[i][parentField]) {
if (!tmpMap[data[i][parentField]]['children'])
tmpMap[data[i][parentField]]['children'] = [];
data[i]['text'] = data[i][textFiled];
tmpMap[data[i][parentField]]['children'].push(data[i]);
} else {
data[i]['text'] = data[i][textFiled];
treeData.push(data[i]);
}
}
return treeData;
}
return data;
};

六、Service层代码

@Service
@Transactional
public class EMenuServiceImpl extends BaseDaoImpl<EMenu> implements EMenuService {

@Override
public List<JsonMenu> getTreeNode(Long id) {
List<JsonMenu> jsonMenuList = new ArrayList<JsonMenu>();
String hql = null;
Map<String, Object> params = new HashMap<String, Object>();
if (id == null || id.equals("")) {
// 查询所有根节点
hql = "FROM Emenu t WHERE t.tmenu IS NULL";
} else {
// 异步加载当前id下的子节点
hql = "FROM Emenu t WHERE t.tmenu.id = :id ";
params.put("id", id);
}
List<EMenu> list = getByMap(hql, params);
if (list != null && list.size() > 0) {
for (EMenu menu : list) {
JsonMenu m = new JsonMenu();
BeanUtils.copyProperties(menu, m);
Set<EMenu> set = menu.getChildren();
if (set != null && !set.isEmpty()) {
m.setState("closed");// 节点以文件夹的形式体现
} else {
m.setState("open");// 节点以文件的形式体现
}
jsonMenuList.add(m);
}
}
return jsonMenuList;
}

@Override
public List<JsonMenu> getAllTreeNode() {
List<JsonMenu> jsonMenuList = new ArrayList<JsonMenu>();
List<EMenu> eMenuList = findAll();
if (eMenuList != null && eMenuList.size() > 0) {
for (EMenu eMenu : eMenuList) {
JsonMenu jsonMenu = new JsonMenu();
BeanUtils.copyProperties(eMenu, jsonMenu);
Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("url", eMenu.getUrl());
jsonMenu.setAttributes(attributes);
EMenu tm = eMenu.getParent();// 获得当前节点的父节点对象
if (tm != null) {
jsonMenu.setpId(tm.getId());
}
jsonMenuList.add(jsonMenu);
}
}
return jsonMenuList;
}

}

七、Dap层数据处理:

@Transactional
@SuppressWarnings("unchecked")
public abstract class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
@Resource(name = "sessionFactory")
public void setSuperSessionFactory(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}

protected Class<T> clazz; // 这是一个问题!

public BaseDaoImpl() {
// 通过反射得到T的真实类型
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
}

public void save(T entity) {
getSession().save(entity);
}

public void update(T entity) {
getSession().update(entity);
}

public void delete(Long id) {
Object obj = getSession().get(clazz, id);
getSession().delete(obj);
}

public T getById(Long id) {
if (id == null) {
return null;
}
return (T) getSession().get(clazz, id);
}

public List<T> getByIds(Long[] ids) {
if (ids == null || ids.length == 0) {
return Collections.EMPTY_LIST;
}

return getSession().createQuery(//
"FROM " + clazz.getSimpleName() + " WHERE id IN(:ids)")//
.setParameterList("ids", ids)//
.list();
}

public List<T> getByParams(String hql, Object[] params) {
if (params == null || params.length == 0) {
return Collections.EMPTY_LIST;
}
Query query = getSession().createQuery(hql);
for (int i = 0; i < params.length; i++) {
query.setParameter(i, params[i]);
}
return query.list();
}

public List<T> getByMap(String hql, Map<String, Object> map){
if (map == null || map.isEmpty()) {
return Collections.EMPTY_LIST;
}
Query query = getSession().createQuery(hql);
for(String key : map.keySet()){
query.setParameter(key, map.get(key));
}
return query.list();
}

public List<T> getByMap(String hql, Map<String, Object> map, int page, int row){
if (map == null || map.isEmpty()) {
return Collections.EMPTY_LIST;
}
Query query = getSession().createQuery(hql);
for(String key : map.keySet()){
query.setParameter(key, map.get(key));
}
return query.setFirstResult((page-1)*row).setMaxResults(row).list();
}

public List<T> getByMap(String hql, int page, int row){
return getSession().createQuery(hql).setFirstResult((page-1)*row).setMaxResults(row).list();
}

public long count(String hql){
return (long) getSession().createQuery(hql).uniqueResult();
}
public long count(String hql , Map<String, Object> map){
if (map == null || map.isEmpty()) {
return 0;
}
Query query = getSession().createQuery(hql);
for(String key : map.keySet()){
query.setParameter(key, map.get(key));
}
return (long) query.uniqueResult();
}

public List<T> findAll() {
return getSession().createQuery(//
"FROM " + clazz.getSimpleName())//
.list();
}

public void saveOrUpdate(T entity){
getHibernateTemplate().saveOrUpdate(entity);
}
public void batchSaveOrUpdate(Collection<T> datas){
getHibernateTemplate().saveOrUpdateAll(datas);
}

}