Spring整合Hibernate
单独配置Hibernate
1、导入实体类&ORM元数据
2、配置主配置文件
3、测试
Spring整合Hibernate
整合原理
将sessioinFactory对象交给Spring容器进行管理
在Spring中配置sessionFactroy
配置方案2:(重点)
!-- 加载配置方案2:在Srpign配置中放置hibernate配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置hibernate基本信息 -->
<property name="hibernateProperties">
<props>
<!-- 必选配置 -->
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.Driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql:///tz_crm</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">liangchen</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!-- 可选配置 -->
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 引入orm元数据,指定orm元数据所在的包路径,spring会自动读取包中的所有配置 -->
<property name="mappingDirectoryLocations" value="classpath:com/itcast/domain">
</property>
Spring整合c3p0连接池
1、db.properties
jdbc.jdbcUrl=jdbc:mysql:///tz_crm
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=自己的密码
2、引入连接池到spring
<!-- 读取db.properties文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- c3p0连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
3、将连接池注入SessionFactory
<!-- 加载配置方案2:在Srpign配置中放置hibernate配置信息 -->
<bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 将连接池注入sessionFactory,hibernate会通过连接池获得连接 -->
<property name="dataSource" ref="dataSource"></property>
整合HibernateTemplate模板操作数据库
整合aop事务
准备工作:
<!-- 核心事务管理器 -->
<bean name="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
xml配置aop事务
配置通知
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="persist*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="modify*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="delete*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="remove*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
<tx:method name="get*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="find*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
<tx:method name="transfer*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
</tx:attributes>
</tx:advice>
配置织入
<!-- 配置将通知织入目标对象 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.itcast.service.impl.*ServiceImpl.*(..))" id="txPc"/>
<!-- 配置切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPc"/>
</aop:config>
注解配置aop事务
开启注解事务
Service类中使用注解
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true)
@Override
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void saveUser(User u) {
ud.save(u);
}
扩大session作用范围
为了避免使用懒加载时出现No-session问题需要扩大sesison的作用范围
配置filter
<!-- 扩大session作用范围
注意:任何filter一定要在struts的fitler之前调用
-->
<filter>
<filter-name>openSessioinInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessioinInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>