读书人

集成struts2 spring hibernate应用注

发布时间: 2012-09-25 09:55:58 作者: rapoo

集成struts2 spring hibernate,使用注解

集成struts,spring,hibernate时,对于初学者来说最大的麻烦就其繁琐的xml配置文件。现在三者都对基于注解的配置提供了良好的支持。在struts2中,使用convent plugin,得益于annotation和规约,配置过程得以大大减少。在spring2.5也可以使用@Autowired,进行注入,使用 @Controller,@Service,@Repository注解,自动定义bean,还支持annotation风格的声明式事务支持,以及 aspectJ类似的AOP。hibernate也可以使用JPA标准注解定义实体描述,避免使用mapping文件。

当然,对于annotation和xml风格的配置,谁更好,更多依赖个人兴趣。但使用 annotation确实减少了很多配置工作量。本文采用annotation风格的配置,以TaskList为例子讲解struts2 spring hibernate的集成。项目文件见附件。

一:配置struts2。

首先在web.xml文件中配置filter

Xml代码

<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>*.action</url-pattern>??
</filter-mapping>??
<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>*.action</url-pattern>?
</filter-mapping>

?

?然后在classpath中创建struts.xml配置文件。

Xml代码

<?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>??????
??? <constant name="struts.devMode" value="true" />??????
??? <constant name="struts.convention.default.parent.package" value="default-package" />??????
??? <constant name="struts.convention.package.locators" value="action" />??????
??? <package name="default-package" extends="convention-default">????????????
??????? <default-action-ref name="index" />????????????
??????? <action name="index"? >??????????????
??????????? <result>/WEB-INF/content/index.jsp</result>??????????
??????? </action>??????????
??? </package>??
</struts>??

struts.devMode属性,配置启用调试,将有更多的错误信息输出,便于排错。struts.convention.default.parent.package属性,指定使用注解标注的控制器的默认包。可以在这个默认包中配置全局信息。

struts.convention.package.locators属性,为查找控制器包路径的关键字。如com.mycompany.action,com.mycompany.action.user,都会被struts2扫描。里面有继承至Action的类,或类名以Action结尾的类,都会做为Action处理。

<default-action-ref name="index" />指定了默认action,如果指定的action不存在则访问该action。

把struts2-spring-plugin-2.1.6.jar添加到 classpath中,struts2会自动扫描struts-plugin.xml文件,该文件自动注册了 com.opensymphony.xwork2.ObjectFactory,完成和spring的集成。

二:配置spring

在web.xml中加入ContextLoaderListener,用以启动spring容器。用contextConfigLocation指定spring配置文件路径,可以使用*通配符结尾。

Xml代码


<listener>
??? <listener-class>org.springframework.web.context.ContextLoaderListener?????? </listener-class>
</listener>
<context-param>
??? <param-name>contextConfigLocation</param-name>
??? <param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<listener>
??? <listener-class>org.springframework.web.context.ContextLoaderListener </listener-class>
</listener>
<context-param>
??? <param-name>contextConfigLocation</param-name>
??? <param-value>classpath:/applicationContext.xml</param-value>
</context-param>

?

?配置applicationContext.xml

Xml代码

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
??? <bean id="transactionManager" >
??????? <property name="sessionFactory" ref="sessionFactory" />
??? </bean>
??? <bean id="sessionFactory" >
??????? <property name="configLocation" value="classpath:hibernate.cfg.xml" />
??? </bean>
??? <bean id="hibernateTemplate" >
??????? <property name="sessionFactory" ref="sessionFactory" />
??? </bean>
??? <bean id="daoTemplate" abstract="true">
????? <!-- 为DAO组件注入SessionFactory引用 -->
??????? <property name="sessionFactory" ref="sessionFactory"/>
??? </bean>
??? <bean id="baseDaoImpl" parent="daoTemplate" parent="baseDaoImpl" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
??? <session-factory>
??????? <property name="org.hibernate.connection.ConnectionProvider">com.mchange.v2.c3p0.ComboPooledDataSource

</property><!-- C3P0连接池设定-->
??????? <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
??????? <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
??????? <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/db_phonesys</property>
??????? <property name="hibernate.connection.username">root</property>
??????? <property name="hibernate.connection.password">root</property>
??????? <property name="hibernate.hbm2ddl.auto">update</property>
??????? <property name="hibernate.show_sql">true</property>
??????? <property name="hibernate.format_sql">false</property>
??????? <property name="hibernate.current_session_context_class">thread</property><!-- 最大连接数 -->
??????? <property name="hibernate.c3p0.max_size">20</property><!-- 最小连接数 -->
??????? <property name="hibernate.c3p0.min_size">5</property><!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
??????? <property name="hibernate.c3p0.timeout">120</property><!-- 最大的PreparedStatement的数量 -->
??????? <property name="hibernate.c3p0.max_statements">100</property><!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒-->
??????? <property name="hibernate.c3p0.idle_test_period">120</property><!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
??????? <property name="hibernate.c3p0.acquire_increment">2</property><!-- 每次都验证连接是否可用 -->
??????? <property name="hibernate.c3p0.validate">true</property>
??????? <mapping />
??? </session-factory>
</hibernate-configuration>
? 四:BaseDao数据库操作基类

public class BaseDaoImpl extends HibernateDaoSupport??{

...............

五:Entity实体Bean:

beans.User.java

//???? 生成日期:2010-10-24 08:12:32
//------------------------------------------
// <auto-generated>
//???? 此代码由工具生成。
//???? 运行库版本:1.4");
//???? 对此文件的更改可能会导致不正确的行为。
//???? 重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------
/// <summary>自动生成类</summary>
///<summary>SysUserInfo</summary>
package beans;

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "sysuserinfo")
public class User implements Serializable {

??? ///所有属性
??? private int id;
??? private String name;
??? private String password;
??? private String trueName;
??? private String sex;
??? private String phone;
??? private String email;
??? private String remark;

??? ///所有方法
??? ///<summary>属性id </summary>
??? ///<summary>类型int </summary>

??? @GeneratedValue
??? @Id
??? public int getId() {
??????? return this.id;
??? }

??? public void setId(int id) {
??????? this.id = id;
??? }

??? @Column(name = "username")
??? public String getName() {
??????? return name;
??? }

??? public void setName(String name) {
??????? this.name = name;
??? }

??? ///<summary>属性username </summary>
??? ///<summary>类型String </summary>
??? ///<summary>属性password </summary>
??? ///<summary>类型String </summary>
??? @Column(name = "password")
??? public String getPassword() {
??????? return this.password;
??? }

??? public void setPassword(String password) {
??????? this.password = password;
??? }

??? ///<summary>属性trueName </summary>
??? ///<summary>类型String </summary>
??? @Column(name = "trueName")
??? public String getTrueName() {
??????? return this.trueName;
??? }

??? public void setTrueName(String trueName) {
??????? this.trueName = trueName;
??? }

??? ///<summary>属性sex </summary>
??? ///<summary>类型String </summary>
??? @Column(name = "sex")
??? public String getSex() {
??????? return this.sex;
??? }

??? public void setSex(String sex) {
??????? this.sex = sex;
??? }

??? ///<summary>属性phone </summary>
??? ///<summary>类型String </summary>
??? @Column(name = "phone")
??? public String getPhone() {
??????? return this.phone;
??? }

??? public void setPhone(String phone) {
??????? this.phone = phone;
??? }

??? ///<summary>属性email </summary>
??? ///<summary>类型String </summary>
??? @Column(name = "email")
??? public String getEmail() {
??????? return this.email;
??? }

??? public void setEmail(String email) {
??????? this.email = email;
??? }

??? ///<summary>属性remark </summary>
??? ///<summary>类型String </summary>
??? @Column(name = "remark")
??? public String getRemark() {
??????? return this.remark;
??? }

??? public void setRemark(String remark) {
??????? this.remark = remark;
??? }
}

读书人网 >编程

热点推荐