您当前的位置: 首页 > 网站编程 > JSP教程 > 在Tomcat下配置Hibernate的开发环境

在Tomcat下配置Hibernate的开发环境

作者:guanchaofeng 来源:不详 发布时间: 2009-03-06 21:49 点击:
这是实战JSP进阶编程之三。 今天花了几个小时,终于将机房里面的Tomcat+Hibernate的开发、学习环境配置好了。 应用场景:Tomcat 5.5, Hibernate 2.1.7, Mysql 3.23.43, Mysql Driver:3.0.14, JDK: 1.4.2 OS: TurboLinux Server 8.0 用户环境:普通学生帐户-

在Tomcat下配置Hibernate的开发环境

这是实战JSP进阶编程之三。
今天花了几个小时,终于将机房里面的Tomcat+Hibernate的开发、学习环境配置好了。
应用场景:Tomcat 5.5, Hibernate 2.1.7, Mysql 3.23.43, Mysql Driver:3.0.14, JDK: 1.4.2 OS: TurboLinux Server 8.0
用户环境:普通学生帐户--j2ee,位置: /home/j2ee/public_html

为了方便初学者,本教程特意作了简化处理。
1。将hibernate2.jar,dom4j,ehcache,cglib等Hibernate手册上说的那些jar copy 到 WEB-INF/lib里面。

2。将hibernate.cfg.xml copy 到WEB-INF里面。
内网用户只要直接从/home/j2ee/public_html/WEB-INF中copy就可以了。
外面的读者也只需要在hibernate.cfg.xml中改改MySQL的参数就可以了。
3. 本例对通行的Cat例子,进行了进一步的简化,只有2个字段:
CAT_ID varchar(50), name varchar(50)。
4. WEB-INF/hb中有如下文件:
Cat.java
Cat.hbm.xml
HibernateUtil.java
5. 2个用于测试的jsp文件:
addCat.jsp
getCats.jsp

具体文件如下:
第一个文件:hibernate.cfg.xml

<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE hibernate-configuration (View Source for full doctype...)>
- <hibernate-configuration>
- <session-factory>
<property name="hibernate.connection.driver_class"]com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url"]jdbc:mysql://localhost/joke</property>
<property name="hibernate.connection.username"]root</property>
<property name="hibernate.connection.password"]
<property name="show_sql"]false</property>
<property name="dialect"]net.sf.hibernate.dialect.MySQLDialect</property>
<mapping resource="hb/Cat.hbm.xml"]
</session-factory>
</hibernate-configuration>

第二个文件:Cat.java

package hb;
public class Cat {
private String id;
private String name;
public Cat() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
String strCat = new StringBuffer()
.append(this.getId()).append(", ")
.append(this.getName()).append(", ")
.toString();
return strCat;
}
}

第三个文件:Cat.hbm.xml

<?xml version="1.0" ?>
<!DOCTYPE hibernate-mapping (View Source for full doctype...)>
- <hibernate-mapping default-cascade="none" default-access="property" auto-import="true"]
- <class name="hb.Cat" table="cat" mutable="true" polymorphism="implicit" dynamic-update="false" dynamic-insert="false" batch-size="1" select-before-update="false" optimistic-lock="version"]
- <id name="id" type="java.lang.String" unsaved-value="null"]
<column name="CAT_ID" sql-type="varchar(32)" not-null="true"]
<generator class="uuid.hex"]
</id>
- <property name="name" not-null="false" unique="false" update="true" insert="true"]
<column name="NAME" sql-type="varchar(20)" not-null="true"]
</property>
</class>
</hibernate-mapping>

第四个文件:HibernateUtil.java

package hb;
import net.sf.hibernate.*;
import net.sf.hibernate.cfg.*;
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 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();
}
}

第五个文件: addCat.jsp

<%@ page contentType="text/html; charset=GB18030" %>
<%@ page import="hb.*, net.sf.hibernate.*" %>
<html>
<head>
<title>test</title>
</head>
<body bgcolor="#ffffff"]
<h1>Test Hibernate</h1>
<a href="getCats.jsp"]View Cats[/url]<br>
<%
if(request.getParameter("Go") != null) {
String name = request.getParameter("name");
if(name == null || name.length() < 1) {
out.println("Name can not be empty!");
return;
}
SessionFactory sessionFactory;
net.sf.hibernate.Session hsession = HibernateUtil.currentSession();
Transaction tx = hsession.beginTransaction();
Cat c;
c = new Cat();
c.setName(name);
hsession.save(c);
hsession.flush();
tx.commit();
HibernateUtil.closeSession();
out.println("Done.");
}
%>
Add New Cats:<br>
<form method=post action=addCat.jsp>
Name:<input name=name><br>
<input type=submit name='Go' value='Add'><br>
</form>
</body>
</html>

第六个文件: getCats.jsp

<%@ page contentType="text/html; charset=GB18030" %>
<%@ page import="hb.*, net.sf.hibernate.*,
java.util.*" %>
<html>
<head>
<title>Hibernate Test</title>
</head>
<body bgcolor="#ffffff"]
<h1>Test Hibernate</h1>
<a href=addCat.jsp>Add Cat[/url]<br>
<p> ID Name </p>
<%
SessionFactory sessionFactory;
net.sf.hibernate.Session hsession;
hsession = HibernateUtil.currentSession();
Transaction tx = hsession.beginTransaction();
Query query = hsession.createQuery ("select cat from Cat as cat ");
for (Iterator it = query.iterate(); it.hasNext();) {
Cat cat = (Cat) it.next();
out.println(cat.getId()+"&nbsp; " + cat.getName() + "<br>");
}
tx.commit();
HibernateUtil.closeSession();
out.println("Done.");
%>
</body>
</html>

说明:
1.对HibernateUtil.java的编译:
javac -classpath ../lib/hibernate2.jar HibernateUtil.java
有2个警告,可以忽略之。

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