Chinaunix
标题:
泛型DAO在EasyJWEb中的应用(一)
[打印本页]
作者:
guoguanzhao
时间:
2007-12-14 16:51
标题:
泛型DAO在EasyJWEb中的应用(一)
在我们开发的过程中大多数的代码都是用来完成增删改查的操作,这样一来重复的代码就是很多了,也会花费很长的时间来写这个代码,如果我们把这写重复的代码有一个通用的类来完成不是更好的做法吗?所以在EasyJWeb中有一个通用的类来完成这些操作就是GenericDAO,它是一个接口用来定义通用增删改查的操作
如果你要是使用GenericDAO的话,你的所用DAO只需要继承它就可以了,同时在Spring中配置你的DAO所依赖的domain就可以了。
如:在EasyJWeb的开源论坛中一个关于论坛目录的DAO,如下:
package com.easyjf.bbs.dao;
import com.easyjf.bbs.domain.BBSDir;
import com.easyjf.core.dao.GenericDAO;
public interface IBBSDirDAO extends
GenericDAO {
}
在Spring的配置用一下
DAO的配置如下:
com.easyjf.bbs.dao.IBBSDirDAO
com.easyjf.bbs.domain.BBSDir
别的就不用做了,你在service层直接调用该DAO就可以了。一定会有人对这个配置不解。下面就让我们来看看具体的代码:
在EasyJWeb的源代码中有一个EasyJWeb1.0\src\ext\src\main\resources目录下面有一个jpa-generic-dao.xml文件,这个文件通过Spring的IOC注入了具体的参数。清单如下:
不过这个带代码你先不用了解,后面将会说到:
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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
property name="driverClassName"
value="${database.driverClassName}"
/>
property name="url" value="${database.url}"
/>
property name="username" value="${database.username}" />
property name="password" value="${database.password}" />
bean>
bean name="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
property name="persistenceXmlLocation"
value="classpath:persistence.xml"
/>
property name="dataSource" ref="dataSource" />
property name="jpaVendorAdapter">
bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
property name="database"
value="MYSQL" />
property name="showSql"
value="false" />
property name="generateDdl"
value="false" />
bean>
property>
bean>
bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager">
property name="entityManagerFactory"
ref="entityManagerFactory"
/>
property name="dataSource" ref="dataSource" />
bean>
bean abstract="true" id="baseDAO"
class="com.easyjf.core.dao.impl.GenericDAOImpl">
property name="entityManagerFactory"
ref="entityManagerFactory"
/>
bean>
bean id="i18nInteceptor"
class="com.easyjf.core.i18n.I18nInteceptor"
/>
bean id="abstractDao" abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean">
bean>
bean id="abstractI18nDao" abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean">
property name="interceptorNames">
list>
value>i18nInteceptorvalue>
list>
property>
bean>
bean id="queryService"
class="com.easyjf.core.service.impl.QueryServiceImpl">
property name="dao">
bean parent="baseDAO">
constructor-arg>
value>java.lang.Objectvalue>
constructor-arg>
bean>
property>
bean>
bean id="jpaPoLoader"
class="com.easyjf.core.dao.impl.JpaPOLoaderImpl">
property name="entityManagerFactory"
ref="entityManagerFactory"
/>
bean>
beans>
这个相关配置是为DAO配置服务的。再回过头来看DAO的配置:
com.easyjf.bbs.dao.IBBSDirDAO
com.easyjf.bbs.domain.BBSDir
你可以看到改Bean继承了abstractDao的bean,该Bean在jpa-generic-dao.xml配置如下:
bean id="abstractDao" abstract="true"
class="org.springframework.aop.framework.ProxyFactoryBean">
bean>
这个用过Spring的人一定明白吧!是Spring的一定代理类,这个类的代理接口只要指向我们需要的DAO。
代理目标了指向了baseDAO的bean了。看看baseDAO在jpa-generic-dao.xml在的配置:
bean abstract="true" id="baseDAO"
class="com.easyjf.core.dao.impl.GenericDAOImpl">
property name="entityManagerFactory"
ref="entityManagerFactory"
/>
bean>
可以看出baseDAO需要两个参数一个是同过属性entityManagerFactory注入,一个是通过构造函数注入domain,即BBSDir
下面是具体的GenericDAO接口的实现代码:我想用过ORM的人都是可以看得懂的。
[/url]
本文来自ChinaUnix博客,如果查看原文请点:
[url]http://blog.chinaunix.net/u/20053/showart_442444.html
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2