Struts2+Spring+Hibernate3整合

这几天正在复习三大框架的知识,特意把写出来,如有错误,希望大家多指教!

代码地址:https://git.coding.net/puchenglin/SSHDemo.git

1. 引入jar

  Struts所需jar包:

    l   ../struts2/apps/路径下的

    l   struts2-spring-plugin-2.3.12  用于整合spring

  Hibernate所需jar包:

    l hibernate3.jar

    l ../hibernate/lib/required/*.jar

    l ../hibernate/lib/jpa.*.jar

    l slf4j-log4j12-1.7.2  日志记录slf4j整合log4j

  l mysql-connector-java-5.1.25-bin  数据库驱动包

  Spring所需jar包:

  IOC:

    l com.springsource.org.apache.commons.logging-1.1.1

    l com.springsource.org.apache.log4j-1.2.15

    l spring-beans-4.0.0.RELEASE

    l spring-context-4.0.0.RELEASE

    l spring-core-4.0.0.RELEASE

    l spring-expression-4.0.0.RELEASE

  AOP:

    l com.springsource.org.aopalliance-1.0.0

    l com.springsource.org.aspectj.weaver-1.6.8.RELEASE

    l spring-aop-4.0.0.RELEASE

    l spring-aspects-4.0.0.RELEASE

  基本:

    l spring-jdbc-4.0.0.RELEASE

    l spring-orm-4.0.0.RELEASE

    l spring-test-4.0.0.RELEASE

    l spring-tx-4.0.0.RELEASE

    l spring-web-4.0.0.RELEASE

    l commons-logging-1.1.1

    l com.springsource.com.mchange.v2.c3p0-0.9.1.2  连接池

2. 引入配置文件

(一)Struts配置

Step 1: 配置web.xml

web.xml文件中为Struts配置过滤器,代码如下

<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>

 

Step2: 配置struts.xml

 

 

(二)Spring配置

Step1 : 配置web.xml

web.xml文件中为Spring配置监听器,使得tomcat启动时加载applicationContex.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>

Step2 : 配置applicationContext.xml

 

(三)Hibernate配置

1.配置映射文件

2.applocationContext.xml中配置数据库信息

 

3. Structs2整合Spring

关键jar包:struts2-spring-plugin.jar

  1. 配置Action.

第一步:编写Action类,继承ActionSupport,实现ModelDriven<T>接口,并且注入Service

public class BookAction extends ActionSupport implements ModelDriven<Book> {

 

/**

 * 模型驱动

 * 把实体类当作页面数据的收集对象

 */

private Book book = new Book();

 

@Override

public Book getModel() {

// TODO Auto-generated method stub

return book;

}

 

/**

 * Service的注入

 * 更具struts2-spring-plugin整合jar包,

 * 按名称自动注入

 */

private BookService bookService;

 

 

public void setBookService(BookService bookService) {

this.bookService = bookService;

}

 

 

public String save() {

System.out.println("BookAction执行了"+book.getPname());

bookService.save(book);

return NONE;

}

 

}

第二步:在XML文件中配置Action,有两种方法

方法一:在Struts.xml文件中配置。

<package name="ssh" extends="struts-default" namespace="/">

<action name="book_*" method="{1}" class="com.pcl.action.BookAction"></action>

</package>

 

 

方法二:交给Spring管理,在applicationContext.xml中配置。

<!-- 配置Action Spring管理Action-->

<bean id="bookAction" class="com.pcl.action.BookAction" scope="prototype">

<!-- 手动注入Service -->

<property name="bookService" ref="bookService"></property>

</bean>

修改struts.xml文件中的actionclass属性,改为beanid.

 

两种方法的区别:

1.方法一的service是自动注入的,根据名称;而方法二是手动注入的。

2.在Struts中是,action是多例的;而springbean默认是单例的,所系需要修改为prototype

PS: 官方建议使用方法二,因为交给Spring管理,可以使用AOP.

 

配置Service

编写Service类,在Service中注入Dao类。

/**

 * 注入dao

声明一个变量,提供set方法,并且在applicationContext.xml文件中配置注入

 * 配置applicationContext.xml

 */

private BookDao bookdao;

 

public void setBookdao(BookDao bookdao) {

this.bookdao = bookdao;

}

<!-- 配置业务层的类 Service -->

<bean id="bookService" class="com.pcl.service.BookService">

<!-- Service类注入Bookdao -->

<property name="bookdao" ref="bookDao"></property>

</bean>

 

<!-- 配置DAO-->

<bean id="bookDao" class="com.pcl.dao.BookDao">

</bean>

配置DAO

DAO类交给Spring管理,在applicationContext.xml文件中定义bean即可。

 4. Spring整合Hibernate

1) 创建映射文件

例:

<?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>

<class name="com.pcl.entity.Book" table="book">

<id name="pid" column="pid">

<generator class="native"></generator>

</id>

<property name="pname" column="pname"></property>

<property name="price" column="price"></property>

</class>

</hibernate-mapping>

2) applicationContext.xml中配置数据库

<!-- 引入外部属性文件 -->

<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置数据库连接池 -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${mysql.driverClass}"></property>

<property name="jdbcUrl" value="${mysql.url}"></property>

<property name="user" value="${mysql.username}"></property>

<property name="password" value="${mysql.password}"></property>

</bean>

<!-- 配置Hibernate属性 -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- 注入数据源 -->

<property name="dataSource" ref="dataSource"></property>

<!-- 配置Hibernate属性 -->

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>

<prop key="hibernate.show_sql">true</prop>

<prop key="hibernate.format_sql">true</prop>

<prop key="hibernate.hbm2ddl.auto">update</prop>

</props>

</property>

<!-- 配置映射文件 -->

<property name="mappingResources">

<list>

<value>com/pcl/entity/Book.hbm.xml</value>

</list>

</property>

</bean>

数据库连接信息db.properties

mysql.driverClass=com.mysql.jdbc.Driver

mysql.url=jdbc:mysql://localhost:3306/ssh

mysql.username=root

mysql.password=admin

3) 编写DAO

继承HibernateDaoSupport,该类中有hibernate模板,然后在xml文件中注入sessionFactory

在最后调用this.getHibernateTemplate().save(book);

public class BookDao extends HibernateDaoSupport{

public void save(Book book) {

System.out.println("BookDao执行了"+book.getPname()+book.getPrice());

this.getHibernateTemplate().save(book);

}

}

<!-- 配置DAO-->

<bean id="bookDao" class="com.pcl.dao.BookDao">

<!-- 注入SessionFactory -->

<property name="sessionFactory" ref="sessionFactory"></property>

</bean>

原文地址:https://www.cnblogs.com/charliePU/p/5776756.html