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

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

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

    李順利
    隨筆-50  評(píng)論-170  文章-0  trackbacks-0

    使用配置文件對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例

     

    李順利

    2010312

    關(guān)鍵詞

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

    前言

    前面寫過使用Annotation并對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例文章,收到了幾位好友的一些意見和支持感到很開心,實(shí)際上整合實(shí)例網(wǎng)上很多,我本人也是從網(wǎng)上學(xué)習(xí)了很多,但是基于很多網(wǎng)上的實(shí)例并不一定能夠運(yùn)行自己才動(dòng)手寫了一個(gè)整合實(shí)例,即為了總結(jié)前人的經(jīng)驗(yàn)也是為了自己以后的開發(fā)需要。

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

    請(qǐng)閱讀本篇博文前請(qǐng)閱讀

    Struts+Spring+Hibernate整合注冊(cè)登錄——使用Myeclipse快速開發(fā)

    使用Annotation并對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例

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

    配置文件整合重點(diǎn)

           這次使用的是配置文件來整合,當(dāng)然把上篇博文中使用Annotation注解去掉。主要是主要StrutsHibernate的配置文件都需要使用Spring進(jìn)行相關(guān)配置。貼幾個(gè)配置文件吧。

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

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

    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);

    //其他就省略請(qǐng)看源代碼

        /**

         * 獲取實(shí)體的名稱

         *

         * @param <E>

         * @param clazz

         *            實(shí)體類

         * @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);

    //其他就省略請(qǐng)看源代碼

        /**

         * 獲取實(shí)體的名稱

         *

         * @param <E>

         * @param clazz

         *            實(shí)體類

         * @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">

     

        <!--配置文件導(dǎo)入  -->

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

            <property name="location">

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

            </property>

        </bean>

     

        <!--數(shù)據(jù)源   -->

        <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>

       

        <!-- 配置事務(wù)管理器 -->

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

            <property name="sessionFactory">

                <ref bean="sessionFactory" />

            </property>

        </bean>

     

        <!-- 配置事務(wù)的傳播特性 -->

        <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>

     

     

        <!-- 那些類的哪些方法參與事務(wù) -->

        <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等邏輯層的服務(wù)類就實(shí)現(xiàn)了DAO層的封裝抽象類,由Spring注入sessionFactory,就避免了Spring直接對(duì)泛型類的注入。

           整合我已經(jīng)測(cè)試過,完全可以運(yùn)行,先提供源碼下載。如果出現(xiàn)問題請(qǐng)首先查看是不是數(shù)據(jù)庫配置錯(cuò)誤,謝謝。

    源碼下載

    順利提供下載:(源碼沒有相應(yīng)的Jar包,需要下載下面的整合Jar包添加進(jìn)后才可以運(yùn)行)
      名: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 

    學(xué)習(xí)探討

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

    順利完成于2010312



    博客中的一些下載已經(jīng)放到了百度云了,請(qǐng)根據(jù)需要下載。【點(diǎn)我去百度云下載】

    最后弱弱地說一下,如果可以的話,轉(zhuǎn)載請(qǐng)?zhí)峁┏鎏? ),謝謝。
    posted on 2010-03-12 10:05 李順利 閱讀(4078) 評(píng)論(6)  編輯  收藏

    評(píng)論:
    # re: 使用配置文件對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例 2010-04-03 15:16 | Love
    # re: 使用配置文件對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例[未登錄] 2010-05-11 18:40 | randy
    我剛學(xué),實(shí)在不明白怎么對(duì)業(yè)務(wù)層配置事務(wù),就是在一個(gè)service層中,當(dāng)調(diào)用多個(gè)dao時(shí),如果其中一個(gè)dao出錯(cuò),會(huì)回滾整個(gè)service中調(diào)用的dao操作?  回復(fù)  更多評(píng)論
      
    # re: 使用配置文件對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例 2010-05-11 20:09 | 李順利
    @randy
    到google搜索一下“spring 事務(wù) service”,應(yīng)該有很詳細(xì)的解答
    Spring的事務(wù)管理功能可以做到細(xì)粒度的  回復(fù)  更多評(píng)論
      
    # re: 使用配置文件對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例[未登錄] 2010-05-11 22:03 | Randy
    @李順利
    謝謝,我去找找看。  回復(fù)  更多評(píng)論
      
    # re: 使用配置文件對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例 2010-06-08 09:05 | 威爾
    有一個(gè)問題想問你?
    你用的是spring3.0 ,HibernateCallback支持泛型
    可spring2.5,HibernateCallback不支持泛型
    很郁悶 不知怎么解決?
      回復(fù)  更多評(píng)論
      
    # re: 使用配置文件對(duì)DAO層封裝具有分頁功能的S2SH整合實(shí)例 2011-09-08 16:33 | 淘寶網(wǎng)女裝冬裝
    下載了,大半年沒搞JAVA了  回復(fù)  更多評(píng)論
      

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲永久在线观看| 最新仑乱免费视频| 国产亚洲综合精品一区二区三区| 亚洲午夜视频在线观看| 亚洲欧洲精品成人久久奇米网| 欧美日韩国产免费一区二区三区| 国产一区二区三区免费| 免费无码婬片aaa直播表情| 在线综合亚洲欧洲综合网站| 亚洲视频在线不卡| 亚洲视频精品在线| 亚洲精品无码久久久久| 亚洲一级黄色视频| 又黄又爽一线毛片免费观看| 全免费一级午夜毛片| 免费能直接在线观看黄的视频| 久久精品免费观看| 免费在线黄色电影| 久久久久久噜噜精品免费直播| 免费一级做a爰片久久毛片潮| 亚洲妇女无套内射精| 亚洲国产精品无码第一区二区三区| 亚洲六月丁香婷婷综合| 亚洲白嫩在线观看| 亚洲美女视频免费| 亚洲精品亚洲人成在线观看麻豆 | 亚洲精品福利你懂| 亚洲春色另类小说| 亚洲网站视频在线观看| 亚洲AV无码成人精品区在线观看| 黑人精品videos亚洲人| 免费一级毛片在线播放视频免费观看永久 | 成人无遮挡毛片免费看| 一二三四免费观看在线视频中文版 | 免费看的黄色大片| 欧洲精品免费一区二区三区| 热久久精品免费视频| 日本不卡在线观看免费v| 国产嫩草影院精品免费网址| 四虎永久在线精品免费影视| 免费国产高清视频|