Best Practice of Spring Framework

xiaoxiao2021-03-06  13

Spring framework is the most famous is the seamless link with Hibernate. It is basically used to use hibernate. Unfortunately, the HibernateTemplate function provided by Spring is not enough. It is not very convenient to use. When we program, you generally write businessService first, by BusinessService Tune DAO to perform storage, Spring does not have a good example in this area, causing truth to use it, it is not easy.

Our idea is to write a basedao, imitation of HibernateTemplate, and implement all the basic functions:

Public class basedaosupport {

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);}}

...

Other DAOs, inherited from Basedao, write other DAOs, and there will be few code.

Inheriting from Basedao Entitydao, it is more convenient to be responsible for the basic operation of general entities.

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 intenceptor * / public class entitydaoimpl extends basedao imports entitydao {}

For the control of Transaction, the way AOP is used:

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 entry service. User sping aop intencer * / public class entitymanagerImpl imports 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);} ...

}

In this way, we have a universal Hibernate entity engine that achieves basic increase, modification, deletion, query, etc. for any hibernate entity.

Other BusinessService can inherit EntityManager and quickly implement business logic.

The specific XML configuration is as follows:

<-! Oracle JNDI DataSource for J2EE environments -> java: comp / env / jdbc / TestPool

user-hbm.xml net.sf.hibernate.diaalect.raclediaalect net.sf.ehcache.hibernate.Provider < / Prop> True false < / bean>

com.gpower.services.entity.dao.EntityDao hibernateInterceptor entitydaotarget

->

<-! Transactional proxy for the Application primary business object ->

转载请注明原文地址:https://www.9cbs.com/read-48819.html

New Post(0)