服务器

位置:IT落伍者 >> 服务器 >> 浏览文章

在Tomcat下配置Hibernate的开发环境[3]


发布日期:2019年06月12日
 
在Tomcat下配置Hibernate的开发环境[3]

第四个文件HibernateUtiljava

package hb;

import netsfhibernate*;

import netsfhibernatecfg*;

public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {

try {

sessionFactory = new Configuration()configure()buildSessionFactory();

} catch (HibernateException ex) {

throw new RuntimeException(Exception building SessionFactory:

+ exgetMessage() ex);

}

}

public static final ThreadLocal session = new ThreadLocal();

public static Session currentSession() throws HibernateException {

// Session s;

Session s = (Session) sessionget();

// Open a new Session if this Thread has none yet

if (s == null) {

s = sessionFactoryopenSession();

sessionset(s);

}

return s;

}

public static void closeSession() throws HibernateException {

Session s = (Session) sessionget();

sessionset(null);

if (s != null)

sclose();

}

}

第五个文件 addCatjsp

<%@ page contentType=text/html; charset=GB %>

<%@ page import=hb* netsfhibernate* %>

<html>

<head>

<title>test</title>

</head>

<body bgcolor=#ffffff]

<h>Test Hibernate</h>

<a href=getCatsjsp]View Cats<br>

<%

if(requestgetParameter(Go) != null) {

String name = requestgetParameter(name);

if(name == null || namelength() < ) {

outprintln(Name can not be empty!);

return;

}

SessionFactory sessionFactory;

netsfhibernateSession hsession = HibernateUtilcurrentSession();

Transaction tx = hsessionbeginTransaction();

[] [] [] []

               

上一篇:在Tomcat下配置Hibernate的开发环境[4]

下一篇:在Tomcat下配置Hibernate的开发环境[2]