⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mnouncedao.java

📁 持久层hibernate技术使用的一个例子
💻 JAVA
字号:
package cn.hope.mana.pojo.dao;

import java.util.List;

import org.apache.log4j.Logger;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;
import cn.hope.mana.pojo.MNounce;
import cn.hope.mana.pojo.MNtype;
import cn.hope.mana.pojo.base.BaseMNounceDAO;


public class MNounceDAO extends BaseMNounceDAO {
	Logger log = Logger.getLogger(MNounceDAO.class.getName());

	/**
	 * Default constructor.  Can be used in place of getInstance()
	 */
	public MNounceDAO () {}

	/****************lijia**********************/
	private int count = 0;

	public int getCount() {
		return this.count;
	}
	
	/**
	 * 通过键查询方法
	 * @param nId
	 * @return MNounce
	 * @throws HibernateException
	 */
	public MNounce searchByKey(Integer nId) throws HibernateException {
		MNounce mNounce = new MNounce();
		try {
			initialize();
			mNounce = this.load(nId);
		}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return mNounce;
	}
	
	/**
	 * 通过值查询方法
	 * @param nId
	 * @return MNounce
	 * @throws HibernateException
	 */
	public int searchByName(MNounce mNounce) throws HibernateException {
		String sqlCnt = "select count(mNounce.NId) from MNounce mNounce where mNounce.flag='0' and mNounce.NTitle='"
			+mNounce.getNTitle()+"'";
		try {
			initialize();
			Query queryCnt = this.getSession().createQuery(sqlCnt);
			return ((Integer)queryCnt.uniqueResult()).intValue();
		}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	
	/**
	 * 通过值和键查询方法
	 * @param nTitle
	 * @param nId
	 * @return MNounce
	 * @throws HibernateException
	 */
	public int search(String nTitle,int nid) throws HibernateException {
		String sqlCnt = "select count(mNounce.NId) from MNounce mNounce where mNounce.flag='0' and mNounce.NTitle='"
			+nTitle+"' and mNounce.NId<>"+ nid;
		try {
			initialize();
			Query queryCnt = this.getSession().createQuery(sqlCnt);
			return ((Integer)queryCnt.uniqueResult()).intValue();
		}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	/**
	 * 删除数据方法
	 * @param  MNounce
	 * @return 
	 * @throws HibernateException
	 */
	public void delete(MNounce mNounce) throws HibernateException {
		try {
			initialize();
			this.update(mNounce);
		}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	
	/**
	 * 添加数据方法
	 * 
	 * @param  MHelp  
	 * @return 
	 * @throws HibernateException
	 */
	public void insert(MNounce mNounce) throws HibernateException {
		try {			
			initialize();
			this.save(mNounce);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}	
	}
	
	/**
	 * 查询数据方法
	 * 
	 * @param  Integer  
	 * @return MNounce
	 * @throws HibernateException
	 */
	public MNounce find(Integer nId) throws HibernateException{
		try {			
			initialize();
			return this.load(nId);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}	
	}
	
	public List search(MNtype mNtype) throws HibernateException {
		List list = null;
		String sqlStr = "select mNounce from MNounce mNounce where mNounce.flag='0' and mNounce.MNtype.typeId='"
			+mNtype.getTypeId()+"'";
		try {			
			initialize();
			list = this.getSession().find(sqlStr);			
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return list;
	}

	/**
	 * 
	 * @param mNounce
	 *            //存放查询条件
	 * @param start
	 *            //分页起始记录数
	 * @param range
	 *            //分页显示记录数
	 * @param isEq
	 *            //某条件是否完全匹配(可以不加)
	 * @return
	 * @throws HibernateException
	 */
	public List search(MNounce mNounce, int start, int range, boolean isEq)
			throws HibernateException {
		List list = null;
		StringBuffer sqlCnt = new StringBuffer();// 取总记录数
		StringBuffer sqlStr = new StringBuffer();// 长字符串的拼合一定要用StringBuffer.append()
		StringBuffer condition = new StringBuffer();// 查询条件
		sqlCnt.append("select count(mNounce.NId) from MNounce mNounce where mNounce.flag='0' ");
		sqlStr.append("select mNounce from MNounce mNounce where mNounce.flag='0' ");

		/* 通过tFile中存储的条件组装HQL语句 */
		if (mNounce.getStartTime() != null){
			condition.append("and mNounce.NTime>='"+mNounce.getStartTime()+"' ");
		}
		if (mNounce.getEndTime() != null){
			condition.append("and mNounce.NTime<='"+mNounce.getEndTime()+"' ");
		}
		if (mNounce.getNTitle()!=null && !mNounce.getNTitle().equals("")){
			if (isEq){
				condition.append("and mNounce.NTitle='"+mNounce.getNTitle()+"' ");
			}
			else{
				condition.append("and mNounce.NTitle like '%"+mNounce.getNTitle().trim()+"%' ");
			}
		}
		if (mNounce.getMNtype() != null){
			if (mNounce.getMNtype().getTypeId() != null && mNounce.getMNtype().getTypeId().intValue() != 0 ){
				condition.append("and mNounce.MNtype.typeId="+mNounce.getMNtype().getTypeId()+" ");
			}
		}
		try {
			initialize();
			Query queryCnt = this.getSession().createQuery(
					sqlCnt.toString() + condition);// 通过hibernate的Query方法查询数据库(自己看文档)
			Integer count = (Integer) queryCnt.uniqueResult();// 得到总记录数
			this.count = count.intValue();

			condition.append(" order by mNounce.NTime desc");// 加入排序条件
			Query query = this.getSession().createQuery(
					sqlStr.toString() + condition);
			query.setFirstResult(start);// 设置查询起始记录数
			query.setMaxResults(range);// 设置查询总记录数
			list = query.list();
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return list;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -