Spring整合Hibernate步骤
本人还在学习中的Noob一枚,这篇博文相当于做个笔记啦,有什么问题欢迎大家指出哦,话不多说啦!步骤大概有如下:
- 1.引入整合需要的jar文件
- 2.生成Hibernate配置文件及带注释实体类、映射文件(可选)
- 3.创建Spring配置文件(导入aop、context、tx 命名空间)
- 4.Spring配置数据源(JNDI、C3P0等)
- 5.配置SessionFactory
- 6.使用Spring提供类(HibernateTemplate)编写Dao,简化代码
- 7.整合事务
- 8.单元测试Service
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!-- 配置 c3p0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/你本地数据库的名字" />
<property name="user" value="数据库用户名" />
<property name="password" value="这里填写数据库" />
</bean>
<!-- 使用 Hibernate 配置文件 configLocation 指定 Hibernate 配置文件位置 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
<!-- 使用注释配置的JavaBean使用包扫描器 -->
<property name="packagesToScan" value="org.reyton.entity" />
</bean>
<!-- 使用Hibernate模板简化Dao操作 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
<!-- 依赖注入 SessionFactory -->
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 注册 Dao 并 依赖注入 HibernateTemplate -->
<bean id="userDao" class="org.reyton.dao.impl.UserDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="loggerDao" class="org.reyton.dao.impl.LoggerDaoImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义 HibernateTransactionManager类 针对 Hibernate 特定实现的事务管理器 -->
<bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 定义事务通知 设置事务属性 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT"
rollback-for="java.lang.Exception" />
<tx:method name="delete*" propagation="REQUIRED" isolation="READ_COMMITTED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 装配事务 事务边界设置在 Service 层 -->
<aop:config>
<aop:pointcut id="servicePointcut"
expression="execution(public * org.reyton.service..*(..))" />
<!-- Advisor顾问 将 Pointcut 和 Advice 的装配在一起 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="servicePointcut" />
</aop:config>
<!-- 注册 Service -->
<bean id="userService" class="org.reyton.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao" />
<property name="loggerDao" ref="loggerDao" />
</bean>
</beans>