Spring整合Hibernate

1.配置数据源

<!-- 配置数据源 使用C3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///shop"></property>
        <property name="user" value="root"></property>
        <property name="password" value=""></property>
    </bean>

2.让Spring的IOC容器管理Hibernate的SessionFactory

<!-- 配置SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
       <property name="packagesToScan">
           <list>
              <value>com.entity</value>
           </list>
       </property>
    </bean>

其中

<property name="packagesToScan">
           <list>
              <value>com.entity</value>
           </list>
       </property>

这里是为了能使用Hibernate对Bean进行注解开发

3.事务管理

<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
       <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

4.声明面向切面编程

<tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
          <tx:method name="get*" read-only="true"/>
          <tx:method name="*"/>
       </tx:attributes>
    </tx:advice>

    <aop:config>
       <aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="txPointcut"/>
       <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

其中

<aop:pointcut expression="execution(* com.service.impl.*.*(..))" id="txPointcut"/>

这里的第一个*号指任意返回值,第二个指impl包下的任意类,第三个指任意方法,最后是任意参数

最后是web.xml中Spring的DispatcherServlet的配置

 <!-- The front controller of this Spring Web application, responsible for handling all application requests -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:applicationContext.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

以上是本人在学习过程中为了避免忘记而记录,若有错,欢迎指正!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值