Hibernate是一款ORM關系映射框架+SPRing是結合第三方插件的大雜燴,Hibernate+Spring整合開發效率大大提升。
整合開發步驟如下:
第一步:導入架包:
1、Hibernate基礎包+Spring基礎包(AOP代理包和cglib...)
第二步:在spring配置文件中配置datasource(數據庫連接信息要么寫在hibernate.cfg.xml中;要么寫在datasource中)
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 設置類掃描器;自動裝配Bean --> <context:component-scan base-package="com.msit.ssh.sh" /> <!-- 引入外部屬性文件 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <!-- 配置數據源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <!-- 配置數據源信息 --> <property name="url"> <value>${connection.url}</value> </property> <property name="driverClassName"> <value>${connection.driver_class}</value> </property> <property name="username"> <value>${connection.username}</value> </property> <property name="passWord"> <value>${connection.password}</value> </property> <!-- 最大連接數 --> <property name="maxActive"> <value>${jdbc.maxactive}</value> </property> <!-- 最大空閑數 --> <property name="maxIdle"> <value>${jdbc.maxidle}</value> </property> <!-- 最小空閑數 --> <property name="minIdle"> <value>${jdbc.minidle}</value> </property> </bean> <!-- sessionFactory(管理hibernate sessionfactory) --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入數據源 --> <property name="dataSource" ref="dataSource" /> <!-- 第一種方式:引入hibernate.cfg.xml --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 第二種方式:所有的hibernate配置配置在spring中 --> <!-- 配置映射文件 --> <!-- <property name="mappingDirectoryLocations"> <list> <value>com/msit/ssh/sh/entity/User.hbm.xml</value> </list> </property> --> <!-- 配置其他選項 --> <!-- <property name="hibernateProperties"> <props> <prop key=""></prop> <prop key="show_sql">true</prop> <prop key="hbm2ddl.auto">update</prop> </props> </property> --> </bean> <!-- 配置hibernate事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <!-- 注入sessionFactory --> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 事務通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 配置哪些方法需要用到事務;哪些方法不需要事務 --> <tx:method name="*" /> <tx:method name="get*" propagation="NOT_SUPPORTED" /> </tx:attributes> </tx:advice> <!-- 配置aop --> <aop:config> <!-- 配置切入點 --> <aop:pointcut id="txPointcut" expression="execution(* com.msit.ssh.sh.service.impl.*.*(..))" /> <!-- 配置事務通知 --> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut" /> </aop:config> <!-- <bean class="org.springframework.orm.hibernate3.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property></bean> --> <!-- <bean id="hibernatedaosuppert" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userdao" class="com.msit.ssh.sh.dao.impl.UserDaoImpl" parent="hibernatedaosuppert"> </bean> --></beans>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration> <session-factory> <!-- 數據庫連接信息 --> <!-- <property name="connection.url">jdbc:Oracle:thin:@localhost:1521:orcl</property> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property> <property name="connection.username">db2</property> <property name="connection.password">db2</property> --> <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property> <!-- 其他配置 --> <property name="show_sql">true</property> <property name="hbm2ddl.auto">update</property> <mapping resource="com/msit/ssh/sh/entity/User.hbm.xml"/> <!-- <class-cache class="org.hibernate.test.legacy.Simple" region="Simple" usage="read-write"/> --> </session-factory></hibernate-configuration>
3、配置sessionfactory(hibernate交給sprig管理)
里邊注入數據源(datasource);再把hibernate配置文件引入進來 或者hibernate所有配置都寫在sessionfactory中(舍棄了hibernate配置文件) <!-- sessionFactory(管理hibernate sessionfactory) --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 注入數據源 --> <property name="dataSource" ref="dataSource" /> <!-- 引入hibernate.cfg.xml --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean>
4、dao層繼承HibernateDaoSuppert(抽象類)(不能用注解);必須要標明abstract="true"
如果使用此方式: <bean id="hibernatedaosuppert" class="org.springframework.orm.hibernate3.support.HibernateDaoSupport" abstract="true"> //將sessionFactory注入給HibernateDaoSuppert <property name="sessionFactory" ref="sessionFactory"></property> </bean> //dao層Bean必須配置parent="hibernatedaosuppert" <bean id="userdao" class="com.msit.ssh.sh.dao.impl.UserDaoImpl" parent="hibernatedaosuppert"> </bean> 還想用注解怎么辦? //編寫一個超類繼承HibernateDaoSuppert public class BaseHibernateDaoSuppert extends HibernateDaoSupport{ @Resource //注入sessionFactory public void setMySessionFactory(SessionFactory sessionFactory){ this.setSessionFactory(sessionFactory); } } //dao層就繼承超類 @Repository("userdao") public class UserDaoImpl extends BaseHibernateDaoSuppert implements IUserDao
您可以通過點擊 右下角 的按鈕 來對文章內容作出評價, 也可以通過左下方的 關注按鈕 來關注我的博客的最新動態。 如果文章內容對您有幫助, 不要忘記點擊右下角的 推薦按鈕 來支持一下哦 如果您對文章內容有任何疑問, 可以通過評論或發郵件的方式聯系我: 2276292708@QQ.com如果需要轉載,請注明出處,謝謝!!
新聞熱點
疑難解答