<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-11  評論-0  文章-0  trackbacks-0

    使用配置文件對DAO層封裝具有分頁功能的S2SH整合實例,李順利,配置文件,DAO層封裝,分頁,SSH整合,實例

    前言

    前面寫過使用Annotation并對DAO層封裝具有分頁功能的S2SH整合實例文章,收到了幾位好友的一些意見和支持感到很開心,實際上整合實例網上很多,我本人也是從網上學習 了很多,但是基于很多網上的實例并不一定能夠運行自己才動手寫了一個整合實例,即為了總結前人的經驗也是為了自己以后的開發需要。

    上篇博文主要是介紹Annotation的使用,也許現在的主流還是使用XML配置文件來整合各個框架。配置文件的使用雖然多但是簡單,很明了。在這一篇博文中我就帶你看看如何 使用配置文件對DAO層封裝具有分頁功能的S2SH整合的。

    請閱讀本篇博文前請閱讀

    Struts+Spring+Hibernate整合注冊登錄——使用Myeclipse快速開發

    使用Annotation并對DAO層封裝具有分頁功能的S2SH整合實例

    其中涉及到與上面兩篇博文相同的我就不再敘述了。

    配置文件整合重點

           這次使用的是配置文件來整合,當然把上篇博文中使用Annotation注解去掉。主要是主要StrutsHibernate的配置文件都需要使用Spring進行相關配置。貼幾個配置文件 吧。

    web.xmlstruts.xml的配置文件沒有太多的變化,主要是Spring的配置文件的變化。

    在對DAO層封裝的時候涉及到一個問題,就是Spring如何對泛型的支持。在網上搜了很久,沒有很好的解決,本想對DAO層注入的,既然注入出現空指針異常的錯 誤,那么就應該換條思路。DAO層封裝使用的是泛型加抽象類,既然Spring對泛型的支持不好,那么應該可以對DAO層抽象類的子類(提供泛型的實際類)進行注入。后來測試了一下,一切OK。還有DAO層封裝的實現類中使用Annotation獲得一些屬性的都應該 修改。例子:

    Annotation:

    public abstract class BaseDaoSupport<T> extends HibernateDaoSupport implements IBaseDao<T>

    {

        protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(this.getClass());

        protected String entityClassName = getEntityName(this.entityClass);

    //其他就省略請 看源代碼

        /**

         * 獲取實體的名稱

         *

         * @param <E>

         * @param clazz

         *            實體類

         * @return

         */

        protected static <E> String getEntityName(Class<E> clazz)

        {

            String entityname = clazz.getSimpleName();

            Entity entity = clazz.getAnnotation(Entity.class);

            if (entity.name() != null && !"".equals(entity.name()))

            {

                entityname = entity.name();

            }

            return entityname;

        }

    }

    配置文件

    public abstract class BaseDaoSupport<T> extends HibernateDaoSupport implements IBaseDao<T>

    {

        protected Class<T> entityClass = GenericsUtils.getSuperClassGenricType(this.getClass());

        protected String entityClassName = getEntityName(this.entityClass);

    //其他就省略請 看源代碼

        /**

         * 獲取實體的名稱

         *

         * @param <E>

         * @param clazz

         *            實體類

         * @return

         */

        protected static <E> String getEntityName(Class<E> clazz)

        {

            String entityname = clazz.getSimpleName();

            return entityname;

        }

    }

     

    整合中主要是Spring配置文件的配置,貼下配置文件

    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"

        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.0.xsd

                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd

                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

     

        <!--配置文件導入  -->

        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

            <property name="location">

                <value>classpath:dataSource.properties</value>

            </property>

        </bean>

     

        <!--數據源   -->

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

            <property name="driverClassName" value="${mysql.database.driver}"></property>

            <property name="url" value="${mysql.database.url}"></property>

            <property name="username" value="${mysql.database.user}"></property>

            <property name="password" value="${mysql.database.password}"></property>

            <property name="maxActive" value="${mysql.database.maxActive}"></property>

            <property name="maxIdle" value="${mysql.database.maxIdle}"></property>

            <property name="maxWait" value="${mysql.database.maxWait}"></property>

        </bean>

       

        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

            <property name="dataSource">

                <ref bean="dataSource" />

            </property>

            <property name="hibernateProperties">

                <props>

                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>

                </props>

            </property>

            <property name="mappingResources">

                <list>

                    <value>org/usc/beans/Teacher.hbm.xml</value>

                    <value>org/usc/beans/Student.hbm.xml</value>

                </list>

            </property>

        </bean>

       

        <!-- 配置事務管理器 -->

        <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

            <property name="sessionFactory">

                <ref bean="sessionFactory" />

            </property>

        </bean>

     

        <!-- 配置事務的傳播特性 -->

        <tx:advice id="txAdvice" transaction-manager="transactionManager">

            <tx:attributes>

                <tx:method name="save*" propagation="REQUIRED" />

                <tx:method name="update*" propagation="REQUIRED" />

                <tx:method name="delete*" propagation="REQUIRED" />

                <tx:method name="*" read-only="true" />

            </tx:attributes>

        </tx:advice>

     

     

        <!-- 那些類的哪些方法參與事務 -->

        <aop:config>

            <aop:pointcut id="allServiceMethod" expression="execution(* org.usc.daos.*.*.*(..))" />

            <aop:advisor pointcut-ref="allServiceMethod" advice-ref="txAdvice" />

        </aop:config>

     

        <bean name="StudentService" class="org.usc.services.student.impl.StudentServiceBean">

            <property name="sessionFactory" ref="sessionFactory"></property>

        </bean>

     

        <bean name="TeacherService" class="org.usc.services.teacher.impl.TeacherServiceBean">

            <property name="sessionFactory" ref="sessionFactory"></property>

        </bean>

     

        <bean name="ListStudentAction" class="org.usc.actions.StudentListAction" scope="prototype">

            <property name="studentService" ref="StudentService"></property>

        </bean>

     

        <bean name="ListTeacherAction" class="org.usc.actions.TeacherListAction" scope="prototype">

            <property name="teacherService" ref="TeacherService"></property>

        </bean>

     

    </beans>

    Spring 的配置文件中studentService等邏輯層的服務類就實現了DAO層的封裝抽象類,由Spring注入sessionFactory,就避免了Spring直接對泛型類的注入。

           整合我已經測試過,完全可以運行,先提供源碼下載。如果出現問題請首先查看是不是數據庫配置錯誤, 謝謝。

    源碼下載

    順 利提供下載:(源碼沒有相應的Jar包,需要下載下面的整合Jar包 添加進后才可以運行)
      名:SSHDemo.rar
    下載地址:http://usc.googlecode.com/files/SSHDemo.rar
    clip_image001
    順利提供下載:
      名:Struts2.1.8+Hibernate3.3+Spring3.0整合所需Jar.rar
    下載地址:

    http://usc.googlecode.com/files/Struts2.1.8%2BHibernate3.3%2BSpring3.0%E6%95%B4%E5%90%88%E6%89%80%E9%9C%80Jar%E5%8C%85.rar
    clip_image001 

    學習探討

    如果有什么建議或意見可以通過Q506817493 Eleeshunli@qq.com,大家一起交流學習。使用AnnotationDAO層封裝具有分頁功能的S2SH整合和使用配置文件對DAO層封裝具有分頁功能的S2SH整合是對S2SH整合的一個系列,基本上能夠完成對SSH整合的基本要求(分頁和DAO層封裝),順利也提供源碼下載供大家一起學習探討。

    順利完成于2010312
    posted on 2010-08-22 23:33 王健 閱讀(164) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲国产天堂在线观看| 无码乱人伦一区二区亚洲一| 亚洲一区二区三区亚瑟| 亚洲成人免费网址| 亚洲精品在线不卡| 蜜臀AV免费一区二区三区| 91嫩草私人成人亚洲影院| free哆啪啪免费永久| 亚洲精品高清国产麻豆专区| 日本亚洲免费无线码| 亚洲中文字幕无码爆乳app| 日本特黄特色aa大片免费| 国产亚洲精品美女久久久久 | 久久精品亚洲日本波多野结衣 | 亚洲精品线在线观看| 在线免费观看你懂的| 亚洲av永久无码精品三区在线4 | 国产又黄又爽胸又大免费视频| 久久精品国产亚洲AV果冻传媒| 免费无码一区二区三区| 亚洲综合激情五月丁香六月| 国产91在线免费| 可以免费观看的毛片| 亚洲人成电影网站| 国产成人免费高清在线观看| 精品国产呦系列在线观看免费 | 亚洲综合在线成人一区| 永久免费毛片手机版在线看| 一区二区三区免费视频播放器 | 免费人成网站在线观看10分钟| 一本色道久久88—综合亚洲精品 | 在线视频精品免费| 国产成人精品亚洲一区| 亚洲av无码片在线播放| 国语成本人片免费av无码| 一级一级毛片免费播放| 亚洲成人福利在线观看| 亚洲国产电影av在线网址| 男人的天堂网免费网站| 亚洲国产日韩a在线播放| 国产亚洲综合一区柠檬导航|