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

📄 topicdao.java

📁 Italk吧简易论坛 基于struts+extjs+hibernate+spring+mysql开发。 /catch目录为系统截图 安装步骤: 1:安装MYSQL
💻 JAVA
字号:
/**
 * 
 */
package com.italk.dao.hibernate;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.italk.dao.ITopicDAO;
import com.italk.vo.BaseEntity;
import com.italk.vo.Topic;

/**
 * @author Administrator
 * 
 */
public class TopicDAO extends BaseHibernateDAO implements ITopicDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.italk.dao.IBaseDAO#getById(java.lang.Long)
	 */
	public BaseEntity getById(Long id) {
		// TODO Auto-generated method stub
		log.debug("start get Topic by id:"+id);
		Topic t=(Topic) this.getHibernateTemplate().get(Topic.class, id);
		log.debug("end get Topic by id:"+id);
		return t;
	}
	
	private List getByPage(final String sql, final int firstRow,
			final int maxRow) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session) throws SQLException,
					HibernateException {
				Query q = session.createQuery(sql);
				q.setFirstResult(firstRow);
				q.setMaxResults(maxRow);
				return q.list();
			}
		});
	}
	
	private  Integer getCount(final String sql) {
		// TODO Auto-generated method stub
		
		return (Integer)this.getHibernateTemplate().execute(new HibernateCallback(){
			public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Connection conn=arg0.connection();
				Statement state=conn.createStatement();
				ResultSet rs=state.executeQuery(sql);
				rs.next();
				int count=rs.getInt(1);
				return new Integer(count);
			}
		});
	}
	
	public List getMainTopic(final int first,final int max) {
		log.debug("start get main topic by first:"+first+"  max:"+max);
		final String sql = "from Topic as t where t.reply = false order by t.top desc,t.lastUpdateTime desc,t.publishTime desc";
		List list=getByPage(sql, first, max);
		log.debug("end get main topic by first:"+first+"  max:"+max);
		return list;
	}
	
	public int getMainTopicCount() {
		log.debug("start get main count");
		int count=getCount("select count(*) from Topic where reply = 0").intValue();
		log.debug("end get main count");
		return count;
	}
	
	public List getPrimeTopic(final int first,final int max) {
		log.debug("start get prime topic by first:"+first+"  max:"+max);
		final String sql = "from Topic as t where t.reply = false and t.prime=true order by t.top desc,t.prime desc,t.publishTime desc";
		List list=getByPage(sql, first, max);
		log.debug("end get prime topic by first:"+first+"  max:"+max);
		return list;
	}
	
	public int getPrimeTopicCount() {
		log.debug("start get prime count");
		int count=getCount("select count(*) from Topic where reply = 0 and prime = 1").intValue();
		log.debug("end get prime count");
		return count;
	}
	
	public List getTopTopic(final int first,final int max) {
		log.debug("start get top topic by first:"+first+"  max:"+max);
		final String sql = "from Topic as t where t.reply = false and t.top=true order by t.top desc,t.prime desc,t.publishTime desc";
		List list=getByPage(sql, first, max);
		log.debug("start get top topic by first:"+first+"  max:"+max);
		return list;
	}
	
	public int getTopTopicCount() {
		log.debug("start get top count");
		int count=getCount("select count(*) from Topic where reply = 0 and onTop = 1").intValue();
		log.debug("end get top count");
		return count;
	}
}

⌨️ 快捷键说明

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