由于长时间没有自己整合框架,这几天没事自己整合一个SSH项目,高级篇将在之后进行体现,希望大家提出宝贵的意见和建议。接下来开始吧!
--------------------------ssh 要整合的jar 包-----------------------------
1.struts2整合spring的一个jar 包
struts2-spring-plugin-2.1.8.1.jar
2.hibernate 的所以jar 包
hibernate-testing.jar
hibernate3.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.9.0.GA.jar
jta-1.1.jar
slf4j-api-1.5.8.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
3.日志记录
slf4j整合loj4j 的 jar
log4j-1.2.15.jar
slf4j-log4j12-1.5.8.jar
4.连接数据库的jar 包
mysql-connector-java-3.1.12-bin.jar
5.spring 的jar 包
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-build-src-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
spring-instrument-4.0.0.RELEASE.jar
spring-jdbc-4.0.0.RELEASE.jar
spring-jms-4.0.0.RELEASE.jar
spring-orm-4.0.0.RELEASE.jar
spring-oxm-4.0.0.RELEASE.jar
spring-test-4.0.0.RELEASE.jar
spring-tx-4.0.0.RELEASE.jar
spring-web-4.0.0.RELEASE.jar
spring-webmvc-4.0.0.RELEASE.jar
spring-websocket-4.0.0.RELEASE.jar
6.struts2 的jar 包
antlr-2.7.6.jar
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-collections-3.1.jar
commons-fileupload-1.3.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.3.jar
dom4j-1.6.1.jar
freemarker-2.3.19.jar
ognl-3.0.6.jar
struts2-core-2.3.15.3.jar
struts2-spring-plugin-2.1.8.1.jar
xwork-core-2.3.15.3.jar
--------------------------ssh 的配置文件-----------------------------
引入相应的配置文件。
1.Struts2 的配置文件
web.xml:核心的filter
struts.xml:配置文件
2.spring 的配置文件
applicationContext.xml
web.xml:配置spring 的监听器
4.hibernate 的配置文件
hibernate.cfg.xml 早SSH 整合是可以进行省略的
映射文件
---------------------------创建包结构--------------------------------------
entity-action-service-dao
----------------------------页面设计--------------------------------------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>保存商品</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form action="#" method="post" theme="simple" namespace="/">
<table border="1" width="400">
<tr>
<td>商品的名称</td>
<td><s:textfield name="pname"/></td>
</tr>
<tr>
<td>商品的价格</td>
<td><s:textfield name="price"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加"/></td>
</tr>
</table>
</s:form>
</body>
</html>
----------------------------编写action service dao--------------------------------------
......
----------------------------配置action service dao--------------------------------------
1.Action 通过配置文件的方式实现
applicationContext.xml:
<!-- 配置业务层的类 -->
<bean id="productService" class="com.org.service.productService">
<!-- 注入dao -->
<property name="productDao" ref="productDao"/>
</bean>
<!-- 配置dao的类 -->
<bean id="productDao" class="com.org.dao.ProductDao">
</bean>
Struts.xml:
<action name="product_*" class="com.org.action.ProductAction" method="{1}">
</action >
注意:Struts.xml中class 是通过全类名的方式实现的。
2.Action 通过spring 的方式实现
applicationContext.xml:
<!-- 配置Action 的类 :scope="prototype" scope="prototype"多实例的-->
<bean id="productAction" class="com.org.action.ProductAction" scope="prototype">
<!-- 手动注入service -->
<property name="productService" ref="productService"></property>
</bean>
<!-- 配置业务层的类 -->
<bean id="productService" class="com.org.service.productService">
<!-- 注入dao -->
<property name="productDao" ref="productDao"/>
</bean>
<!-- 配置dao的类 -->
<bean id="productDao" class="com.org.dao.ProductDao">
</bean>
Struts.xml:
<action name="product_*" class="productAction" method="{1}">
</action >
注意:Struts.xml:中class 直接引用applicationContext.xml:中的配置Action 的类中id。
--------------------------创建数据库和映射文件-------------------------------------
mysql 数据库:
命令创建数据库:
cmd------------>mysql -u root -p-------------->create database ssh;
--------------------------编写Dao 的保存操作 -------------------------------------
在Dao 中注入sessionFactory;
1.继承HibernateDaoSupport。
2.使用模板文件即可进行保存操作。
注意: 使用Hibernate4的话,这个方法不适用,必须用etSessionFactory().getCurrentSession()来获取session.
-------------------------- 添加事务 -------------------------------------
首先添加事务的前提是:
1.配置事务管理器。
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
2.开启基于事务的注解。
<tx:annotation-driven transaction-manager="transactionManager"/>
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- 配置struts2 的核心filter -->
<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>
<!-- spring 的监听器 -->
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0 连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置hibernate 相关属性-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<!-- 注入连接池-->
<property name="dataSource" ref="dataSource"></property>
<!-- hibernate 的相关属性 -->
<property name="hibernateProperties" >
<props>
<!--是否在后台显示Hibernate用到的SQL语句,开发时设置为true,便于差错,
程序运行时可以在Eclipse的控制台显示Hibernate的执行Sql语句。项目部署后可以设置为false,提高运行效率-->
<prop key="hibernate.show_sql">true</prop>
<!--hibernate.dialect 只是Hibernate使用的数据库方言,就是要用Hibernate连接那种类型的数据库服务器。-->
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<!--输出格式化后的sql,更方便查看 -->
<prop key="hibernate.format_sql">true </prop>
<!--
它包含4个属性:
* create : 会根据你的model类来生成表,但是每次运行都会删除上一次的表,重新生成表,哪怕2次没有任何改变
* create-drop : 根据model类生成表,但是sessionFactory一关闭,表就自动删除
* update : 最常用的属性,也根据model类生成表,即使表结构改变了,表中的行仍然存在,不会删除以前的行
* validate : 只会和数据库中的表进行比较,不会创建新表,但是会插入新值
-->
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- hibernate 映射文件 -->
<property name="mappingResources" >
<list>
<value>com/org/entity/Product.hbm.xml</value>
</list>
</property>
</bean>
<!-- 配置Action 的类 -->
<bean id="productAction" class="com.org.action.ProductAction" scope="prototype">
<!-- 手动注入service -->
<property name="productService" ref="productService"></property>
</bean>
<!-- 配置业务层的类 -->
<bean id="productService" class="com.org.service.productService">
<!-- 注入dao -->
<property name="productDao" ref="productDao"/>
</bean>
<!-- 配置dao的类 -->
<bean id="productDao" class="com.org.dao.ProductDao">
<!--注入 sessionFactoryBean-->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
jdbc.properties:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssh
jdbc.username=root
jdbc.password=root
struts.xml:
<?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>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
<action name="product_*" class="productAction" method="{1}">
<result name="index">index.jsp</result>
</action >
</package>
</struts>
Product.java:
package com.org.entity;
/**
* 商品的实体类
* @author kf0101
*
*/
public class Product {
private int id;
private String name;
private Double price;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Double getPrice() {
return price;
}
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://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.org.entity.Product" table="product">
<id name="id" column="id">
<generator class="native"/>
</id>
<property name="name" column="name" length="20"></property>
<property name="price" column="price"></property>
</class>
</hibernate-mapping>
ProductAction.java:
package com.org.action;
import sun.rmi.runtime.Log;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.org.entity.Product;
import com.org.service.productService;
/**
* 商品管理的action 类
* @author one
*
*/
public class ProductAction extends ActionSupport implements ModelDriven<Product>{
/**
*
*/
private static final long serialVersionUID = 1L;
//模型驱动
private Product product=new Product();
public Product getModel() {
return product;
}
//注入productService
private productService productService;
public void setProductService(productService productService) {
this.productService = productService;
}
/**
* 保存商品的方法
* @return
*/
public String save(){
productService.save(product);
return "index";
}
}
productService.java:
package com.org.service;
import org.springframework.transaction.annotation.Transactional;
import com.org.dao.ProductDao;
import com.org.entity.Product;
/**
* 商品管理的业务层
* @author kf0101
*
*/
@Transactional
public class productService {
//注入productDao
private ProductDao productDao;
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
public void save(Product product) {
System.out.println("productService----------------");
productDao.save(product);
}
}
ProductDao.java:
package com.org.dao;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.org.entity.Product;
/**
* 商品管理的持久化层
* @author kf0101
*
*/
public class ProductDao extends HibernateDaoSupport{
public void save(Product product) {
Session session = getSessionFactory().getCurrentSession();
session.save(product);
// System.out.println("ProductDao----------------");
//this.getHibernateTemplate().save(product);
}
}
addProduct.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>保存商品</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<s:form action="product_save" method="post" theme="simple" namespace="/">
<table border="1" width="400">
<tr>
<td>商品的名称</td>
<td><s:textfield name="name"/></td>
</tr>
<tr>
<td>商品的价格</td>
<td><s:textfield name="price"/></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="添加"/></td>
</tr>
</table>
</s:form>
</body>
</html>
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>商品添加成功</h1>
</body>
</html>
以上就是整合的全部代码。jar 上传,需要请联系我。