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

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

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

    軟件藝術思考者  
    混沌,彷徨,立志,蓄勢...
    公告
    日歷
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    隨筆分類(86)

    隨筆檔案(85)

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

     

    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>


    Spring Framework之最佳實踐二
    posted on 2006-07-05 16:38 智者無疆 閱讀(1642) 評論(2)  編輯  收藏 所屬分類: about spring
    評論:
    • # re: Spring Framework之最佳實踐二   白白 Posted @ 2006-07-05 16:54
      我發現javascript源碼大全挺好,里面的代碼都是正確的,我試了幾個都可以,呵呵,老公,你真聰明啊!  回復  更多評論   

    • # re: Spring Framework之最佳實踐二   白白 Posted @ 2006-07-10 15:12
      我時刻關注著你的博客,點擊率還蠻高的嘛。比我們公司的網站還要厲害!!!呵呵,我喜歡啦。  回復  更多評論   

     
    Copyright © 智者無疆 Powered by: 博客園 模板提供:滬江博客


       觀音菩薩贊

    主站蜘蛛池模板: 亚洲色成人中文字幕网站| 成人毛片18女人毛片免费96 | 在线看无码的免费网站| 亚洲日韩精品一区二区三区无码 | 精品免费久久久久国产一区 | 中文字幕无码免费久久99| 亚洲国产精品综合久久网各| 91免费国产自产地址入| 亚洲AV综合色区无码二区偷拍 | 永久免费av无码不卡在线观看| 亚洲国产福利精品一区二区| 成年女人免费v片| 亚洲妇女无套内射精| 亚洲福利中文字幕在线网址| eeuss影院www天堂免费| 亚洲免费人成在线视频观看| 91香蕉在线观看免费高清| 亚洲国产美女在线观看| 成年女人视频网站免费m| 国产精品亚洲精品爽爽| 亚洲性猛交XXXX| 日本免费xxxx| 在线观看亚洲免费| 亚洲色自偷自拍另类小说| 97青青草原国产免费观看| 亚洲免费综合色在线视频| 亚洲视频一区二区| 99视频在线看观免费| 亚洲国产成人无码AV在线| 久久精品国产亚洲精品| 1000部羞羞禁止免费观看视频| 亚洲欧美精品午睡沙发| 亚洲日韩v无码中文字幕| 黄+色+性+人免费| 牛牛在线精品观看免费正| 亚洲AV成人片色在线观看| 免费鲁丝片一级观看| 99在线热播精品免费99热| 久久精品国产亚洲αv忘忧草| 亚洲高清免费视频| 88xx成人永久免费观看|