SSH环境搭建(一)整合SSH配置文件

一,将hibernate配置文件写到applicationContext.xml中.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        ">
    <!-- 配置service -->     
   <bean id="customerService" class="com.service.impl.CustomerServiceImpl">
        <property name="customerDao" ref="customerDao"></property>
   </bean>
   <!-- 配置Dao -->
   <bean id="customerDao" class="com.dao.impl.CustomerDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"></property>
   </bean>
   <!-- 配置hibernateTemplate -->
   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <!-- spring接管sessionFactory的创建 
        用spring提供的提高sessionFactory:LocalSessionFactoryBean
        创建sessionFactory有三部分信息
            就是hibernate配置文件的信息      
   -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <!-- 注入hibernate配置文件 -->
        <!-- <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>  -->
        <property name="dataSource" ref="dataSource"/>
        <!-- hibernate参数设置 -->
        <property name="hibernateProperties">
            <props>
                <!-- 数据库方言 -->
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <!-- 显示sql语句-->
                <prop key="hibernate.show_sql">true</prop>
                <!-- 格式化SQL语句 -->
                <prop key="hibernate.format_sql">true</prop>
                <!-- create:根据映射关系生成表 -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
            </props>
        </property>
         <!-- 3,映射配置文件位置 
            name有三种属性:
                mappingResource:它是注入一个spring类型的数组,提供的是映射文件的位置
                mappingDirectoryLocations:它是注入一个Resource类型的数组,提供映射文件的目录
                mappingLocations:它是注入一个Resource类型的数组,可以使用通配符
         -->
         <property name="mappingLocations">
            <array>
                <value>classpath:com/entity/*.hbm.xml</value>
            </array>
         </property>
   </bean>
    <!-- 数据源,与数据库连接 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <!-- 数据库驱动 -->
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <!-- 数据库连接地址 -->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/ssh?characterEncoding=utf8"/>
        <!-- 用户名 -->
        <property name="user" value="root"/>
        <!-- 密码 -->
        <property name="password" value="123456"/>
        <!-- 最小连接数 -->
        <property name="minPoolSize" value="3"/>
        <!-- 最大连接数 -->
        <property name="maxPoolSize" value="10"/>
        <!-- 初始化连接数 -->
        <property name="initialPoolSize" value="5"/>
    </bean>


   <!-- 配置事务管理器 -->
   <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <!-- 配置事务的通知 -->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED" read-only="false"/>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
        </tx:attributes>
   </tx:advice>
   <!-- 配置AOP -->
   <aop:config>
        <!-- 配置切入点表达式 -->
        <aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="pt1"/>
        <!-- 建立切入点表达式和事务通知的关联 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"/>
   </aop:config>
</beans>

二, 手动指定spring配置文件和struts2配置文件的位置
web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>struts2</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- struts2核心过滤器 -->
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    <!-- 手动指定struts2配置文件位置 -->
    <init-param>
        <param-name>config</param-name>
        <param-value>struts-default.xml,struts-plugin.xml,config/struts.xml</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 配置spring监听器,用于监听servletContext对象创建,同时为我们创建spring容器
        默认情况下:只能加载web-inf目录的spring配置文件,同时文件名为:applicationContext.xml
   -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 手动指定spring配置文件位置 -->
 <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
</web-app>

hibernateDaoSupport的使用
持久层Dao的实现类继承HibernateDaoSupport

/*
 * 持久层实现类
 */

public class CustomerDaoImpl extends HibernateDaoSupport implements CustomerDao {
    /*private HibernateTemplate hibernateTemplate;*/

    /*
     * 继承了HibernateTemplate,不需要在创建HibernateTemplate
     * 因为在HibernateTemplate中以及被创建了,可以直接获取父类中的hibernateTemplate
     * 
     */
    @Override
    public List<Customer> findAll() {
    return (List<Customer>) getHibernateTemplate().find("from Customer");
    /*return (List<Customer>) hibernateTemplate.find("from Customer");*/
    }

    @Override
    public void save(Customer customer) {
    /*hibernateTemplate.save(customer);*/
    getHibernateTemplate().save(customer);
    }

   /* public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }*/




}

修改applicationContext.xml配置文件.
由于hibernateTemplate 不再由我们自己创建,所以在applicationContext中,我们不再需要配置hibernateTemplate .

<!-- 配置Dao -->
   <bean id="customerDao" class="com.dao.impl.CustomerDaoImpl">
        <!-- <property name="hibernateTemplate" ref="hibernateTemplate"></property> -->
        <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <!-- 配置hibernateTemplate -->
  <!--  <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
   </bean> -->

配置action
applicationContext.xml

<!-- 配置action -->    
    <bean id="customerAction" class="com.action.CustomerAction" scope="prototype">
        <property name="customerService" ref="customerService"></property>
    </bean>

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!-- 在把action交给spring配置后,spring中bean的id要与class相同 -->
    <package name="customer-action" extends="struts-default" namespace="/customer">
        <action name="customerAction_*" class="customerAction" method="{1}">
            <result name="{1}">/WEB-INF/{1}.jsp</result>
        </action>
    </package>  
</struts>

猜你喜欢

转载自blog.csdn.net/qecode/article/details/81146086