你的位置:首页 > 软件开发 > Java > Hibernate+Spring整合开发步骤

Hibernate+Spring整合开发步骤

发布时间:2015-09-13 14:00:03
Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升。整合开发步骤如下:第一步:导入架包:1、Hibernate基础包+Spring基础包(AOP代理包和cglib...)第二步:在spring配置 ...

Hibernate是一款ORM关系映射框架+Spring是结合第三方插件的大杂烩,Hibernate+Spring整合开发效率大大提升。

整合开发步骤如下:

第一步:导入架包:

1、Hibernate基础包+Spring基础包(AOP代理包和cglib...)

第二步:在spring配置文件中配置datasource(数据库连接信息要么写在hibernate.cfg.

<??><beans ="http://www.springframework.org/schema/beans"  ="http://www.springframework.org/schema/context" ="http://www.springframework.org/schema/tx"  ="http://www.springframework.org/schema/aop" ="http://www.w3.org/2001/  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.-->    <property name="configLocation" value="classpath:hibernate.cfg.></property>                <!-- 第二种方式:所有的hibernate配置配置在spring中 -->        <!-- 配置映射文件 -->    <!--     <property name="mappingDirectoryLocations">      <list>        <value>com/msit/ssh/sh/entity/User.hbm.-->        <!-- 配置其他选项 -->    <!-- <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 >     <property name="sessionFactory" ref="sessionFactory"></property></bean> -->  <!-- <bean id="hibernatedaosuppert"        abstract="true">    <property name="sessionFactory" ref="sessionFactory"></property>  </bean>  <bean id="userdao"     parent="hibernatedaosuppert">  </bean> --></beans>

原标题:Hibernate+Spring整合开发步骤

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录