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

📄 ttestinfodao.java

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

import java.util.List;
import org.apache.log4j.Logger;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Query;







import cn.hope.front.pojo.TTestInfo;
import cn.hope.front.pojo.base.BaseTTestInfoDAO;


public class TTestInfoDAO extends BaseTTestInfoDAO {
	Logger log = Logger.getLogger(TTestInfoDAO.class.getName());
	/**
	 * Default constructor.  Can be used in place of getInstance()
	 */
	public TTestInfoDAO () {}
	public List searchByKey(Integer id) throws HibernateException {
		List list = null;
		String sqlStr = "select tTestInfo from TTestInfo tTestInfo where tTestInfo.flag='0' and tTestInfo.CSubject='"
				+ id + "'";
		try {
			initialize();
			list = this.getSession().find(sqlStr);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		return list;
	}
	public List searchAll() throws HibernateException {
		List list = null;
		String sqlStr = "select tTestInfo from TTestInfo tTestInfo where tTestInfo.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;
	}
	private int count = 0;
	
	public int getCount(){
		return this.count;
	}

	public List searchAround(int start, int range, boolean isEq)throws HibernateException{
	
		String sqlStr = "select tTestInfo from TTestInfo tTestInfo where tTestInfo.flag='0' ";
		List list = null;
		StringBuffer sqlCnt = new StringBuffer();// 取总记录数
		sqlCnt.append("select count(tTestInfo.TTestid) from TTestInfo tTestInfo where tTestInfo.flag='0' ");//计算结果总和
		StringBuffer condition = new StringBuffer();// 查询条件
		
		try {
			initialize();
			
			
			//分页
			Query queryCnt = this.getSession().createQuery(
					sqlCnt.toString() + condition);// 通过hibernate的Query方法查询数据库(自己看文档)
			Integer count = (Integer) queryCnt.uniqueResult();// 得到总记录数
			this.count = count.intValue();
			Query query = this.getSession().createQuery(sqlStr.toString());
			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 Integer insert(TTestInfo tTestInfo) throws HibernateException {
		try {
			initialize();// 初始化session
			return this.save(tTestInfo);// 调用生成好的父类方法save(),返回被插入数据的主键
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();// 关闭当前session
		}
	}

	
	public TTestInfo testInfo(String id) throws HibernateException {
		List list = null;
		//Student student=null;
		String sqlStr = "select testInfo from TTestInfo testInfo where testInfo.flag='0' and testInfo.TTestid='"+id+"'";
		TTestInfo tTestInfo = new TTestInfo();
		try {
			initialize();
			list = this.getSession().find(sqlStr);
			if (list.size() > 0) {
				tTestInfo = (TTestInfo) list.get(0);
				
			}
			//查寻名字				
					return tTestInfo;
		} 
		catch (HibernateException e) {
		log.error(e);
		e.printStackTrace();
		throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	
	public void modify(TTestInfo tTestInfo) throws HibernateException {
		try {
			initialize();
			this.update(tTestInfo);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
	}
	
	

	
	//===================zhang hong tao ==============================
private int zht = 0;
	
	public int getCountzht(){
		return this.zht;
	}
	public List searchAroundAll(int start, int range, boolean isEq)throws HibernateException{
		
		String sqlStr = "select tTestInfo from TTestInfo tTestInfo where tTestInfo.flag='0' ";
		List list = null;
		StringBuffer sqlCnt = new StringBuffer();// 取总记录数
		sqlCnt.append("select count(*) from TTestInfo tTestInfo where tTestInfo.flag='0' ");//计算结果总和
		StringBuffer condition = new StringBuffer();// 查询条件
		
		try {
			initialize();
		
			//分页
			Query queryCnt = this.getSession().createQuery(
					sqlCnt.toString() + condition);// 通过hibernate的Query方法查询数据库(自己看文档)
			Integer zht = (Integer) queryCnt.uniqueResult();// 得到总记录数
			this.zht = zht.intValue();
			System.out.println(zht);
			Query query = this.getSession().createQuery(sqlStr.toString());
			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 + -