基本的整合步骤如下:
由于在spring中可以直接实现自动装配bean对象,所以可以直接将hibernate中的配置属性移植过来:
1. 装配dataSource对象
<!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/test"></property> <property name="username" value="test"></property> <property name="password" value="test"></property> </bean>
2. 添加事务管理
2.1 添加事务管理
<!-- 声明事务管理 --> <!-- 配置事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="select*" read-only="true"/> <tx:method name="find*" read-only="true"/> <tx:method name="get*" read-only="true"/> <tx:method name="query*" read-only="true"/> </tx:attributes> </tx:advice>
2.2 添加AOP:在service层切面添加事务
<!-- 配置aop --> <aop:config> <aop:pointcut expression="execution(* com.bjsxt.service.impl.*.*(..))" id="pointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/> </aop:config>
3. 装配sessionFactory对象
<!-- 定义hibernate的 sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="mappingLocations"> <list> <value>com/bjsxt/pojo/*.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> </props> </property> </bean>
4. 该步骤的实现方式有3种:
4.1 直接在dao中注入sessionFactory
<bean id="userDao" class="com.bjsxt.dao.impl.UserDaoImpl"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean>
4.2 先装配HibernateTemplate对象,并注入sessionFactory。然后在dao中注入hibernateTemplate。
<!-- 使用hibernateTemplate --> <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 在DAO中注入hibernateTemplate --> <bean id="userDao" class="com.bjsxt.dao.impl.UserDaoImpl"> <property name="hibernateTemplate" ref="hibernateTemplate"></property> </bean> <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean>
4.3 先装配HibernateDaoSupport对象,并注入sessionFactory。然后在dao对象中继承hibernateDaoSupport。
<!-- 使用hibernateDaoSupport --> <bean id="hibernateDaoSupport" class="org.springframework.orm.hibernate4.support.HibernateDaoSupport" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 在DAO中继承hibernateDaoSupport --> <bean id="userDao" class="com.bjsxt.dao.impl.UserDaoImpl" parent="hibernateDaoSupport"> </bean> <bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean>