本文實例講述了Spring和Hibernate的整合操作。分享給大家供大家參考,具體如下:
一 web配置
<?xml version="1.0" encoding="GBK"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping></web-app>
二 applicationContext.xml
<?xml version="1.0" encoding="GBK"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"> <!-- 定義數據源Bean,使用C3P0數據源實現,并注入數據源的必要信息 --> <bean class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="com.mysql.jdbc.Driver" p:jdbcUrl="jdbc:mysql://localhost/spring" p:user="root" p:password="32147" p:maxPoolSize="40" p:minPoolSize="2" p:initialPoolSize="2" p:maxIdleTime="30"/> <!-- 定義Hibernate的SessionFactory,SessionFactory需要依賴數據源,注入dataSource --> <bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" p:dataSource-ref="dataSource"> <!-- mappingResources用來列出全部映射文件 --> <property name="annotatedClasses"> <list> <!-- 以下用來列出所有的PO類--> <value>org.crazyit.booksys.domain.Book</value> </list> </property> <!-- 定義Hibernate SessionFactory的屬性 --> <property name="hibernateProperties"> <props> <!-- 指定Hibernate的連接方言 --> <prop key="hibernate.dialect"> org.hibernate.dialect.MySQL5InnoDBDialect</prop> <!--是否根據Hiberante映射創建數據表 --> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean> <!-- 定義Service組件,并將DAO組件注入Service組件 --> <bean class="org.crazyit.booksys.service.impl.BookServiceImpl" p:bookDao-ref="bookDao"/> <!-- 定義DAO組件,并將SessionFactory注入DAO組件 --> <bean class="org.crazyit.booksys.dao.impl.BookDaoHibernate4" p:sessionFactory-ref="sessionFactory"/> <!-- 配置Hibernate的局部事務管理器,使用HibernateTransactionManager類 --> <!-- 該類是PlatformTransactionManager接口針對采用Hibernate的特定實現類 --> <!-- 配置HibernateTransactionManager需依賴注入SessionFactory --> <bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory"/><!-- 配置事務增強處理Bean,指定事務管理器 --><tx:advice transaction-manager="transactionManager"> <!-- 用于配置詳細的事務定義 --> <tx:attributes> <!-- 所有以'get'開頭的方法是read-only的 --> <tx:method name="get*" read-only="true"/> <!-- 其他方法使用默認的事務設置,指定超時時長為5秒 --> <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" timeout="5"/> </tx:attributes></tx:advice><!-- AOP配置的元素 --><aop:config> <!-- 配置一個切入點 --> <aop:pointcut expression="bean(bookService)"/> <!-- 指定在myPointcut切入點應用txAdvice事務增強處理 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/></aop:config></beans>
新聞熱點
疑難解答