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

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

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

    posts - 33,  comments - 11,  trackbacks - 0

    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 2007-12-20 09:28 方濤升 閱讀(492) 評論(0)  編輯  收藏 所屬分類: hibernate

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2007年12月>
    2526272829301
    2345678
    9101112131415
    16171819202122
    23242526272829
    303112345

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费国产成人午夜私人影视| 91精品国产免费久久久久久青草 | 亚洲国产欧美国产综合一区 | 亚洲毛片在线免费观看| 国产精品99精品久久免费| 亚洲gv猛男gv无码男同短文| 一级a性色生活片久久无少妇一级婬片免费放 | 亚洲精品成人网站在线播放| 一区二区三区观看免费中文视频在线播放| 国产亚洲成AV人片在线观黄桃| 99久久精品毛片免费播放| 亚洲AV无码久久精品色欲| 久久久久久AV无码免费网站| 亚洲美女视频免费| 免费AA片少妇人AA片直播| 久久亚洲精品国产精品婷婷 | 在线免费播放一级毛片 | 亚洲国产高清在线| 香蕉97超级碰碰碰免费公| 国产成人精品日本亚洲网址 | 青柠影视在线观看免费| 7777久久亚洲中文字幕蜜桃| 国产成人无码免费看视频软件| 亚洲精品无码久久久久久| 免费人成在线观看网站品爱网日本| 四虎国产精品永免费| 亚洲人成网7777777国产| 久久成人国产精品免费软件| 四虎亚洲精品高清在线观看| 亚洲国产成人久久综合区| 三级毛片在线免费观看| 亚洲成年人电影在线观看| 全免费A级毛片免费看网站| 一级美国片免费看| 亚洲综合无码一区二区三区| 国产免费黄色大片| 亚欧免费一级毛片| 老湿机一区午夜精品免费福利 | 日韩一级视频免费观看| 丝瓜app免费下载网址进入ios| 亚洲三级视频在线|