<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-72  評論-20  文章-0  trackbacks-1

    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配置如下:

    <!-- Oracle JNDI DataSource for J2EE environments -->
     
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
      
    <property name="jndiName"><value>java:comp/env/jdbc/testPool</value></property>
     
    </bean>

     
    <!-- Hibernate SessionFactory for Oracle -->
     
    <!-- Choose the dialect that matches your "dataSource" definition -->
     
    <bean id="sessionFactory" class="org.springframework.orm.hibernate.LocalSessionFactoryBean">
      
    <property name="dataSource"><ref local="dataSource"/></property>
      
    <property name="mappingResources">
       
    <value>user-hbm.xml</value>
      
    </property>
      
    <property name="hibernateProperties">
       
    <props>
        
    <prop key="hibernate.dialect">net.sf.hibernate.dialect.OracleDialect</prop>
        
    <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.Provider</prop>
        
    <prop key="hibernate.cache.use_query_cache">true</prop>
                      
    <prop key="hibernate.show_sql">false</prop>
       
    </props>
      
    </property>
     
    </bean>

     
    <!-- AOP DAO Intecepter -->
            
    <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate.HibernateInterceptor">
              
    <property name="sessionFactory">
                
    <ref bean="sessionFactory"/>
              
    </property>
            
    </bean>

            
    <bean id="entityDaoTarget" class="com.gpower.services.entity.dao.EntityDaoImpl">
              
    <property name="sessionFactory">
                
    <ref bean="sessionFactory"/>
              
    </property>
            
    </bean>

            
    <bean id="entityDao" class="org.springframework.aop.framework.ProxyFactoryBean">
              
    <property name="proxyInterfaces">
                
    <value>com.gpower.services.entity.dao.EntityDao</value>
              
    </property>
              
    <property name="interceptorNames">
                
    <list>
                  
    <value>hibernateInterceptor</value>
                  
    <value>entityDaoTarget</value>
                
    </list>
              
    </property>
            
    </bean>

     
    <!-- Transaction manager for a single Hibernate SessionFactory (alternative to JTA) -->
     
    <bean id="transactionManager" class="org.springframework.orm.hibernate.HibernateTransactionManager">
      
    <property name="sessionFactory"><ref local="sessionFactory"/></property>
     
    </bean>

     
    <!-- Transaction manager that delegates to JTA (for a transactional JNDI DataSource) -->
     
    <!--
     <bean id="transactionManager" class="org.springframework.transaction.jta.JtaTransactionManager"/>
     
    -->

     
    <!-- Transactional proxy for the Application primary business object -->
            
    <bean id="entityManagerTarget" class="com.gpower.services.entity.EntityManagerImpl">
              
    <property name="entityDao">
                
    <ref bean="entityDao"/>
              
    </property>
            
    </bean>
            
    <bean id="entityManager" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
              
    <property name="transactionManager">
                
    <ref bean="transactionManager"/>
              
    </property>
              
    <property name="target">
                
    <ref bean="entityManagerTarget"/>
              
    </property>
              
    <property name="transactionAttributes">
         
    <props>
           
    <prop key="get*">PROPAGATION_SUPPORTS</prop>
           
    <prop key="*">PROPAGATION_REQUIRED</prop>
         
    </props>
              
    </property>
            
    </bean>
    posted on 2008-08-14 15:15 前方的路 閱讀(260) 評論(0)  編輯  收藏 所屬分類: Java技術
    主站蜘蛛池模板: 久99久无码精品视频免费播放| 7777久久亚洲中文字幕蜜桃| 亚洲精品av无码喷奶水糖心| 91成人免费在线视频| 亚洲国产精品乱码在线观看97 | 亚洲精品视频免费| 日本一区二区三区在线视频观看免费 | 亚洲国产成人久久精品app| 91精品免费高清在线| 亚洲日韩中文字幕天堂不卡 | 免费h成人黄漫画嘿咻破解版| 国产大陆亚洲精品国产| 免费a级毛片无码a∨性按摩| 人与动性xxxxx免费| 日本亚洲视频在线| 国产91色综合久久免费分享| 亚洲中文字幕无码中文| 免费又黄又爽又猛的毛片| 久青草视频在线观看免费| 亚洲一区二区影院| 中文字幕无码免费久久99 | 中国一级全黄的免费观看| 亚洲av一综合av一区| 免费三级毛片电影片| 另类图片亚洲校园小说区| 亚洲永久精品ww47| 国产91免费在线观看| 窝窝影视午夜看片免费| 亚洲精品在线观看视频| 久久亚洲精品视频| 成人免费视频77777| 在线播放国产不卡免费视频| 亚洲精品亚洲人成在线麻豆| 国产免费久久精品久久久| 99久久精品毛片免费播放| 亚洲一区二区三区深夜天堂| 亚洲视频一区二区| 精品女同一区二区三区免费站| 精品久久久久久亚洲中文字幕 | 亚洲短视频在线观看| 亚洲成av人片一区二区三区|