s2sh整合之注解方式
说明:本文档所采用的框架版本为:Struts 2.1.8, Sping2.5.5, Hibernate 3.5.6
Action层未使用注解,依然是用的配置文件,个人觉得Action层使用注解并没有给开发带来多少简化,反而更加难以维护
------------Strut2-----------:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-logging.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
struts2-spring-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
-------------Spring---------------:
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-lang.jar
commons-logging.jar
spring.jar
-----------Hibernate------------:
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
hibernate3.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar
------------other-----------------
c3p0-0.9.1.2.jar
json-lib-2.1.jar
jstl.jar
junit.jar
log4j-1.2.15.jar
mysql-connector-java-5.1.10-bin.jar
standard.jar
<?xml version="1.0"encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<!-- spring config begin -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- spring config end-->
<filter>
<filter-name>Spring character encoding filter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Spring character encoding filter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<!-- struts config begin-->
<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>
<!-- struts config end-->
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
由于bean的管理使用了注解,使得spring配置文件变得简单多了,所以我把spring的配置文件就合成了一个,同时把hibernate的配置也集成进来啦!
<?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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 启动注解扫描器 -->
<context:component-scan base-package="cn.s2sh.demo"></context:component-scan>
<!-- 数据库配置文件读取 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:props/database.properties</value>
</list>
</property>
</bean>
<!-- 数据库驱动的配置 -->
<bean id="dataSource" name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"destroy-method="close" >
<!-- 指定连接数据库的驱动-->
<property name="driverClass"value="${jdbc.driverClassName}"/>
<!-- 指定连接数据库的URL-->
<property name="jdbcUrl"value="${jdbc.url}"/>
<!-- 指定连接数据库的用户名-->
<property name="user"value="${jdbc.username}"/>
<!-- 指定连接数据库的密码-->
<property name="password"value="${jdbc.password}"/>
<!-- 指定连接池中保留的最大连接数. Default:15-->
<property name="maxPoolSize"value="${jdbc.maxPoolSize}"/>
<!-- 指定连接池中保留的最小连接数-->
<property name="minPoolSize"value="${jdbc.minPoolSize}"/>
<!-- 指定连接池的初始化连接数 取值应在minPoolSize 与 maxPoolSize 之间.Default:3-->
<property name="initialPoolSize"value="${jdbc.initialPoolSize}"/>
<!-- 最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。 Default:0-->
<property name="maxIdleTime"value="${jdbc.maxIdleTime}"/>
<!-- 当连接池中的连接耗尽的时候c3p0一次同时获取的连接数. Default:3-->
<property name="acquireIncrement"value="${jdbc.acquireIncrement}"/>
<!-- JDBC的标准,用以控制数据源内加载的PreparedStatements数量。
但由于预缓存的statements属于单个connection而不是整个连接池所以设置这个参数需要考虑到多方面的因数.
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default:0
-->
<property name="maxStatements"value="${jdbc.maxStatements}"/>
<!-- 每60秒检查所有连接池中的空闲连接.Default:0 -->
<property name="idleConnectionTestPeriod"value="${jdbc.idleConnectionTestPeriod}"/>
</bean>
<!-- sessionFactory配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource"ref="dataSource"/>
<!--annotatedPackages 不可用, packagesToScan 版本2.5.6才会有-->
<property name="annotatedClasses">
<list>
<value>cn.s2sh.demo.domain.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<!-- 配置时前面最好添加上 'hibernate.'-->
<!-- 一个Hibernate Dialect类名允许Hibernate针对特定的关系数据库生成优化的SQL -->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
<!-- 允许被缓存的JDBC 连接开启自动提交 -->
<prop key="hibernate.connection.autocommit">false</prop>
<!-- 强制Hibernate按照被更新数据的主键,为SQL更新排序。 -->
<prop key="hibernate.order_updates">true</prop>
<!-- 允许Hibernate使用JDBC2的可滚动结果集。 -->
<prop key="hibernate.jdbc.use_scrollable_resultset">true</prop>
<!-- 输出所有SQL语句到控制台 -->
<prop key="hibernate.show_sql">true</prop>
<!-- 在log和console中打印出更漂亮的SQL -->
<prop key="hibernate.format_sql">true</prop>
<!-- hibernate将在SQL中生成有助于调式的注解信息 -->
<prop key="hibernate.use_sql_comments">true</prop>
<!-- 指定Hibernate在何时释放 JDBC 连接 -->
<prop key="hibernate.connection.release_mode">after_transaction</prop>
<!-- 同一个事务用一个session -->
<prop key="hibernate.current_session_context_class">thread</prop>
<!-- 同一个事务用一个session -->
<prop key="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- 声明注解方式事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
@Entity
@Table(name= "user",catalog = "s2shdemo")
public classUser implements java.io.Serializable {
// Fields
private Long uid;
private String username;
private String password;
private String email;
private String phone;
private String sex;
// Constructors
/** default constructor */
public User() {
}
/** full constructor */
public User(String username,String password, String email, String phone,
Stringsex) {
this.username = username;
this.password = password;
this.email = email;
this.phone = phone;
this.sex = sex;
}
// Property accessors
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "uid", unique = true, nullable = false)
public Long getUid() {
return this.uid;
}
public void setUid(Long uid) {
this.uid = uid;
}
@Column(name = "username",length = 20)
public String getUsername() {
return this.username;
}
public void setUsername(Stringusername) {
this.username = username;
}
@Column(name = "password",length = 20)
public String getPassword() {
return this.password;
}
public void setPassword(Stringpassword) {
this.password = password;
}
@Column(name = "email",length = 20)
public String getEmail() {
return this.email;
}
public void setEmail(String email){
this.email = email;
}
@Column(name = "phone",length = 20)
public String getPhone() {
return this.phone;
}
public void setPhone(String phone){
this.phone = phone;
}
@Column(name = "sex", length = 10)
public String getSex() {
return this.sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
@Repository("userDao")
public classUserDaoImpl implements UserDao {
@Resource(name="hibernateTemplate")
public HibernateTemplate hibernateTemplate;
...
(其他方法与XML方式无区别)
}
@Service("userService")
public classUserServiceImpl implements UserService {
@Resource(name="userDao")
private UserDao userDao;
...
(其他方法与XML方式无区别)
}
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 开发模式下使用,这样可以打印出更详细的错误信息 ,默认值为false -->
<constant name="struts.devMode" value="true" />
<!-- 设置浏览器是否缓存静态内容,默认值为true(生产环境下使用),开发阶段最好关闭 -->
<constant name="struts.serve.static.browserCache" value="false" />
<!-- 默认的视图主题 simple模式减少多余代码的生成 -->
<constant name="struts.ui.theme" value="simple" />
<!-- 指定Web应用的默认编码集,相当于调用setCharacterEncoding方法 -->
<constant name="struts.i18n.encoding" value="UTF-8" />
<constant name="struts.custom.i18n.resources"
value="cn.s2sh.action.token"></constant>
<!-- 把struts的请求委托给spring管理,
作用:创建Action实例的过程由spring处理,其他的还是由struts2自己处理 -->
<constant name="struts.objectFactory" value="spring" />
<package name="user" namespace="/"extends="struts-default">
<action name="userAction_*"class="userAction" method="{1}">
<result name="login">login.jsp</result>
<result name="index">index.jsp</result>
<result name="listAction">WEB-INF/jsp/user/list.jsp</result>
<result name="action2action"type="redirectAction">userAction_getAllUser</result>
<result name="addUI">WEB-INF/jsp/user/saveUI.jsp</result>
<result name="updateUI">WEB-INF/jsp/user/updateUI.jsp</result>
</action>
</package>
</struts>
下载完整demo: http://download.csdn.net/detail/rchm8519/7537035