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

使用配置文件对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" />

       

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值