Spring Framework之最佳实践二
来源:百度文库 编辑:神马文学网 时间:2024/04/30 04:32:14
Spring Framework最得以出名的是与Hibernate的无缝链接,基本上用Spring,就会用Hibernate。可惜的是Spring提供的HibernateTemplate功能显得不够,使用起来也不是很方便。我们编程序时,一般先写BusinessService,由BusinessService调DAO来执行存储,在这方面Spring没有很好的例子,造成真正想用好它,并不容易。
我们的思路是先写一个BaseDao,仿照HibernateTemplate,将基本功能全部实现:
public class BaseDao extends HibernateDaoSupport{
private Log log = LogFactory.getLog(getClass());
public Session openSession() {
return SessionFactoryUtils.getSession(getSessionFactory(), false);
}
public Object get(Class entityClass, Serializable id) throws DataAccessException {
Session session = openSession();
try {
return session.get(entityClass, id);
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
public Serializable create(Object entity) throws DataAccessException {
Session session = openSession();
try {
return session.save(entity);
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
...
其它的DAO,从BaseDao继承出来,这样写其他的DAO,代码就会很少。
从BaseDao继承出来EntityDao,专门负责一般实体的基本操作,会更方便。
public interface EntityDao {
public Object get(Class entityClass, Serializable id) throws DataAccessException;
public Object load(Class entityClass, Serializable id) throws DataAccessException;
public Serializable create(Object entity) throws DataAccessException;
...}
/**
* Base class for Hibernate DAOs. This class defines common CRUD methods for
* child classes to inherit. User Sping AOP Inteceptor
*/
public class EntityDaoImpl extends BaseDao implements EntityDao{
}
为了Transaction的控制,采用AOP的方式:
public interface EntityManager {
public Object get(Class entityClass, Serializable id);
public Object load(Class entityClass, Serializable id);
public Serializable create(Object entity);
...
}
/**
* Base class for Entity Service. User Sping AOP Inteceptor
*/
public class EntityManagerImpl implements EntityManager {
private EntityDao entityDao;
public void setEntityDao(EntityDao entityDao) {
this.entityDao = entityDao;
}
public Object get(Class entityClass, Serializable id) {
return entityDao.get(entityClass, id);
}
public Object load(Class entityClass, Serializable id) {
return entityDao.load(entityClass, id);
}
...
}
这样我们就有了一个通用的Hibernate实体引擎,可以对任何Hibernate实体实现基本的增加、修改、删除、查询等。
其它的BusinessService就可以继承EntityManager,快速实现业务逻辑。
具体XML配置如下:
java:comp/env/jdbc/testPool
user-hbm.xml
net.sf.hibernate.dialect.OracleDialect
net.sf.ehcache.hibernate.Provider
true
false
com.gpower.services.entity.dao.EntityDao
PROPAGATION_SUPPORTS
PROPAGATION_REQUIRED
我们的思路是先写一个BaseDao,仿照HibernateTemplate,将基本功能全部实现:
public class BaseDao extends HibernateDaoSupport{
private Log log = LogFactory.getLog(getClass());
public Session openSession() {
return SessionFactoryUtils.getSession(getSessionFactory(), false);
}
public Object get(Class entityClass, Serializable id) throws DataAccessException {
Session session = openSession();
try {
return session.get(entityClass, id);
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
public Serializable create(Object entity) throws DataAccessException {
Session session = openSession();
try {
return session.save(entity);
}
catch (HibernateException ex) {
throw SessionFactoryUtils.convertHibernateAccessException(ex);
}
}
...
其它的DAO,从BaseDao继承出来,这样写其他的DAO,代码就会很少。
从BaseDao继承出来EntityDao,专门负责一般实体的基本操作,会更方便。
public interface EntityDao {
public Object get(Class entityClass, Serializable id) throws DataAccessException;
public Object load(Class entityClass, Serializable id) throws DataAccessException;
public Serializable create(Object entity) throws DataAccessException;
...}
/**
* Base class for Hibernate DAOs. This class defines common CRUD methods for
* child classes to inherit. User Sping AOP Inteceptor
*/
public class EntityDaoImpl extends BaseDao implements EntityDao{
}
为了Transaction的控制,采用AOP的方式:
public interface EntityManager {
public Object get(Class entityClass, Serializable id);
public Object load(Class entityClass, Serializable id);
public Serializable create(Object entity);
...
}
/**
* Base class for Entity Service. User Sping AOP Inteceptor
*/
public class EntityManagerImpl implements EntityManager {
private EntityDao entityDao;
public void setEntityDao(EntityDao entityDao) {
this.entityDao = entityDao;
}
public Object get(Class entityClass, Serializable id) {
return entityDao.get(entityClass, id);
}
public Object load(Class entityClass, Serializable id) {
return entityDao.load(entityClass, id);
}
...
}
这样我们就有了一个通用的Hibernate实体引擎,可以对任何Hibernate实体实现基本的增加、修改、删除、查询等。
其它的BusinessService就可以继承EntityManager,快速实现业务逻辑。
具体XML配置如下:
Spring Framework之最佳实践二
Spring Framework之最佳实践一
Spring Framework中的AOP编程之入门篇
Spring Framework中的AOP编程之入门篇 2
Spring Framework中的AOP编程之入门篇 3
Spring Framework中的AOP编程之入门篇
Spring Framework介紹
Introducing to Spring Framework
Spring Framework 开发参考手册
Spring Framework 开发参考手册
Spring Framework 开发参考手册
Spring Framework介紹
Spring Framework 开发参考手册
Spring Framework中的AOP编程之入门篇-开发频道-Spring-天极网
Spring:总结用XML配置的十二个最佳实践
Spring - Java/J2EE Application Framework
Spring - Java/J2EE Application Framework
12.2.?Hibernate - Spring Framework reference ...
Spring Framework标记库初学者指南
Introducing to Spring Framework(中文修订版)
JMS Subscription [Archive] - Spring Framework Support Forums
Spring:总结用XML配置的十二个最佳实践-Java-CSDN
Exception 处理之最佳实践-天下无名
VC++ ADO开发实践之二