tomcat5 hibernate3 mysql5 jsp的实现
昨天在独立应用程序中使用了hibernate,今天配置了hibernate在web应用中.
--------------
hibernate.cfg.xml 注意点,主要是要加包目录:
<!-- mapping files -->
<mapping resource="po/user.hbm.xml"/>
----------------
user.hbm.xml 注意点,主要是要加包目录:
<hibernate-mapping>
<class name="po.user" table="user">
--------直接在jsp中是用hibernate API,注意import 包中的对象-----------------------
<%@ page import="java.util.*" %>
<%@ page import="org.hibernate.*" %>
<%@ page import="org.hibernate.cfg.*" %>
<%@ page import="po.*" %>
<%@ page contentType="text/html;charset=GB2312" %>
<html>
<head></head>
<body>
<%
try
{
//通过Configuration获得一个SessionFactory对象
SessionFactory sf = new Configuration().configure().buildSessionFactory();
//打开一个Session
Session mysession = sf.openSession();
//开始一个事务
Transaction tx = mysession.beginTransaction();
//创建一个User对象
user stu = new user();
//通过User的setter方法改变它的属性
stu.setUser_name("kkk6545");
stu.setUser_pass("6330");
//通过session的save()方法将User对象保存到数据库中
mysession.save(stu);
//提交事务
tx.commit();
//关闭会话
mysession.close();
}
catch(Exception e)
{
e.printStackTrace();
}
%>
</body>
</html>
本文介绍了如何在Tomcat环境下使用Hibernate 3和MySQL 5进行集成开发,具体包括配置hibernate.cfg.xml和user.hbm.xml文件,以及在JSP页面中调用Hibernate API实现用户数据的保存。

1万+

被折叠的 条评论
为什么被折叠?



