用Hibernate封装数据库操作的实例

2008-02-23 08:18:29来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

这是我的实战JSP进阶教程之四。

本文,主要是想给Hibernate初学者,一点可以实际参考的例子。这段代码,就是本站bbs实际使用的代码片断,其实,也是我初学Hibernate的第一个习作,因此,比较幼稚,但也因此,可能更便于初学者理解。

代码比较简单,因此不作过多的说明。其中的HibernateUtil,就是我上篇Jsp教学文章贴的。

package bbs;
import net.sf.hibernate.*;
import java.math.BigDecimal;
import java.util.*;
import org.apache.log4j.Logger;
public final class ThreadDAO {
private final static Logger logger = Logger.getLogger(ThreadDAO.class);
public static List getThreads(String fid, String pno) throws HibernateException {
long forumid;
forumid = new Long(fid).longValue();
if(forumid > 9) forumid = 3;
return getThreads0(forumid, pno);
}
public static List getThreads2(String fid, String pno) throws HibernateException {
long forumid;
forumid = new Long(fid).longValue();
// if(forumid > 10) forumid = 3;
return getThreads0(forumid, pno);
}
//用hibernate进行分页显示的例子。
public static List getThreads0(long forumid, String pno) throws HibernateException {
List list = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
int PAGE_SIZE = 20;
try {
tx = s.beginTransaction();
String q = "select t from bbs.Threads t where t.forumid=" forumid
" and t.modvalue>=5 order by t.modvalue desc, t.modificationdate desc ";
Query query = s.createQuery (q);
int pnum = new Integer(pno).intValue();
int start = (pnum-1) * PAGE_SIZE;
/* 设置返回记录的起始位置 */
query.setFirstResult(start);
/* 设置返回记录数量的最大值 */
query.setMaxResults(PAGE_SIZE);
list = query.list();
tx.commit(); } catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return list;
}
public static int getTotalThreads(String fid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
/* 使用hibernate 的session的load()方法, 根据主键获取实例
这是本程序反复使用的方法
*/
Forum forum = (Forum) s.load(Forum.class,new Long(fid));
total = forum.getThreads().intValue();
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return total;
}
public static Threads getThreads(String tid) throws HibernateException {
Threads t = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
t = (Threads) s.load(Threads.class,new Long(tid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return t;
}
public static String getUsername(String uid) throws HibernateException {
User u = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
u = (User) s.load(User.class,new Long(uid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return u.getUsername();
}
public static String getThreadSubject(String tid) throws HibernateException {
Threads t = null;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
t = (Threads) s.load(Threads.class,new Long(tid));
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return t.getSubject();
}

/* 更新数据库的update操作 */
// added on 2004-12-29
public static boolean hideThread(String tid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
Threads t = (Threads) s.get(Threads.class,new Long(tid));
if(t == null) return false;
BigDecimal mVal = t.getModvalue();
BigDecimal negOne = new BigDecimal("-1");
t.setModvalue(mVal.add(negOne));
s.saveOrUpdate(t);
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {
HibernateUtil.closeSession();
}
return true;
}
// added on 2004-12-29
public static boolean topThread(String tid) throws HibernateException {
int total = 0;
Session s = HibernateUtil.currentSession();
Transaction tx = null;
try {
tx = s.beginTransaction();
Threads t = (Threads) s.get(Threads.class,new Long(tid));
if(t == null) return false;
BigDecimal mVal = t.getModvalue();
// BigDecmal oneVal = new BigDecimal("1");
BigDecimal oneVal = new BigDecimal("1");
t.setModvalue(mVal.add(oneVal));
s.saveOrUpdate(t);
tx.commit();
} catch(HibernateException he) {
if(tx != null) tx.rollback();
throw he;
}
finally {

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:在Tomcat下配置Hibernate的开发环境

下一篇:详细讲解Hibernate的配置文件