【SSH】:基于Struts2+HIbernate3+Spring3实现员工管理系统之框架整合篇

来源:互联网 发布:游戏编程要学历吗 编辑:程序博客网 时间:2024/05/06 12:35

       SSH知识点回顾

       这个不用多说,老师画的一张图还是可以的:

 

       搭建SSH开发环境

       版本比较旧,但是方法还是好的,给初学者还是很大帮助的。

       SSH整合的三种方式:

       1、带有HIbernate配置文件的方式

       2、不带有HIbernate配置文件的方式

       3、纯注解的

       这一次我们采用第二种方式来进行SSH框架整合。

       步骤一:创建web项目并引入相应的jar包

       Struts2框架开发的相应的jar包

       一般我们都去这个路径下struts-2.3.31\apps\struts2-blank\WEB-INF\lib索取开发的jar包:

       

       然后再去struts-2.3.31\lib索取其它比较重要的jar包:

       struts2-convention-plugin-2.3.31.jar:Struts2的注解开发的jar包

       struts2-spring-plugin-2.3.31.jar:Struts2用于整合Spring的jar包

       HIbernate框架开发的相应的jar包

       我们使用的Hibernate版本是hibernate-release-3.3.1.Final。

       hibernate-release-3.3.1.Final\lib\required\*.jar

       hibernate-release-3.3.1.Final\lib\jpa\*.jar

       Spring框架开发的相应的jar包

       spring-framework-3.2.0.RELEASE

       做Spring基本开发需要引入的jar包:

       IoC

       spring-beans-3.2.0.RELEASE.jar

       spring-context-3.2.0.RELEASE.jar

       spring-core-3.2.0.RELEASE.jar

       spring-expression-3.2.0.RELEASE.jar

       依赖的jar包

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

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

       AOP

       spring-aop-3.2.0.RELEASE.jar

       spring-aspects-3.2.0.RELEASE.jar

       依赖的jar包

       com.springsource.org.aopalliance-1.0.0.jar

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

       事务管理的jar包 

       spring-tx-3.2.0.RELEASE.jar

       整合HIbernate的包

       spring-orm-3.2.0.RELEASE.jar

       spring-jdbc-3.2.0.RELEASE.jar

       整合Web项目

       spring-web-3.2.0.RELEASE.jar

       整合Junit单元测试

       spring-test-3.2.0.RELEASE.jar

       连接池:

       com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

       有关日志记录的jar包

       slf4j-log4j12-1.7.5.jar: slf4j整合log4j的jar包

       commons-logging-1.1.3.jar

       mysql-connector-java-5.1.22-bin.jar:MySQL数据库驱动包

       步骤二:引入相关配置文件

       这里将已经配置好的配置文件贴出来,后面很多的配置连接池和事务管理以及注入方式步骤省略。

       WebContent/WEB-INF/web.xml配置文件的内容:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                   xmlns="http://xmlns.jcp.org/xml/ns/javaee"                   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                                                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"                   id="WebApp_ID"                   version="3.1">  <display-name>struts2_spring3_hibernate3</display-name>    <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <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>    <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>  </web-app>
       struts.xml配置文件

       在src目录下引入struts.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><package name="ssh" namespace="/" extends="struts-default"><action name="product_*" class="productAction" method="{1}"></action> </package></struts>    

       src/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/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context.xsd        http://www.springframework.org/schema/aop        http://www.springframework.org/schema/aop/spring-aop.xsd        http://www.springframework.org/schema/tx        http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 引入外部的属性文件 --><context:property-placeholder location="classpath:jdbc.properties"/><!-- 配置连接池 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.url}"></property><property name="user" value="${jdbc.username}"></property><property name="password" value="${jdbc.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><!-- 加载hibernate中的映射文件 --><property name="mappingResources"><list><value>com/demo/domain/Product.hbm.xml</value></list></property></bean><!-- 配置Action的类 --><bean id="productAction" class="com.demo.action.ProductAction" scope="prototype"><property name="productService" ref="productService"></property></bean><!-- 配置业务层的类 --><bean id="productService" class="com.demo.service.ProductService"><property name="productDao" ref="productDao"></property></bean><!-- 配置DAO的类 --><bean id="productDao" class="com.demo.dao.ProductDao"><property name="sessionFactory" ref="sessionFactory"></property> </bean><!-- 配置事务管理 --><bean id="transaction" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory"></property></bean><!-- 开启注解事务 --><tx:annotation-driven transaction-manager="transaction"></tx:annotation-driven></beans>

       src/jdbc.properties属性配置文件的内容:

jdbc.driverClass=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/sshjdbc.username=rootjdbc.password=root

       src/log4j.properties属性配置文件的内容:

