在Tomcat下配置Hibernate的开发环境
来源: 录入时间:07-09-02 12:20:55
这是实战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
-
-
第二个文件: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
-
-
第四个文件: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.*" %>
Test Hibernate
<%
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:
第六个文件: getCats.jsp
<%@ page contentType="text/html; charset=GB18030" %>
<%@ page import="hb.*, net.sf.hibernate.*,
java.util.*" %>
Test Hibernate
Add Cat[/url]
ID Name
<%
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()+" " + cat.getName() + "
");
}
tx.commit();
HibernateUtil.closeSession();
out.println("Done.");
%>
说明:
1.对HibernateUtil.java的编译:
javac -classpath ../lib/hibernate2.jar HibernateUtil.java
有2个警告,可以忽略之。
上一篇文章:
下一篇文章: