一个Hibernate的开发指南
nbsp;if ( !(other instanceof SysUser) ) return false;
SysUser castOther = (SysUser) other;
return new EqualsBuilder()
.append(this.getUserid(), castOther.getUserid())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getUserid())
.toHashCode();
}
}
建立测试用的Servlet
创建Servlet框架
使用新建Servlet的向导我们来建立一个测试用的Servlet,下图是新建Servlet时输入的参数和选项:
在下一步的XML Wizard对话框中记得将 Display Name 和 Description 的“Test System Function Servlet”都清除,如下图所示:
这样就建立了一个初始的Servlet框架。
编写数据库连接获取类
我们编写一个单独的类,用于从SessionFactory中获取数据库连接session。这个类名为HibernateUtil,我们将其放于com.huangdong.demo.util包中,具体的代码如下:
package com.huangdong.demo.util;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.cfg.Configuration;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory =
new Configuration().configure().buildSessionFactory();
} catch (HibernateException ex) {
throw new RuntimeException(
"Exception building SessionFactory: " + ex.getMessage(),
ex);
}
}
public static final ThreadLocal session = new ThreadLocal();
public static Session currentSession() throws HibernateException {
Session s = (Session) session.get();
// Open a new Session, if this Thread has none yet
if (s == null) {
s = sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession() throws HibernateException {
Session s = (Session) session.get();
session.set(null);
if (s != null)
s.close();
}
}
我们可以通过currentSession方法来获取数据库连接,如果系统中有可用的session就会反回可用的,如果没有时才会创建一个新的session。当session操作完成后,需要操作者使用closeSession方法释放使用的session。
建立测试Bean
接下来我们建立一个TestHibernate的Java Bean,主要加入一些Hibernate的测试方法,以下是建立该类所输入的相关信息:
我们为该类加入一个向数据库中增加数据的方法:
public boolean TestAdd() {
try {
Session session = HibernateUtil.currentSession();
Transaction tx = session.beginTransaction();
SysUser user = new SysUser();
user.setUsername("丫丫");
user.setUserpasword("uhkuhkqepdwqi");
user.setLastlogin(Calendar.getInstance());
session.save(user);
tx.commit();
HibernateUtil.closeSession();
} catch (HibernateException e) {
e.printStackTrace();
return false;
}
return true;
}
第一次我写这个代码时感到非常的意外,总感觉应该再多写点什么,但是真的是这 样,我们只用了这么几句话就向数据库加入了一条记录。
在Servlet中调用TestAdd方法
这就很简单了,更改TestServlet.java类的doGet方法,初始化TestHibernate类,并调用TestAdd方法就可以了:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=GBK");
PrintWriter out = response.getWriter();
out.println(
"");
out.println("");
out.println(" ");
out.println(" ");
//调用Test Add
TestHibernate test = new TestHibernate();
out.println("TestAdd:" + test.TestAdd() + "
");
out.println(" ");
out.println("");
out.flush();
out.close();
}
如果TestAdd方法执行成功则会在页面上返回True,否则会返回False。下面我们就来测试一把。
测试我们第一个基于Servlet的Hibernate应用
启动Tomcat
如果之前按本文配置好了Tomcat的部署,哪么这里就不会有任何问题,如图操作则会进入Eclipse的调试视图:
在Debug视图中看到所有的线程启动,并在控制台窗口中看到“Starting Coyote HTTP/1.1 on port 8080”则说明Tomcat启动成功并侦听了8080端口,如下图:
打开浏览器,在地址栏中输入:
http://localhost:8080/Demo/servlet/TestServlet
如果返回的页面如下图所示,则代表数据库更新成功:
同时在Eclipse的控制台中我们可以看到Hibernate的log输出,说明了它工作的细节情况 :
2003-12-26 15:10:37 net.sf.hibernate.cfg.Environment
信息: Hibernate 2.1.1
2003-12-26 15:10:37 net.sf.hibernate.cfg.Environment
信息: hibernate.properties not found
2003-12-26 15:10:37 net.sf.hibernate.cfg.Environment
信息: using CGLIB reflection optimizer
2003-12-26 15:10:37 net.sf.hibernate.cfg.Configuration configure
信息: configuring from resource: /hibernate.cfg.xml
2003-12-26 15:10:37 net.sf.hibernate.cfg.Configuration getConfigurationInputStream
信息: Configuration resource: /hibernate.cfg.xml
2003-12-26 15:10:38 net.sf.hibernate.cfg.Configuration addResource
信息: Mapping resource: com/huangdong/demo/dao/SysUser.hbm.xml
2003-12-26 15:10:38 net.sf.hibernate.cfg.Binder bindRootClass
信息: Mapping class: com.huangdong.demo.dao.SysUser -> SYSUSER
2003-12-26 15:10:39 net.sf.hibernate.cfg.Configuration doConfigure
信息: Configured SessionFactory: null
2003-12-26 15:10:39 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing one-to-many association mappings
2003-12-26 15:10:39 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing one-to-one association property references
2003-12-26 15:10:39 net.sf.hibernate.cfg.Configuration secondPassCompile
信息: processing foreign key constraints
2003-12-26 15:10:39 net.sf.hibernate.dialect.Dialect
信息: Using dialect: net.sf.hibernate.dialect.Oracle9Dialect
2003-12-26 15:10:39 net.sf.hibernate.cfg.SettingsFactory buildSettings
信息: JDBC result set&nb
上一页 [1] [2] [3] [4] 下一页
进入问吧