您当前的位置: 首页 > 网站编程 > JSP教程 > 用Hibernate封装数据库操作的实例

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

作者:guanchaofeng 来源:不详 发布时间: 2009-03-06 21:49 点击:
这是我的实战JSP进阶教程之四。 本文,主要是想给Hibernate初学者,一点可以实际参考的例子。这段代码,就是本站bbs实际使用的代码片断,其实,也是我初学Hibernate的第一个习作,因此,比较幼稚,但也因此,可能更便于初学者理解。 代码比较简单,因此不作

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

这是我的实战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 {
HibernateUtil.closeSession();
}
return true;
}
}

分享到:
本文"用Hibernate封装数据库操作的实例"由远航站长收集整理而来,仅供大家学习与参考使用。更多网站制作教程尽在远航站长站。
顶一下
(0)
0%
踩一下
(0)
0%
[点击 次] [返回上一页] [打印]
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
关于本站 - 联系我们 - 网站声明 - 友情连接- 网站地图 - 站点地图 - 返回顶部
Copyright © 2007-2013 www.yhzhan.com(远航站长). All Rights Reserved .
远航站长:为中小站长提供最佳的学习与交流平台,提供网页制作与网站编程等各类网站制作教程.
官方QQ:445490277 网站群:26680406 网站备案号:豫ICP备07500620号-4