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

📄 sreblogdao.java

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

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

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



import cn.hope.front.pojo.SReblog;
import cn.hope.front.pojo.base.BaseSReblogDAO;


public class SReblogDAO extends BaseSReblogDAO {
	Logger log = Logger.getLogger(SReblogDAO.class.getName());
	private int count = 0;

	public int getCount() {
		return this.count;
	}
	public SReblogDAO () {}
	public static void main(String args[]) throws Exception{
		List list = null;
		int sbId=1;
		int start =0;
		int range = 10;
		SReblogDAO sr=new SReblogDAO();
		list = sr.searchReblog(sbId,start,range);
		System.out.println(sr);
		Iterator it = list.iterator();
		while (it.hasNext()) {
			SReblog s = (SReblog) it.next();
			System.out.println(s.getReCon());
		}
	}
	public List searchReblog(int sbId,int start, int range ) throws HibernateException {
		List list = null;
		StringBuffer sqlCnt = new StringBuffer();// 取总记录数
		StringBuffer sqlStr = new StringBuffer();// 长字符串的拼合一定要用StringBuffer.append()
		StringBuffer condition = new StringBuffer();// 查询条件
		sqlCnt.append("select count(sreblog.reId) from SReblog sreblog where sreblog.flag='0' ");
		sqlStr.append("select sreblog from SReblog sreblog where sreblog.flag='0'and sreblog.SBlog.sbId='"+sbId+"'");
		
		
			
		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() + 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 Integer insert(SReblog sreblog) throws HibernateException {
		try {
			initialize();// 初始化session
			return this.save(sreblog);// 调用生成好的父类方法save(),返回被插入数据的主键
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();// 关闭当前session
		}
	}

	public List search(SReblog sreblog, 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(sreblog.reId) from SReblog sreblog where sreblog.flag='0' ");
		sqlStr.append("select sreblog from SReblog sreblog where sreblog.flag='0' ");
		
		/* 通过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 sreblog.reTime 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;
		}
	public void update(SReblog sreblog)throws HibernateException{
		try {
			initialize();
			
			super.update(sreblog);
		} catch (HibernateException e) {
			log.error(e);
			e.printStackTrace();
			throw new HibernateException(e);
		} finally {
			closeCurrentThreadSessions();
		}
		
	}

}

⌨️ 快捷键说明

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