第一:
spring整合hibernate
加入jar包
加入spring和aop所需必须包
加入hibernate的必须包
spring整合hibernate的必须包
org.springframework.jdbc-3.1.3.RELEASE.jar
org.springframework.orm-3.1.3.RELEASE.jar
org.springframework.transaction-3.1.3.RELEASE.jar
第二:
加入配置文件
加入spring的配置文件
加入hibernate的配置文件
第三: applicationContext.xml
<!--1. 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.DriverManagerDataSource">
<property name="user" value="root"></property>
<property name="password" value=""></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost/zry?characterEncoding=UTF-8"> </property>
</bean>
<!--2. 創建session工廠類 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean" autowire="byName">
<property name="configLocation">
<value>
classpath:hibernate.cfg.xml
</value>
</property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--3 创建事物管理对象 -->
<bean id="tx" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 创建事物通知 -->
<tx:advice id="advice" transaction-manager="tx">
<tx:attributes>
<tx:method name="add*" read-only="false" />
<tx:method name="save*" read-only="false" />
<tx:method name="del*" read-only="false" />
<tx:method name="update*" read-only="false" />
<tx:method name="*" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 操作数据库模板类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate" autowire="byType">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.hp.service.impl.*.*(..))" />
<aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>
</aop:config>
<!-- 用户操作 -->
<bean id="userDAO" class="com.hp.dao.impl.UserDAOImpl" autowire="byName"></bean>
<bean id="userService" class="com.hp.service.impl.UserServiceImpl" autowire="byName"></bean>
<bean id="userAction" class="com.hp.action.UserAction" autowire="byName"></bean>