spring+hibernate整合(基本配置)

1導包

導入spring和hibernate相關的jar包。

spring的jar包:spring.jar,spring-aop.jar,spring-asm.jar,spring-beans.jar,spring-context.jar,spring-context-support.jar,spring-core.jar,spring-expression.jar,spring- jdbc.jar,spring-jms.jar,spring-web.jar,spring-tx.jar等

spring配置中如果有事務還需要導入aspectjweaver.jar

hibernate的jar包:hibernate3.jar,hibernate-annotations.jar,hibernate-cglib-repack-2.1_3.jar,hibernate-commons-annotations.jar,hibernate-entitymanager.jar等

一般需要些hql語句,則需要導入antlr-2.7.6.jar,來檢查和識別hql的語法

如果使用mysql 還需要導入mysql-connector-java-5.1.22-bin.jar

如果使用oracle則需要導入ojdbc14.jar

如果涉及json的使用則需要導入json-lib-2.4-jdk15.jar

還有一些jar包很有可能需要導入:

commons-beanutils-1.7.0.jar 動態的獲取/設值Java Bean的屬性 
commons-chain-1.1.jar 實現責任鏈設計模式的Java 類庫 
commons-codec-1.3.jar 用來處理常用的編碼方法的工具類包,例如DES、SHA1、MD5、Base64等等 
commons-collections-3.1.jar 對標準java Collection的擴展 
commons-collections.jar 對標準java Collection的擴展 
commons-digester-1.8.jar 用於處理struts-config.xml配置文件 
commons-fileupload-1.1.1.jar struts上傳文件 
commons-httpclient-3.1.jar 用來簡化HTTP客戶端與服務器端進行各種通信編程實現 
commons-io-1.1.jar 針對java.io.InputStream和Reader進行了擴展 
commons-lang-2.4.jar 對java.lang.*的擴展 
commons-logging-1.1.1.jar 日誌包 
commons-pool-1.3.jar 實現對象池化框架 
commons-validator-1.3.1.jar 用來把驗證規則程序提取出來,以供重複使用

以上jar包概括的不一定全面,可能還需要其他的jar包,如果不知道還需要什麼jar包的話,可以啓動工程,看console的異常提示,會提示缺少設 什麼jar包,小編也是使用這笨方法一步一步加的jar包,希望讀者即時指正,以助於相互學習。

2整合

javaweb在開始學習的時候一般是spring配置和hibernate配置分開獨立的,但是實際工作中,一般都是整合在一起的。

下面是我整合的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 開啓註解掃描 -->
       <context:component-scan base-package="com.*" />
       <!-- 數據庫連接 -->
       <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" name="dataSource2">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/moffice2</value>
</property>
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mappingResources">
<list>
<value>com/matrix/ztree/model/CatalogInfo.hbm.xml</value>
</list>
</property>

<!-- 配置數據庫方言 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
</props>
</property>
</bean>

<!-- 配置事務 -->
<!-- 生命事務管理,採用aop切入 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事務傳播性 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />

</tx:attributes>
</tx:advice>
<!-- 配置參與事務的類 -->
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="txAdvice" pointcut="within(com.matrix.*)" />
</aop:config>
</beans>

以上配置spring+hibernate就配置完成了,其中 註解掃描配置後,就可以在mvc模式中使用註解標記;在sessionFactory的bean中配置 xxx.hbm.xml就相當於在java中使用new Configuration("xxx.hbm.xml的路徑"),就是我們平時說的javabean.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章