SSH(spring+springMVC+hibernate)配置

**整合spring+springMVC+hibernate個人感覺開發效率確實很快,不需要自己創建表,面向對象的操作數據庫更加清晰的分析業務關係。不過相比spring+springMVC+mybatis欠缺靈活度,不方便改動**

1、導入jar包
(jar包中包含了部分多餘元素)
這裏寫圖片描述這裏寫圖片描述這裏寫圖片描述
2、配置文件
(1)首先配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
    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_3_0.xsd">
  <!-- 配置spring容器監聽器 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>
  </context-param>

  <!-- 監聽器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 前端控制器 -->
  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/springMVC.xml</param-value>
    </init-param>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>*.action</url-pattern>
  </servlet-mapping>

  <display-name></display-name> 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

(2)配置spring,這裏對spring進行了分類

這裏寫圖片描述

一、springMVC.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

    <!-- 註解的handler -->
    <!-- <bean class="com.yy.springMVC.ItemsController3"/> -->
    <!-- spring的註解掃描 -->
    <context:component-scan base-package="com.yy.controller" />

    <!-- 可以替代映射器和適配器 -->
    <mvc:annotation-driven conversion-service="conversionService" />

    <!-- 配置視圖解析器 -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

二、applicationContext-dao.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
        http://www.springframework.org/schema/mvc 
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd 
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
    <!-- spring註解掃描 -->

    <context:annotation-config />
    <context:component-scan base-package="com.yy.dao" />

    <!-- 數據源 -->
    <!-- 加載配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />
    <!-- 數據庫連接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <!-- 連接池啓動時的初始值 -->
        <property name="initialSize" value="1" />
        <!-- 連接池的最大值 -->
        <property name="maxActive" value="10" />
        <!-- 最大空閒值.當經過一個高峯時間後,連接池可以慢慢將已經用不到的連接慢慢釋放一部分, 一直減少到maxIdle爲止 -->
        <property name="maxIdle" value="5" />
        <!-- 最小空閒值.當空閒的連接數少於閥值時,連接池就會預申請去一些連接,以免洪峯來時來不及申請 -->
        <property name="minIdle" value="1" />

    </bean>


    <!-- 把數據源模板注入sessionfactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 用hibernate.cfg.xml配置文件方式配置 -->
        <!-- <property name="configLocation" value="classpath:hibernate/hibernate.cfg.xml" 
            /> -->
        <!-- 在spring中配置 -->
        <property name="dataSource" ref="dataSource" />
        <property name="mappingDirectoryLocations">
            <list>
                <value>classpath:/com/yy/entity</value>
            </list>
        </property>
        <!-- 可在哪個包中掃描 -->
        <!-- <property name="packagesToScan"> <value>com.yy.entity</value> </property> -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <!-- sessionFactory.getCurrentSession()可以完成一系列的工作,
                當調用時,hibernate將session綁定到當前線程,事務結束後,
                hibernate將session從當前線程中釋放,並且關閉session。
                當再次調用getCurrentSession()時,將得到一個新的session,並重新開始這一系列工作。 -->
                <!-- <prop key="hibernate.current_session_context_class">thread</prop>
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</prop>  -->
                <!-- 系統將該HQL轉換爲SQL語句時,該設置表明將用字符 1 和 0 來 取代關鍵字true 和 false -->
                <prop key="hibernate.query.substitutions">yes 'Y',no 'N'</prop>
                <!-- hql語句可定位性 -->
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="javax.persistence.validation.mode">none</prop>
            </props>
        </property>
    </bean>

</beans>

三、applicationContext-service.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:aop="http://www.springframework.org/schema/aop"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:instance="http://www.springframework.org/schema/instance"
        xsi:schemaLocation= "http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.2.xsd
                            http://www.springframework.org/schema/tx 
                            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">

    <!-- 注入service(單個注入) -->
    <!-- <bean id="userService" class="com.yy.service.impl.UserServiceImpl"/> -->
    <!-- spring的註解掃描 -->
        <context:component-scan base-package="com.yy.service"/>

</beans>

四、applicationContext-transaction.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:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:instance="http://www.springframework.org/schema/instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.2.xsd
                            http://www.springframework.org/schema/tx 
                            http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <!-- 使用聲明式事務配置,可以有效地規範代碼 -->
    <!-- 事務管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    <!-- 通知 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="load*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="list*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="save*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="insert*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="update*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
            <tx:method name="delete*" propagation="REQUIRED"
                rollback-for="java.lang.Exception" />
        </tx:attributes>
    </tx:advice>


    <!-- aop -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice"
            pointcut="execution( * com.yy.service..*.*(..))" />
    </aop:config>

</beans>

3、分別創建Controller、Service、Dao,實體類和映射文件
4、將sessionFactory注入Dao的方法

    @Resource(name = "sessionFactory")
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Session getSession() {
        return sessionFactory.getCurrentSession();
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章