Spring+Hibernate統合
6326 ワード
以前の記事を通して、SpringとStrutsの統合案を紹介しました.Springは開放性が良いので、Strutsと統合できるだけでなく、ORMのほとんどの枠組みとうまく統合できます.次にSpringとHibernateの統合をまとめます.主な流れは以下の通りです.
*セットアップSession Factory
*事務マネージャの設定
*事務の伝播特性の設定
1 Session Factoryを配置します.
Hybernateによる恒久層訪問において、HibernateのSession Factoryは非常に重要なオブジェクトであり、単一のデータベースマッピング関係をコンパイルしたメモリミラーです.ほとんどの場合、一つのJ 2 EEアプリケーションは一つのデータベース、すなわち一つのSession Factoryオブジェクトに対応する.
純粋なHibernateアクセスでは、アプリケーションは手動でSession Factoryのインスタンスを作成する必要がありますが、これは良いスキームではありません.実際の開発では、Session Factoryの例をステートメント的に管理し、Session Factoryのインスタンスを直接構成ファイルで管理することができます.SpringのIoC容器は、より良い管理方式を提供し、声明的にSession-Factoryのインスタンスを構成します.
プロファイル:appication Contront-common.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- Hibernate -->
<property name="configLocation">
<!-- ClassPath . classpath , http, classpath . -->
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
SpringのIoC容器にSession FactoryBeanを配置すると、アプリケーションの起動に伴ってロードされ、IoC容器の機能を十分に利用して、DAOコンポーネントなどのSession FactoryBenを任意のBeanに注入することができる.DAOコンポーネントがSession Factory Beanの参照を得ると、実際のデータベースアクセスが完了します.2事務管理器と伝播特性の設定
Springは非常に簡潔な声明式事務制御を提供しており、設定ファイルに事務制御セグメントを追加するだけで、業務ロジックコードは何も変更する必要がない.Springの宣言式事務論理は、異なる事務戦略の間で切り換えることもサポートしています.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd" >
<!-- SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- Hibernate -->
<property name="configLocation">
<!-- ClassPath . classpath , http, classpath . -->
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<!-- -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- SessionFactory -->
<property name="sessionFactory">
<!-- SessionFactory -->
<ref bean="sessionFactory" />
</property>
</bean>
<!--Aspect -->
<aop:config>
<!-- -->
<aop:pointcut id="allManagerMethod" expression="execution(* com.tgb.drp.service.*.*(..))" />
<!-- advice pointcut -->
<aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/>
</aop:config>
<!-- -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- -->
<tx:attributes>
<!-- add -->
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<!-- -->
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
</tx:attributes>
</tx:advice>
</beans>
3、業務ロジック方法を編纂し、Daoコンポーネントを実現する.
Springは一連の抽象類を提供しています.これらの抽象は応用中のDAO実現類の親類として利用されます.これらの抽象的なクラスを継承することにより、SpringはDAOの開発ステップを簡略化し、データベースアクセス技術を一貫して使用することができます.下の階にはJDBC、JDO、またはHybernateを採用していますが、アプリケーションでは一貫したプログラミングモデルを採用することができます.応用するDAO類はこれらの抽象類を継承し、応用の開発を大いに簡略化します.最大の利点は、これらの抽象的なクラスを継承したDAOがデータベースに一貫してアクセスできることであり、アプリケーションが異なる耐久層アクセス技術において切り替えられることを意味する.
Springは同じアクセスモード、異なるデータベースアクセス技術を使用することができます.Hibernateの持続層アクセス技術に関して、Springは、DAOコンポーネントの実現をサポートするために、以下の3つのツール類(またはインターフェース)を提供する.
ハイベルナーDao Support
ハイベルナントTemplate
ハイベルナターCallBack
以下のコードは主に増加と検索機能の展示を行い、ItemDaoImplはHbernate DaoSupport類を継承し、Hbernature Templateを用いて耐久化しています.
public class ItemDaoImpl extends HibernateDaoSupport implements ItemDao {
//
public void addItem(Item item) {
getHibernateTemplate().save(item);
}
//
public Item findItemById(String itemNo) {
return (Item) getHibernateTemplate().load(Item.class, itemNo);
}
}
以上の内容はSpringとHibernateの核心統合内容です.Spring+StrutsとSpring+Hibergの統合によって、Springが特殊なキャラクター位置にあることが分かります.彼の開放性によって他のフレームと統合することができます.各フレームの整合を熟練することで、各フレームの役割と彼らの関係をもっと理解してください.