李顺利
随笔-50  评论-170  文章-0  trackbacks-0

使用配置文件对DAO层封装具有分页功能的S2SH整合实例

 

李顺利

2010312

关键词

使用配置文件对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-03-12 10:05 李顺利 阅读(4065) 评论(6)  编辑  收藏

评论:
# re: 使用配置文件对DAO层封装具有分页功能的S2SH整合实例 2010-04-03 15:16 | Love
谢谢  回复  更多评论
  
# re: 使用配置文件对DAO层封装具有分页功能的S2SH整合实例[未登录] 2010-05-11 18:40 | randy
我刚学,实在不明白怎么对业务层配置事务,就是在一个service层中,当调用多个dao时,如果其中一个dao出错,会回滚整个service中调用的dao操作?  回复  更多评论
  
# re: 使用配置文件对DAO层封装具有分页功能的S2SH整合实例 2010-05-11 20:09 | 李顺利
@randy
到google搜索一下“spring 事务 service”,应该有很详细的解答
Spring的事务管理功能可以做到细粒度的  回复  更多评论
  
# re: 使用配置文件对DAO层封装具有分页功能的S2SH整合实例[未登录] 2010-05-11 22:03 | Randy
@李顺利
谢谢,我去找找看。  回复  更多评论
  
# re: 使用配置文件对DAO层封装具有分页功能的S2SH整合实例 2010-06-08 09:05 | 威尔
有一个问题想问你?
你用的是spring3.0 ,HibernateCallback支持泛型
可spring2.5,HibernateCallback不支持泛型
很郁闷 不知怎么解决?
  回复  更多评论
  
# re: 使用配置文件对DAO层封装具有分页功能的S2SH整合实例 2011-09-08 16:33 | 淘宝网女装冬装
下载了,大半年没搞JAVA了  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: