SSH框架整合+Spring事务全注解开发和XML开发
web.xml配置文件: 配置Struts2和 监听器listener (applicationContext.xml视情况而定)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- 配置Struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置spring ,如果applicationContext.xml放在web-inf下不用配置,只需配置listener -->
<context-param>
<param-name>contextConfigLocation</param-name><!-- contextConfigLocation名不能变 -->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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-3.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com"/>
<!-- dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource ">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property><!-- 驱动 -->
<property name="url"
value="jdbc:mysql://localhost:3306/cc2?useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="root"></property><!-- 用户姓名-->
<property name="password" value="huayu123" /><!-- 用户密码 -->
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="3" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="300" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1" />
</bean>
<!-- 配置sessionFactory 引入工厂模式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--分开整合<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
<property name="dataSource" ref="dataSource"></property><!-- 数据源参考上面的decp -->
<property name="hibernateProperties"><!-- hibernate的属性 -->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- hibernate的方言 -->
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 自动生成表 -->
<prop key="hibernate.show_sql">true</prop><!-- 显示sql语句 -->
<prop key="hibernate.format_sql">true</prop><!-- 格式化 -->
</props>
</property>
<property name="mappingResources"><!-- 映射文件 -->
<list>
<value>com/entity/Publish.hbm.xml</value><!-- 映射文件路径 -->
</list>
</property>
</bean>
基于全注解开发
<bean id="HibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 步骤二、具有@Transactional 注解的bean自动配置为声明式事务支持 -->
<!-- 事务管理器配置, Hibernate的数据源事务 -->
<bean id="defaultTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="defaultTransactionManager" proxy-target-class="true" />
</beans>
基于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/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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-3.0.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com"/>
<!-- dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource ">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property><!-- 驱动 -->
<property name="url"
value="jdbc:mysql://localhost:3306/cc2?useUnicode=true&characterEncoding=utf8"></property>
<property name="username" value="root"></property><!-- 用户姓名-->
<property name="password" value="huayu123" /><!-- 用户密码 -->
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="3" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="300" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="2" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="1" />
</bean>
<!-- 配置sessionFactory 引入工厂模式 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!--分开整合<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>-->
<property name="dataSource" ref="dataSource"></property><!-- 数据源参考上面的decp -->
<property name="hibernateProperties"><!-- hibernate的属性 -->
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop><!-- hibernate的方言 -->
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 自动生成表 -->
<prop key="hibernate.show_sql">true</prop><!-- 显示sql语句 -->
<prop key="hibernate.format_sql">true</prop><!-- 格式化 -->
</props>
</property>
<property name="mappingResources"><!-- 映射文件 -->
<list>
<value>com/baidu/entity/Dept.hbm.xml</value>
<value>com/baidu/entity/Person.hbm.xml</value><!-- 映射文件路径 -->
</list>
</property>
</bean>
<!-- 配置HibernateTemplate 里面有很多HibernateTemplate的方法 -->
<bean id="HibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 步骤二、具有@Transactional 注解的bean自动配置为声明式事务支持 -->
<!-- 事务管理器配置, Hibernate的数据源事务 基于全注解开发,只需在所需类前加上:@Transactional
不需要事务的方法前加上: @Transactional(propagation
= Propagation.NOT_SUPPORTED)-->
<bean id="defaultTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- xml方式配置 -->
<aop:aspectj-autoproxy proxy-target-class="true" /><!-- 激活自动代理功能 -->
<aop:config> <!-- 定义一个切面,并将事务通知和切面组合 pointcut切点表达式 advisor切点-->
<aop:pointcut expression="execution(* com.baidu.service.impl.*.*(..))" id="trPointcut"/>
<!-- advice-ref 定义切得规则 怎么切 pointcut-ref定义切得位置 “<aop:advisor>”则定义了在哪些连接点应用什么<aop:aspect>-->
<aop:advisor advice-ref="trcut" pointcut-ref="trPointcut"/>
</aop:config>
<!-- 定义事务通知 -->
<tx:advice id="trcut" transaction-manager="defaultTransactionManager">
<!-- 定义事务传播规则 -->
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="*" propagation="REQUIRED" read-only="true"/>
<!-- 也可以对所有方法都应用REQUIRED事务规则
<tx:method name="*" propagation="REQUIRED"/>-->
</tx:attributes>
</tx:advice>
<!-- 使用annotation定义事务 -->
<!-- <tx:annotation-driven transaction-manager="defaultTransactionManager" proxy-target-class="true" /> -->
</beans>
struts2配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<!-- 上传文件大小,不设置默认为2MB -->
<constant name="struts.multipart.maxSize" value="15728640" />
<!-- 设置开发模式 -->
<constant name="struts.devMode" value="true" />
<!-- 编码格式,struts2解决乱码方式 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<package name="default" extends="struts-default">
<!-- <action name="per_*" class="com.baidu.controller.PersonAction" method="{1}"> -->
<action name="per_*" class="peraction" method="{1}">
<result name="list">/WEB-INF/per/list.jsp</result>
<result name="add">/WEB-INF/per/add.jsp</result>
<result name="update">/WEB-INF/per/update.jsp</result>
<result name="show" type="redirect">/per_list</result>
</action>
</package>
</struts>
dao层
service层
controller层
aspect切面相当于拦截器
默认勾选
Person.hbm.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.baidu.entity.Person" table="person">
<id name="id" type="int">
<column name="id"/>
<generator class="native"/>
</id>
<property name="name" type="java.lang.String">
<column name="name"/>
</property>
<property name="sex" type="java.lang.String">
<column name="sex"/>
</property>
<property name="age" type="int">
<column name="age"/>
</property>
<property name="skill" type="java.lang.String">
<column name="skill"/>
</property>
<property name="jobtime" type="date">
<column name="jobtime"/>
</property>
<property name="filepath" type="java.lang.String">
<column name="filepath"/>
</property>
<property name="resume" type="java.lang.String">
<column name="resume"/>
</property>
<many-to-one name="dept" lazy="false" column="did"
class="com.baidu.entity.Dept"/>
</class>
</hibernate-mapping>
Dept.hbm.xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.baidu.entity.Dept" table="dept">
<id name="did" type="int">
<column name="did"/>
<generator class="native"/>
</id>
<property name="dname" type="java.lang.String">
<column name="dname"/>
</property>
<set name="set" lazy="false" inverse="true">
<key column="did"/>
<one-to-many class="com.baidu.entity.Person"/>
</set>
</class>
</hibernate-mapping>
aop全注解关键字
链接: http://pan.baidu.com/s/1dE8UGsL 密码: cw9f