每日一句: Knowledge is a treasure, but practice the key to it. 知识是一座宝库,实践是打开宝库的钥匙。

Spring整合Hibernate笔记

 

1. Spring 指定datasource

       a) 参考文档,找dbcp.BasicDataSource

              i. c3p0

              ii. dbcp

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/spring" />
        <property name="username" value="root" />
        <property name="password" value="bjsxt" />
    </bean>

              iii. proxool

                    b) 在DAO或者Service中注入dataSource

                     c) 在Spring中可以使用PropertyPlaceHolderConfigure来读取Properties文件的内容

jdbc.properties 

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/spring
jdbc.username=root
jdbc.password=root

 

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <value>classpath:jdbc.properties</value>
        </property>
    </bean>

    <bean id="dataSource" destroy-method="close"
        class="org.apache.commons.dbcp.BasicDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

2. Spring整合Hibernate

       a) <bean .. AnnotationSessionFactoryBean>

      <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
     
        <property name="annotatedClasses">
             <list>
                <value>cn.wym.hibernate.domain.Users
                </value>
             </list> 
        </property>
        </bean>

              i. <property dataSource

              ii. <annotatedClasses

       b) 引入hibernate 系列jar包

       c) User上加Annotation

       d) UserDAO或者UserServie 注入SessionFactory

 

       e) jar包问题一个一个解决

3. 声明式的事务管理

       a) 事务加在DAO层还是Service层?

       b) annotation

              i. 加入annotation.xsd

              ii. 加入txManager bean

              iii. <tx:annotation-driven

              iv. 在需要事务的方法上加:@Transactional

              v. 需要注意,使用SessionFactory.getCurrentSession 不要使用OpenSession

 

       c) @Transactional详解

              i. 什么时候rollback 

                     1. 运行期异常,非运行期异常不会触发rollback

                     2. 必须uncheck (没有catch)

                     3. 不管什么异常,只要你catch了,spring就会放弃管理

                     4. 事务传播特性:propagation_required

                     5. read_only

       d) xml(推荐,可以同时配置好多方法)

              i. <bean txmanager

              ii. <aop:config 

                     1. <aop:pointcut

                     2. <aop:advisor pointcut-ref advice-ref

              iii. <tx:advice: id transaction-manager =

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

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

    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="getUser" read-only="true" />
            <tx:method name="add*" propagation="REQUIRED" />
        </tx:attributes>
    </tx:advice>

       e) HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要)介绍

             i. 设计模式:Template Method

             ii. Callback:回调/钩子函数

             iii. 第一种:(建议)

                     1. 在spring中初始化HibernateTemplate,注入sessionFactory

   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

                     2. DAO里注入HibernateTemplate

                     3. save写getHibernateTemplate.save();

              iv. 第二种:

                     1. 从HibernateDaoSupport继承

                     2. 必须写在xml文件中,无法使用Annotation,因为set方法在父类中,而且是final的spring整合hibernate的时候使用packagesToScan属性,可以让spring自动扫描对应包下面的实体类。

       <property name="packagesToScan">
            <list>
                <value>com.bjsxt.model</value>
            </list>
        </property>
posted @ 2013-09-03 07:42  wuyongmao  阅读(837)  评论(0编辑  收藏  举报