log4j.rootLogger = debug, console, E#level=INFO,all can be output#console is set to be a ConsoleAppenderlog4j.appender.console = org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout = org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern = [%p] %d{yyyy-MM-dd HH:mm:ss} - %m%n#file is set to output to a extra filelog4j.appender.E = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.E.layout = org.apache.log4j.PatternLayoutlog4j.appender.E.Threshold=errorlog4j.appender.E.Encoding=UTF-8log4j.appender.E.layout.ConversionPattern=[%p] %d{yyyy-MM-dd HH:mm:ss} - %m%nlog4j.appender.E.DatePattern='.'yyyy-MM-ddlog4j.appender.E.File = ${catalina.base}/logs/dd/error.log

       步骤三:创建包结构

       

       步骤四:创建页面(保存商品)

       addProduct.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><%@ taglib uri="/struts-tags" prefix="s" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>添加商品页面</title></head><body>     <h1>保存商品的页面</h1><s:form action="product_save" method="post" theme="simple" namespace="/"><table border="1" width="400"><tr><td>商品名称:</td><td><s:textfield name="pname"></s:textfield></td></tr><tr><td>商品价格:</td><td><s:textfield name="price"></s:textfield></td></tr><tr><td colspan="2"><input type="submit" value="添加" /></td></tr></table></s:form></body></html>

       步骤五:编写实体类、对象关系映射文件、Action、Service和DAO

       Product.java

package com.demo.domain;/** * 商品实体类 * @author Administrator * @date 2016年12月24日 */public class Product {private Integer pid;private String pname;private Double price;public Integer getPid() {return pid;}public void setPid(Integer pid) {this.pid = pid;}public Double getPrice() {return price;}public String getPname() {return pname;}public void setPname(String pname) {this.pname = pname;}public void setPrice(Double price) {this.price = price;}}

       Product.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.demo.domain.Product" table="product"><id name="pid" column="pid"><generator class="native"></generator></id><property name="pname" column="pname" length="20"></property><property name="price" column="price"></property></class></hibernate-mapping>

       ProductAction.java

package com.demo.action;import com.demo.domain.Product;import com.demo.service.ProductService;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;/** * 商品管理的action类 * @author Administrator * @date 2016年12月24日 */@SuppressWarnings("serial")public class ProductAction extends ActionSupport implements ModelDriven<Product>{//模型驱动使用的类private Product product = new Product();@Overridepublic Product getModel() {return product;}//Struts2和Spring整合过程中按名称注入的业务层的类private ProductService productService;public void setProductService(ProductService productService) {this.productService = productService;}/** * action保存商品的执行方法:save() * @return */public String save(){System.out.println("Action中的save()方法执行了");productService.save(product);return NONE;}}

       ProductService.java

package com.demo.service;import org.springframework.transaction.annotation.Transactional;import com.demo.dao.ProductDao;import com.demo.domain.Product;/** * 商品管理的service类 * @author Administrator * @date 2016年12月24日 */@Transactionalpublic class ProductService {//业务层注入DAO的类private ProductDao productDao;public void setProductDao(ProductDao productDao) {this.productDao = productDao;}/** * 业务层保存商品的方法 * @param product */public void save(Product product) {System.out.println("Service中的save()方法执行了");productDao.save(product);}}

       ProductDao.java

package com.demo.dao;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.demo.domain.Product;/** * 商品管理的dao类 * @author Administrator * @date 2016年12月24日 */public class ProductDao extends HibernateDaoSupport {/** * DAO中保存商品的方法 * @param product */

public void save(Product product) {System.out.println("DAO中的save()方法执行了");this.getHibernateTemplate().save(product);}}

       步骤六:配置Action、Service和DAO

       Struts2和Spring整合的两种方式:

       1、Action的类由Struts2自身去创建
       Action的配置:struts.xml

      <package name="ssh" namespace="/" extends="struts-default"><action name="product_*" class="com.demo.action.ProductAction" method="{1}"></action>       </package>
       Service和DAO是如何配置的
       直接在applicationContext.xml中进行配置

        <!-- 配置业务层的类 --><bean id="productService" class="com.demo.service.ProductService"><property name="productDao" ref="productDao"></property></bean><!-- 配置DAO的类 --><bean id="productDao" class="com.demo.dao.ProductDao" />
       2、Action的类交给Spring框架创建
       Action、Service和DAO的配置:applicationContext.xml

        <!-- 配置Action的类 --><bean id="productAction" class="com.demo.action.ProductAction" scope="prototype"><property name="productService" ref="productService"></property></bean><!-- 配置业务层的类 --><bean id="productService" class="com.demo.service.ProductService"><property name="productDao" ref="productDao"></property></bean><!-- 配置DAO的类 --><bean id="productDao" class="com.demo.dao.ProductDao"><property name="sessionFactory" ref="sessionFactory"></property> </bean>
       Action配置

      <package name="ssh" namespace="/" extends="struts-default"><action name="product_*" class="productAction" method="{1}"></action> </package>

       步骤七:创建数据库

       create database ssh charset utf8;

       步骤八:Spring整合HIbernate

       在上面的Applicatiom.xml文件已经配置。不管是注入方式还是事务管理,在上面的步骤基本都包括了,就不再

一一赘述。

       运行项目:

       

       控制台输出:

       

       

       数据库的数据:

       

       上述的流程基本通过,下一篇就是实现案例:开发员工管理系统(一个小的示例)

        

3 0
原创粉丝点击