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

📄 tquestiondao.java

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

import java.util.List;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;

import org.apache.log4j.Logger;

import cn.hope.mana.pojo.TQuestion;
import cn.hope.mana.pojo.base.BaseTQuestionDAO;


public class TQuestionDAO extends BaseTQuestionDAO {
	
	Logger log = Logger.getLogger(TQuestionDAO.class.getName());

	/**
	 * Default constructor.  Can be used in place of getInstance()
	 */
	public TQuestionDAO () {}
	
/*----------liul-------------------------------------------------------------------*/
	
	/**
	 * 查询指定的试题
	 */
	public TQuestion U05search(Integer questionID) throws HibernateException {
		TQuestion tQuestion = new TQuestion();
		List list = null;
		String sqlStr = "select tQuestion from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.tqId='"+questionID+"'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
			if(list.size()>0){
				tQuestion = (TQuestion)list.get(0);
			}
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return tQuestion;
	}
	
	
	/**
	 * 修改
	 */
	public void U05amend(Integer questionID,String questions,String answer) throws HibernateException {
		TQuestion tQuestion = new TQuestion();
		List list = null;
		String sqlStr = "select tQuestion from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.tqId='"+questionID+"'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
			if(list.size()>0){
				tQuestion = (TQuestion)list.get(0);
				tQuestion.setTqName(questions);
				tQuestion.setTqAnswer(answer);
				this.save(tQuestion);
			}
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	
	/**
	 * 查询所有的试题
	 */
	public List U05searchTQuestion() throws HibernateException {
		List list = null;
		String sqlStr = "select tQuestion from TQuestion tQuestion where tQuestion.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 sSid
	 * @param tqtId
	 *            //存放查询条件
	 * @param start
	 *            //分页起始记录数
	 * @param range
	 *            //分页显示记录数
	 * @param isEq
	 *            //某条件是否完全匹配(可以不加)
	 * @return
	 * @throws HibernateException
	 */
	private int count = 0;

	public int getCount() {
		return this.count;
	}
	
	public List U05search(Integer sSid,Integer tqtId, 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(tQuestion.tqId) from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.CSubject.SSid='"+sSid+"' and tQuestion.TQtype.tqtId='"+tqtId+"'");
		sqlStr.append("select tQuestion from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.CSubject.SSid='"+sSid+"' and tQuestion.TQtype.tqtId='"+tqtId+"'");
		
		/* 通过tFile中存储的条件组装HQL语句 */
		
		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 tQuestion.tqId");// 加入排序条件
			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;
	}
	
	
	
	public List U05search(Integer SSid, 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(tQuestion.tqId) from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.CSubject.SSid='"+SSid+"'");
		sqlStr.append("select tQuestion from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.CSubject.SSid='"+SSid+"'");
		
		/* 通过tFile中存储的条件组装HQL语句 */
		
		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 tQuestion.TQtype.tqtId");// 加入排序条件
			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;
	}
	
	/**
	 * 删除
	 */
	public void U05del(String[] a) throws HibernateException {
		TQuestion tQuestion= null;
		if(a!=null && a.length>0){
			for(int j=0;j<a.length;j++){
				Integer i = new Integer(a[j]); 
				String sqlStr = "select tQuestion from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.tqId='"+i+"'";
				try {
					initialize();
					List list = this.getSession().find(sqlStr);
					if (list.size() > 0) {
						tQuestion = (TQuestion) list.get(0);
						tQuestion.setFlag("1");
						this.save(tQuestion);
					}
				} catch (HibernateException e) {
					log.error(e);
					e.printStackTrace();
					throw new HibernateException(e);
				} finally {
					closeCurrentThreadSessions();
				}			
			}
		}
	}
	
	public void U05del(Integer questionID) throws HibernateException {
		TQuestion tQuestion= null;
		List list = null;
		String sqlStr = "select tQuestion from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.tqId='"+questionID+"'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
			if (list.size() > 0) {
				tQuestion = (TQuestion) list.get(0);
				tQuestion.setFlag("1");
				this.save(tQuestion);
			}
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
				
	}
	
	/**
	 * 查询某学科的试题
	 */
	public List U05searchCSubjectQuestions(Integer id) throws HibernateException {
		List list = null;
		String sqlStr = "select tQuestion from TQuestion tQuestion where tQuestion.flag='0' and tQuestion.CSubject.SSid='"+id+"'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return list;
	}

	
	/**
	 * 插入数据方法
	 * 
	 * @param tQuestion
	 * @return
	 * @throws HibernateException
	 */
	public Integer U05insert(TQuestion tQuestion) throws HibernateException {
		try {
			initialize();// 初始化session
			return this.save(tQuestion);// 调用生成好的父类方法save(),返回被插入数据的主键
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();// 关闭当前session
		}
	}
	
}

⌨️ 快捷键说明

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