Spring hibernate struts2 三大框架整合配置文件:
applicationContext.xml文件:基于xml配置
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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.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">
<!-- spring整合hibernate方式一 -->
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean> -->
<!-- spring整合hibernate方式二 -->
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 加载连接池 -->
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<!-- 以下属性在书写时不能省略hibernate -->
<!-- <props> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.format_sql">true</prop>
</props> -->
<!-- 上述的配置可以简写成以下 -->
<value>
hibernate.show_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true
</value>
</property>
<!-- 加载hibernate的Xxx.hbm.xml配置文件 -->
<property name="mappingResources">
<list>
<value>cn/itheima/domain/User.hbm.xml</value>
</list>
</property>
<!-- <property name="mappingLocations"> <list> <value>classpath:cn/ithiema/domain/User.hbm.xml</value>
</list> </property> <property name="mappingDirectoryLocations"> <list> <value>classpath:cn/itheima/domain</value>
</list> </property> -->
</bean>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 声明dao -->
<bean id="userDao" class="cn.itheima.dao.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 声明service -->
<bean id="userService" class="cn.itheima.service.UserServiceImpl">
<property name="userDao" ref="userDao" />
</bean>
<!-- 声明式事务管理 -->
<!-- 事务管理器 -->
<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="find*" read-only="true"/>
<tx:method name="add"/>
<tx:method name="update"/>
<tx:method name="del"/>
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:pointcut expression="execution(* cn.itheima.service.*..*(..))" id="mypointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="mypointcut"/>
</aop:config>
<!-- 配置action -->
<!-- <bean id="userAction" class="userAction类的全路径" scope="prototype">
<property name="userService" ref="userService"/>
</bean> -->
</beans>
applicationContext.xml文件:基于注解开发配置
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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.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">
<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 开启注解扫描 @Respostory @Service @Controller-->
<context:component-scan base-package="cn.itcast"/>
<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 声明sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 加载连接池 -->
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<value>
hibernate.show_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true
</value>
</property>
<!-- 加载注解类 -->
<property name="packagesToScan">
<list>
<value>cn.itcast.domain</value>
</list>
</property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>