Dao层接口:
/**
* @Title: IVoteTreeDao.java
* @Package org.dao
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com
* @date 2017-5-6 下午10:38:47
* @version V1.0
*/
package org.dao;import java.util.List;import org.entity.VoteTree;/** * * 项目名称:testTree * 类名称:IVoteTreeDao * 类描述: * 创建人:Mu Xiongxiong * 创建时间:2017-5-6 下午10:38:47 * 修改人:Mu Xiongxiong * 修改时间:2017-5-6 下午10:38:47 * 修改备注: * @version * */
public interface IVoteTreeDao {//查询最小的levelLong minId();//根据最小的查询所有一级菜单List<VoteTree> getFirstLevel(Long minLevel);//根据pid查询所有相对应的子集List<VoteTree> getNextSubSet(VoteTree tvote);//递归查询List<VoteTree> getDeeptLevel(VoteTree tvote);}
HibernateBaseDao实现:
/**
* @Title: BaseHibernateDao.java
* @Package org.dao
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com
* @date 2017-5-6 下午1:50:18
* @version V1.0
*/
package org.dao;import org.hibernate.Session;
import org.util.HibernateUtil;/** * * 项目名称:votedemo * 类名称:BaseHibernateDao * 类描述: hibernate的工具类BaseDao* 创建人:Mu Xiongxiong * 创建时间:2017-5-6 下午1:50:18 * 修改人:Mu Xiongxiong * 修改时间:2017-5-6 下午1:50:18 * 修改备注: * @version * */
public class BaseHibernateDao {/*** * @Title: getSession* @Description: 该方法的主要作用:打开session * @param @return 设定文件 * @return 返回类型:Session * @throws*/public Session getSession(){return HibernateUtil.getSession();}/*** * @Title: closeSession* @Description: 该方法的主要作用:关闭session * @param 设定文件 * @return 返回类型:void * @throws*/public void closeSession (){HibernateUtil.closeSession();}}
Dao层实现类:
/**
* @Title: IVoteTreeDaoImpl.java
* @Package org.dao.impl
* @Description: TODO该方法的主要作用:
* @author A18ccms A18ccms_gmail_com
* @date 2017-5-6 下午10:40:25
* @version V1.0
*/
package org.dao.impl;import java.util.ArrayList;
import java.util.List;import javax.persistence.criteria.From;import org.dao.BaseHibernateDao;
import org.dao.IVoteTreeDao;
import org.entity.VoteTree;/** * * 项目名称:testTree * 类名称:IVoteTreeDaoImpl * 类描述: * 创建人:Mu Xiongxiong * 创建时间:2017-5-6 下午10:40:25 * 修改人:Mu Xiongxiong * 修改时间:2017-5-6 下午10:40:25 * 修改备注: * @version * */
public class IVoteTreeDaoImpl extends BaseHibernateDao implements IVoteTreeDao {////查询最小的数@Overridepublic Long minId() {String hql = "select min(levels) from VoteTree";Long minCount = (Long) getSession().createQuery(hql).uniqueResult(); System.out.println("最小的数是:"+minCount);return minCount;}//查询一级菜单@Overridepublic List<VoteTree> getFirstLevel(Long minLevel) {String hql="from VoteTree where levels=?";List<VoteTree> listFirstLevel = getSession().createQuery(hql).setParameter(0, minLevel).list();return listFirstLevel;}//根据一级id查询所有的子集@Overridepublic List<VoteTree> getNextSubSet(VoteTree tvote) {String hql = "from VoteTree where pid = ?";List<VoteTree> tNextLevel = getSession().createQuery(hql).setParameter(0, tvote.getId()).list();//遍历这个二级目录的集合for (VoteTree voteTree : tNextLevel) {List<VoteTree> ts = getDeeptLevel(voteTree);//将下面的子集都依次递归进来voteTree.setChildren(ts);}return tNextLevel;}//递归查询@Override public List<VoteTree> getDeeptLevel(VoteTree tvote) {String hql="from VoteTree where pid = ?";List<VoteTree> tsLevel = getSession().createQuery(hql).setParameter(0, tvote.getId()).list();if(tsLevel.size()>0){for (int i = 0; i <tsLevel.size(); i++) {getDeeptLevel(tsLevel.get(i));}}return tsLevel;}}