【 SSH 整合】Spring、Struts、Hibernate基本整合

applicationContext.xml

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

    <!-- 加载db.properties文件 -->
    <bean id="config" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
        <property name="locations" value="classpath*:*.properties"/>
    </bean>

    <!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <property name="initialSize" value="${initialSize}"></property>
        <property name="maxActive" value="${maxActive}"></property>
        <property name="maxIdle" value="${maxIdle}"></property>
        <property name="minIdle" value="${minIdle}"></property>
    </bean>

    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <!-- //加载实体类的映射文件位置及名称 -->
        <property name="mappingLocations" value="classpath:k/bean/*.hbm.xml"></property>
        <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
    </bean>

    <!--事务-->
    <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!--开启注解配置-->
    <context:annotation-config/>

    <!--配置 action-->
    <bean name="/login" class="k.action.LoginAction" scope="prototype"/>
    <bean name="/employee" class="k.action.EmployeeAction" scope="prototype"/>

    <!--配置 Service-->
    <bean name="testService" class="k.service.TestService"/>
    <bean name="employeeService" class="k.service.impl.EmployeeServiceImpl"/>
    <bean name="departmentService" class="k.service.impl.DepartmentServiceImpl"/>

</beans>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts-config PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
        "http://struts.apache.org/dtds/struts-config_1_3.dtd">

<struts-config>

    <form-beans>
        <form-bean name="employeeForm" type="k.form.EmployeeForm">
            <form-property name="id" type="java.lang.Integer"></form-property>
            <form-property name="name" type="java.lang.String"></form-property>
            <form-property name="password" type="java.lang.String"></form-property>
        </form-bean>
    </form-beans>

    <global-forwards>
        <forward name="ok" path="/WEB-INF/jsp/ok.jsp"></forward>
        <forward name="err" path="/WEB-INF/jsp/err.jsp"></forward>
    </global-forwards>

    <action-mappings>
        <action name="employeeForm" path="/login" parameter="action" type="k.action.LoginAction"
                scope="request" attribute="employeeForm" input="index.jsp" validate="false">
            <forward name="main" path="/WEB-INF/jsp/main.jsp"></forward>
            <forward name="loginJsp" path="/WEB-INF/jsp/login.jsp"></forward>
        </action>
        <action name="employeeForm" path="/employee" parameter="action" type="k.action.EmployeeAction"
                scope="request" attribute="employeeForm" input="index.jsp" validate="false">
            <forward name="addEmployeeUI" path="/WEB-INF/jsp/addEmployeeUI.jsp"></forward>
            <forward name="loginJsp" path="/WEB-INF/jsp/login.jsp"></forward>
        </action>
    </action-mappings>

    <controller>
        <set-property property="processorClass"
                      value="org.springframework.web.struts.DelegatingRequestProcessor"></set-property>
    </controller>

</struts-config>

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">
<hibernate-configuration>
    <session-factory>
        <!-- 配置Hibernate的基本属性 -->
        <!-- 1.数据源配置到IOC容器中 -->
        <!-- 2.关联的.hbm.xml也在IOC容器配置SessionFactory实例 -->
        <!-- 3.配置Hibernate的基本属性:方言,SQL显示及格式化,生成数据表的策略以及二级缓存 -->
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.show_sql">true</property>
        <property name="hibernate.format_sql">true</property>
        <property name="cache.use_second_level_cache">true</property>
        <property name="ache.provider_class">org.hibernate.cache.internal.DefaultCacheKeysFactory</property>
        <property name="generate_statistics">true</property>
    </session-factory>
</hibernate-configuration>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- spring 初始化 -->
    <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-class>k.listener.StartSystemListener</listener-class>
        <!--<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>-->
    </listener>

    <!-- struts 初始化 -->
    <servlet>
        <servlet-name>action</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!-- 配置请求过滤器,编码格式设为UTF-8,避免中文乱码-->
    <filter>
        <filter-name>springUtf8Encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>springUtf8Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--<filter>-->
    <!--<filter-name>EncodingFilter</filter-name>-->
    <!--<filter-class>k.listener.EncodingFilter</filter-class>-->
    <!--</filter>-->
    <!--<filter-mapping>-->
    <!--<filter-name>EncodingFilter</filter-name>-->
    <!--<url-pattern>/*</url-pattern>-->
    <!--</filter-mapping>-->

    <!-- 懒加载 -->
    <filter>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInViewFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <!-- 以下3项参数与log4j的配置相关 -->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>classpath:log4j.properties</param-value>
    </context-param>

    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>


</web-app>

猜你喜欢

转载自www.cnblogs.com/kikyoqiang/p/12327819.html