responsedao.java

来自「bbs论坛 采用java的Web开发strtus、hibernate」· Java 代码 · 共 62 行

JAVA
62
字号
package com.wish.bbs.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

import com.wish.bbs.factory.HibernateSessionFactory;
import com.wish.bbs.pojo.Response;


public class ResponseDAO {
	Session session=null;
	public void getSession(){
		this.session=new Configuration().configure().buildSessionFactory().openSession();
	}
	
	public void save(Response response){
		this.session=HibernateSessionFactory.getSession();
		this.session.save(response);
		this.session.beginTransaction().commit();
		HibernateSessionFactory.closeSession();
	}
	
	public List queryById(String id,int start) {
		this.session=HibernateSessionFactory.getSession();
		String hql = "FROM Response AS r WHERE r.topic.tid=?";
		Query q = this.session.createQuery(hql);
		q.setString(0, id);
		q.setFirstResult(start);
		q.setMaxResults(5);
		List all = q.list();
		HibernateSessionFactory.closeSession();
		return all;
	}
	
	public List queryResponseById(String id) {
		this.session=HibernateSessionFactory.getSession();
		String hql = "FROM Response AS r WHERE r.rid=?";
		Query q = this.session.createQuery(hql);
		q.setString(0, id);
		List all = q.list();
		HibernateSessionFactory.closeSession();
		return all;
	}
	
	public int getCountItems(String tid){
		int count=0;
		this.session=HibernateSessionFactory.getSession();
		String hql="select count(*) from Response as r where r.topic.tid=?";
		Query q=this.session.createQuery(hql);
		q.setString(0, tid);
		List l=q.list();
		if(l.size()>0){
			count=(Integer)l.get(0);
		}
		HibernateSessionFactory.closeSession();
		return count;
	}
}

⌨️ 快捷键说明

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