**html
struts.xml
**java
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!--禁用动态访问 user!add.action-->
<constant name="struts.enable.DynamicMethodInvocation" value="false"/>
<!-- 开发模式下使用,这样能够打印出更详细的错误信息 -->
<constant name="struts.devMode" value="true"/>
<!-- 设置url请求后缀 -->
<constant name="struts.action.extention" value="do,action,html,htm"/>
<!--把主题配置成simple-->
<constant name="struts.ui.theme" value="simple"/>
<!-- 指定Web应用的默认编码集,至关于调用HttpServletRequest的setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 当struts的配置文件修改后,系统是否自动从新加载该文件,默认值为false(生产环境下使用),开发阶段最好打开 -->
<constant name="struts.configuration.xml.reload" value="true" />
<!-- spring 托管 -->
<constant name="struts.objectFactory" value="spring" />
<!-- 该属性指定处理 MIME-type multipart/form-data,文件上传 -->
<constant name="struts.multipart.parser" value="cos" />
<constant name="struts.multipart.parser" value="pell" />
<constant name="struts.multipart.parser" value="jakarta" />
<!-- 指定上传文件时的临时目录,默认使用 javax.servlet.context.tempdir -->
<constant name="struts.multipart.saveDir" value="/tmpuploadfiles" />
<!-- 该属性指定Struts 2文件上传中整个请求内容容许的最大字节数 -->
<constant name="struts.multipart.maxSize" value="2097152" />
<!-- 设置是否支持动态方法调用,true为支持,false不支持. -->
<constant name="struts.enable.DynamicMethodInvocation" value="true" />
<package name="" extends="struts-default" namespace="/">
</package>
</struts>
hibernate.cfg.xmlmysql
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<!--设置方言-->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!--是否打印sql语句。-->
<property name="show_sql">true</property>。
<!--是否格式化sql语句。-->
<property name="format_sql">true</property>
<!--设置更新数据库。-->
<property name="hbm2ddl.auto">update</property>
<!--设置被映射的bean类。-->
<mapping resource="com/cloud/sys/domain/User.hbm.xml" />
<!--设置url。-->
<property name="connection.url">jdbc:mysql://localhost:3306/oa</property>
<!--设置数据库帐号。-->
<property name="connection.username">root</property>
<!--设置数据库密码。-->
<property name="connection.password">root</property>
<!--设置数据库驱动。-->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!--设置c3p0链接池。-->
<property name="hibernate.connection.provider_class">
org.hibernate.connection.C3P0ConnectionProvider
</property>
<!-- 指定链接池里最大链接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定链接池里最小链接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定链接池里链接的超时时长 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 指定链接池里最大执行命令的个数 -->
<property name="hibernate.c3p0.max_statements">100</property>
<!-- 指定链接池里空闲测试时间 -->
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- 指定链接池里每次增长的链接数 -->
<property name="hibernate.c3p0.acquire_increment">2</property>
<!-- 指定链接池每次都验证链接是否可用 -->
<property name="hibernate.c3p0.validate">false</property>
<!-- 设置获得的session为线程类型的 -->
<property name="hibernate.current_session_context_class">thread</property>
</session-factory">
</hibernate-configuration>
applicationContext.xmlweb
<?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:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置注解扫描 -->
<context:component-scan base-package="zhujie"></context:component-scan>
<!-- 建立sessionfactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property value="classpath:hibernate.cfg.xml" name="configLocation"/> //引进hibernate
</bean>
<!-- 配置事务管理器 -->
<bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" id="txManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务加强 (xml的方式来配置事务)-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置AOP -->
<aop:config>
<!-- 切入点-->
<aop:pointcut expression="execution(* com.cloud.*.service.impl.*.*(..))" id="pt"/>
<!-- 切面 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
</aop:config>
<!-- 引入外部spring文件 -->
<import resource="com/cloud/conf/test-spring.xml"/>
<!-- 设置用注解的方式来开启事务 ( 须要在须要开启的类上面加上@Transactional )-->
<tx:annotation-driven transaction-manager="txManager" proxy-target-class="true" />
</beans>
spring 2.5 中除了提供 @Component
注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。spring
@Service用于标注业务层组件sql
@Controller用于标注控制层组件(如struts中的action)数据库
@Repository用于标注数据访问组件,即DAO组件express
@Component泛指组件,当组件很差归类的时候,咱们可使用这个注解进行标注。apache
//=====================================浏览器
@Autowired与@Resource的区别
<!--strtus2的过滤器配置-->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--spring的监听器配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<description>spring监听器</description>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
user.hbm.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.cloud.sys.domain">
<class name="User" table="t_user">
<id name="id" column="u_id">
<generator class="native"/>
</id>
<property name="account"/>
<property name="password"/>
<property name="gender"/>
<property name="country"/>
<property name="name"/>
<property name="age"/>
<property name="birthday"/>
</class>
</hibernate-mapping>
在applicationContext.xml中配置以下本来在hibernate.cfg.xml中须要配置的信息,在spring中配置后hibernate.cfg.xml 可删除。
二、 db.properties
jdbcUrl=jdbc:mysql://localhost:3306/itcastTax?useUnicode=true&characterEncoding=utf8
driverClass=com.mysql.jdbc.Driver
user=root
password=root
initialPoolSize=10
maxPoolSize=30
<!-- 导入外部的properties配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 配置c3p0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<!--初始化时获取三个链接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="${initialPoolSize}"></property>
<!--链接池中保留的最小链接数。Default: 3 -->
<property name="minPoolSize" value="3"></property>
<!--链接池中保留的最大链接数。Default: 15 -->
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<!--当链接池中的链接耗尽的时候c3p0一次同时获取的链接数。Default: 3 -->
<property name="acquireIncrement" value="3"></property>
<!--最大空闲时间,1800秒内未使用则链接被丢弃,若为0则永不丢弃。Default: 0-->
<property name="maxIdleTime" value="1800"></property>
</bean>
<!--配置sessionFactory,并将dataSource指向c3p0建立的dataSource:-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="javax.persistence.validation.mode">none</prop>
</props>
</property>
<property name="mappingLocations">
<list>
<value>classpath:cn/itcast/nsfw/*/entity/*.hbm.xml</value>
<value>classpath:cn/itcast/test/entity/*.hbm.xml</value>
</list>
</property>
</bean>
<!--配置spring事务管理:-->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!—事务通知-->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true" />
<tx:method name="get*" read-only="true" />
<tx:method name="load*" read-only="true" />
<tx:method name="list*" read-only="true" />
<tx:method name="search*" read-only="true" />
<tx:method name="*" rollback-for="Throwable" />
</tx:attributes>
</tx:advice>
<!—配置须要进行事务控制的类 -->
<aop:config>
<aop:pointcut id="serviceOperation" expression="bean(*Service)" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
</aop:config>
【注意:上面的pointcut expression 表示拦截以Service结尾的bean,或者可写成
execution(* cn.itcast..service.impl.*.*(..))】