/**
* Location of hibernate.cfg.xml file.
* Location should be on the classpath as Hibernate uses
* #resourceAsStream style lookup for its configuration file.
* The default classpath location of the hibernate config file is * in the default package. Use #setConfigFile() to update
* the location of the configuration file for the current session. */
private static String CONFIG_FILE_LOCATION = \ private static final ThreadLocal
private static Configuration configuration = new Configuration(); private static org.hibernate.SessionFactory sessionFactory; private static String configFile = CONFIG_FILE_LOCATION;
static { try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err
.println(\ e.printStackTrace(); } }
private HibernateUtil() { } /**
* Returns the ThreadLocal Session instance. Lazy initialize * the SessionFactory if needed. *
* @return Session
* @throws HibernateException */
public static Session getSession() throws HibernateException { Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen()) { if (sessionFactory == null) { rebuildSessionFactory(); }
session = (sessionFactory != null) ? sessionFactory.openSession() : null;
threadLocal.set(session);
24
}
return session; }
/**
* Rebuild hibernate session factory * */
public static void rebuildSessionFactory() { try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory(); } catch (Exception e) { System.err
.println(\ e.printStackTrace(); } }
/**
* Close the single hibernate session instance. *
* @throws HibernateException */
public static void closeSession() throws HibernateException { Session session = (Session) threadLocal.get(); threadLocal.set(null);
if (session != null) { session.close(); } }
/**
* return session factory * */
public static org.hibernate.SessionFactory getSessionFactory() { return sessionFactory; }
/**
* return session factory *
25
* session factory will be rebuilded in the next call */
public static void setConfigFile(String configFile) { HibernateUtil.configFile = configFile; sessionFactory = null; }
/**
* return hibernate configuration * */
public static Configuration getConfiguration() { return configuration; } }
Struts.xml
\ \>
class=\ method=\> 26
class=\>
27
五、系统测试
5-1 添加图书类别成功
输入所要添加的管理员名称,然后输入密码,再次确认密码,点击添加,显示添加管理员成功。
28