Spring 整合 hibernate
1.主要整合什么?
1)用spring生成hibernate的seesion
2)让hibernate使用spring的声明事务
2.整合步骤
1)加入hibernate
1.添加hibernate的jar包 此处是java项目所以还添加了吃c3p0的连接池jar包 如果是web项目 服务器一般自带jdbc连接池
2.添加hibernate的配置文件hibernate.cfg.xml
主要配置hibernate基本属性 如sql的显示与格式化 sql方言 hibernate二级缓存 数据表生成方式
不需要再这个里配置数据源
3.配置表映射文件hibernate.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- hibernate配置文件 -->
<hibernate-configuration>
<session-factory>
<!-- 配置基本属性 -->
<!-- 1.配置数据源 但是此处与spring 整合 故在spring中配置数据源即可 -->
<!-- 2.hibernate.hbm.xml 表映射也在spring ioc容器生成sessionfactory时配置 -->
<!-- 3.配置hibernate的基本属性如 方言,数据表生成策略 ,二级缓存,sql显示与格式化 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- 4.生成数据表策略 -->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 配置二级缓存相关 -->
</session-factory>
</hibernate-configuration>
2)加入spring
1.加入spring的核心jar包
2.配置文件
1.首先配置数据源 因为在Hibernate中没有配置
2.配置sessionFactory
3.配置spring的声明事务
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 配置自动扫描注解包 -->
<context:component-scan base-package="com.cst.spring.hibernate.dao"></context:component-scan>
<!-- 实例数据库接口 -->
<bean id="dbDao" class="com.cst.spring.hibernate.dao.IDbDao"/>
<!-- 首先配置数据源 此处配置的是java项目 如果为web项目则不一样 -->
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initialPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
<!-- 配置hibernate 的seesionFactory 通过spring提供的localsessionFactory进行配置-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate配置文件位置 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!-- 配置hibernate映射文件的位置 -->
<property name="mappingLocations" value="classpath:com/cst/spring/hibernate/entities/*.hbm.xml"></property>
</bean>
<!-- 配置spring的声明事务 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop相关 -->
<aop:config>
<aop:pointcut expression="execution(* com.cst.spring.hibernate.service.*.*(..))" id="txpointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txpointcut"/>
</aop:config>
</beans>
3)编写测试程序