spring+hiberate+struts+oracle+myeclipse的环境配置

首先应该配置hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools.                   -->
<hibernate-configuration>

<session-factory>
	<property name="dialect">
		org.hibernate.dialect.Oracle9Dialect
	</property>
	<property name="connection.url">
		hjdbc:oracle:thin:@localhost:1521:orcl
	</property>
	<property name="connection.username">chy</property>
	<property name="connection.password">chy</property>
	<property name="connection.driver_class">
		oracle.jdbc.driver.OracleDriver
	</property>
	<property name="myeclipse.connection.profile">news</property>
	<property name="show_sql">true</property>
	<property name="connection.autocommit">true</property>
	<property name="current_session_context_class">thread</property>
	<property name="hbm2ddl.auto">save-update</property>
	<mapping resource="bean/News.hbm.xml" />
	<mapping resource="bean/Review.hbm.xml" />
	<mapping resource="bean/Topic.hbm.xml" />
	<mapping resource="bean/Users.hbm.xml" />

</session-factory>

</hibernate-configuration>

然后再类HibernateSessionFactory调用

package hibernate;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;

/**
 * Configures and provides access to Hibernate sessions, tied to the
 * current thread of execution.  Follows the Thread Local Session
 * pattern, see {@link http://hibernate.org/42.html }.
 */
public class HibernateSessionFactory {

    /** 
     * 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 = "/hibernate.cfg.xml";
	private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    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("%%%% Error Creating SessionFactory %%%%");
			e.printStackTrace();
		}
    }
    private HibernateSessionFactory() {
    }
	
	/**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> 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);
		}

        return session;
    }

	/**
     *  Rebuild hibernate session factory
     *
     */
	public static void rebuildSessionFactory() {
		try {
			configuration.configure(configFile);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Exception e) {
			System.err
					.println("%%%% Error Creating SessionFactory %%%%");
			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
     *
     *	session factory will be rebuilded in the next call
     */
	public static void setConfigFile(String configFile) {
		HibernateSessionFactory.configFile = configFile;
		sessionFactory = null;
	}

	/**
     *  return hibernate configuration
     *
     */
	public static Configuration getConfiguration() {
		return configuration;
	}

}

 然后再applicationContext.xml文件中把所有要用到的biz,dao,controller写在里面

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml">
		</property>
	</bean>
	<bean id="TopicDAO" class="dao.TopicDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="UsersDAO" class="dao.UsersDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="ReviewDAO" class="dao.ReviewDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="NewsDAO" class="dao.NewsDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
	<bean id="UserBizImpl" class="biz.impl.UserBizImpl">
		<property name="ud">
			<ref bean="UsersDAO" />
		</property>
	</bean>
	
	<bean id="UserAction" class="action.UserAction" >
		<property name="ub">
			<ref bean="UserBizImpl" />
		</property>
	</bean>
	
	<bean id="TopicBizImpl" class="biz.impl.TopicBizImpl">
		<property name="td">
			<ref bean="TopicDAO" />
		</property>
	</bean>
	
	<bean id="TopicAction" class="action.TopicAction" >
		<property name="tb">
			<ref bean="TopicBizImpl" />
		</property>
	</bean>
	
	<bean id="NewsBizImpl" class="biz.impl.NewsBizImpl">
		<property name="nd">
			<ref bean="NewsDAO" />
		</property>
	</bean>
	
	<bean id="NewsAction" class="action.NewsAction" >
		<property name="nb">
			<ref bean="NewsBizImpl" />
		</property>
		<property name="tb">
			<ref bean="TopicBizImpl" />
		</property>
		<property name="rb">
			<ref bean="ReviewBizImpl" />
		</property>
	</bean>
	
	<bean id="ReviewBizImpl" class="biz.impl.ReviewBizImpl">
		<property name="rd">
			<ref bean="ReviewDAO" />
		</property>
	</bean>
	
	<bean id="ReviewAction" class="action.ReviewAction" >
		<property name="rb">
			<ref bean="ReviewBizImpl" />
		</property>
		<property name="nb">
			<ref bean="NewsBizImpl" />
		</property>
	</bean>
</beans>

 然后是最重要的文件,web.xml综合监听和配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<filter>
		<filter-name>CharacterEncodingFilter</filter-name>
		<filter-class>filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	<filter>
		<filter-name>struts2</filter-name>
		<filter-class>
			org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>
原文地址:https://www.cnblogs.com/bingrong/p/3342499.html