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

📄 mhtypedao.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.MHtype;
import cn.hope.mana.pojo.base.BaseMHtypeDAO;


public class MHtypeDAO extends BaseMHtypeDAO {
	Logger log = Logger.getLogger(MHtypeDAO.class.getName());

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

	/********************lijia***********************/
	/**
	 * 查询数据方法
	 * 
	 * @param 
	 * @return List
	 * @throws HibernateException
	 */
	public List searchMHtype() throws HibernateException {
		List list = null;//结果
		String sqlStr = "select mHtype from MHtype mHtype where mHtype.flag='0'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return list;
	}
	
	/**
	 * 查询数据方法
	 * 
	 * @param 
	 * @return MHtype
	 * @throws HibernateException
	 */
	public MHtype load(Integer htId) throws HibernateException {
		
		try {
			initialize();
			return super.load(htId);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	
	/**
	 * 添加数据方法
	 * 
	 * @param  MHtype
	 * @return int
	 * @throws HibernateException
	 */
	public Integer insert(MHtype mHtype) throws HibernateException {
		
		try {
			initialize();
			if (searchByName(mHtype) == null){
				this.save(mHtype);
			}
			return searchByName(mHtype);
		}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	public Integer searchByName(MHtype mHtype) throws HibernateException {
		String sqlKey = "select mHtype.htId from MHtype mHtype where mHtype.flag='0' and mHtype.htName='"
			+mHtype.getHtName()+"'";
		try {
			initialize();			
			Query queryCnt = this.getSession().createQuery(
					sqlKey.toString());
			Integer i = (Integer)queryCnt.uniqueResult();			
			return i;
		}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}	
	public int searchByName(String htName) throws HibernateException {
		String sqlCnt = "select count(mHtype.htId) from MHtype mHtype where mHtype.flag='0' and mHtype.htName='"
			+htName+"'";
		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();
		}
	}	
	public MHtype searchByKey(Integer htId) throws HibernateException {
		MHtype mHtype = new MHtype();
		try {
			initialize();			
			mHtype = this.load(htId);
			return mHtype;
		}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	public void update(MHtype mHtype)  throws HibernateException {
		try {
			initialize();			
			super.update(mHtype);
		}catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
}

⌨️ 快捷键说明

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