总体架构:
[img]http://dl.iteye.com/upload/attachment/0070/5025/0752a9f3-58de-3e09-abd3-0fe65ccae950.png[/img]
1.定义泛型DAO接口:
package com.integration.framework.dao;
import java.io.Serializable;
import java.util.List;
/**
* @ClassName:IBaseGenericDAO
* @Description:TODO(IBaseGenericDAO DAO层泛型接口,定义基本的DAO功能 )
* @param 实体类
* @param 主键类,必须实现Serializable接口
* @author zj
* @date:Jul 9, 2012 1:32:07 PM
* @version V1.0
*/
public abstract interface IBaseGenericDAO {
/**
* 按主键取记录
* @param id 主键值
* @return 记录实体对象,如果没有符合主键条件的记录,则返回null
*/
public abstract T get(PK id);
/**
* 按主键取记录
* @param id 主键值
* @return 记录实体对象,如果没有符合主键条件的记录,则 throw DataAccessException
*/
public abstract T load(PK id);
/**
* 获取全部实体
* @return 返回一个list集合数据
*/
public abstract List loadAll();
/**
* 修改一个实体对象(UPDATE一条记录)
* @param entity 实体对象
*/
public abstract void update(T entity);
/**
* 插入一个实体(在数据库INSERT一条记录)
* @param entity 实体对象
*/
public abstract void save(T entity);
/**
* 增加或更新实体
* @param entity 实体对象
*/
public abstract void saveOrUpdate(T entity);
/**
* 删除指定的实体
* @param entity 实体对象
*/
public abstract void delete(T entity);
}
2.实现IBaseGenericDAO接口的Hibernate的实现类HibernateBaseGenericDAOImpl:
package com.integration.framework.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;
import com.integration.framework.dao.IBaseGenericDAO;
/**
* @ClassName:HibernateBaseGenericDAOImpl
* @Description:TODO(HibernateBaseGenericDAOImpl DAO层泛型基类,实现了基本的DAO功能 利用了Spring的HibernateDaoSupport功能)
* @param 实体类
* @param 主键类,必须实现Serializable接口
* @author zj
* @date:Jul 9, 2012 1:41:37 PM
* @version V1.0
*/
@SuppressWarnings("all")
public class HibernateBaseGenericDAOImpl extends HibernateDaoSupport implements IBaseGenericDAO{
private static Log logger = LogFactory.getLog(HibernateBaseGenericDAOImpl.class);
private Class entityClass;
/**
* 构造方法,根据实例类自动获取实体类类型
*/
public HibernateBaseGenericDAOImpl() {
this.entityClass = null;
Class c = getClass();
Type t = c.getGenericSuperclass();
if (t instanceof ParameterizedType) {
Type[] p = ((ParameterizedType) t).getActualTypeArguments();
this.entityClass = (Class) p[0];
}
}
/*
* (non-Javadoc)
* @see com.integration.framework.dao.IBaseGenericDAO#delete(java.lang.Object)
*/
public void delete(T entity) {
try{
getHibernateTemplate().delete(entity);
}catch(DataAccessException e){
logger.error(e.getMessage(), e);
}
}
/*
* (non-Javadoc)
* @see com.integration.framework.dao.IBaseGenericDAO#get(java.io.Serializable)
*/
public T get(PK id) {
return (T) getHibernateTemplate().get(entityClass, id);
}
/*
* (non-Javadoc)
* @see com.integration.framework.dao.IBaseGenericDAO#load(java.io.Serializable)
*/
public T load(PK id) {
return (T) getHibernateTemplate().load(entityClass, id);
}
/*
* (non-Javadoc)
* @see com.integration.framework.dao.IBaseGenericDAO#loadAll()
*/
public List loadAll() {
return getHibernateTemplate().loadAll(entityClass);
}
/*
* (non-Javadoc)
* @see com.integration.framework.dao.IBaseGenericDAO#save(java.lang.Object)
*/
public void save(T entity) {
try{
getHibernateTemplate().save(entity);
}catch(DataAccessException e){
logger.error(e.getMessage(), e);
}
}
/*
* (non-Javadoc)
* @see com.integration.framework.dao.IBaseGenericDAO#saveOrUpdate(java.lang.Object)
*/
public void saveOrUpdate(T entity) {
try{
getHibernateTemplate().saveOrUpdate(entity);
}catch(DataAccessException e){
logger.error(e.getMessage(), e);
}
}
/*
* (non-Javadoc)
* @see com.integration.framework.dao.IBaseGenericDAO#update(java.lang.Object)
*/
public void update(T entity) {
try{
getHibernateTemplate().update(entity);
}catch(DataAccessException e){
logger.error(e.getMessage(), e);
}
}
}
3.定义Service泛型接口:
package com.integration.framework.service;
import java.io.Serializable;
import java.util.List;
/**
* @ClassName:IBaseGenericService
* @Description:TODO(IBaseGenericService Service层泛型接口,定义基本的Service功能)
* @param 实体类
* @param 主键类,必须实现Serializable接口
* @author zhoujing
* @date:Jul 9, 2012 2:48:56 PM
* @version V1.0
*/
public interface IBaseGenericService {
/**
* 按主键取记录
* @param id 主键值
* @return 记录实体对象,如果没有符合主键条件的记录,则返回null
*/
public abstract T get(PK id);
/**
* 按主键取记录
* @param id 主键值
* @return 记录实体对象,如果没有符合主键条件的记录,则 throw DataAccessException
*/
public abstract T load(PK id);
/**
* 获取全部实体
* @return 返回一个list集合数据
*/
public abstract List loadAll();
/**
* 修改一个实体对象(UPDATE一条记录)
* @param entity 实体对象
*/
public abstract void update(T entity);
/**
* 插入一个实体(在数据库INSERT一条记录)
* @param entity 实体对象
*/
public abstract void save(T entity);
/**
* 增加或更新实体
* @param entity 实体对象
*/
public abstract void saveOrUpdate(T entity);
/**
* 删除指定的实体
* @param entity 实体对象
*/
public abstract void delete(T entity);
}
4.实现Service:
package com.integration.framework.service.impl;
import java.io.Serializable;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.integration.framework.dao.IBaseGenericDAO;
import com.integration.framework.service.IBaseGenericService;
/**
* @ClassName:BaseGenericServiceImpl
* @Description:TODO(这里用一句话描述这个类的作用)
* @author zhoujing
* @date:Jul 9, 2012 5:16:46 PM
* @version V1.0
*/
@Service
@SuppressWarnings("all")
public class BaseGenericServiceImpl implements IBaseGenericService {
@Autowired
private IBaseGenericDAO dao;
public void delete(T entity) {
dao.delete(entity);
}
public T get(PK id) {
return (T)dao.get(id);
}
public T load(PK id) {
return (T) dao.load(id);
}
public List loadAll() {
return dao.loadAll();
}
public void save(T entity) {
dao.save(entity);
}
public void saveOrUpdate(T entity) {
dao.saveOrUpdate(entity);
}
public void update(T entity) {
dao.update(entity);
}
}
5.配置applicationContext:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
default-autowire="byName" default-lazy-init="false">
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
classpath:/db/jdbc.properties
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
${hibernate.dialect}
true
true
auto
true
true 1, false 0, yes '1', no '1'
50
25
true
true
org.hibernate.cache.EhCacheProvider
com.integration
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
6.web.xml配置:
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
Integration
contextConfigLocation
classpath:/context/applicationContext*.xml
log4jConfigLocation
classpath:/log4j.properties
org.springframework.web.util.Log4jConfigListener
struts2
org.apache.struts2.dispatcher.FilterDispatcher
struts2
/*
org.springframework.web.context.ContextLoaderListener
index.jsp