时间:2017年08月16日星期三
说明:本文部分内容均来自慕课网。@慕课网:http://www.imooc.com
教学源码:无
学习源码:https://github.com/zccodere/s...
第一章:课程介绍
1-1 课程介绍
课程目录
1.ssh知识点回顾
2.搭建ssm开发环境
3.struts2整合spring
4.spring整合hibernate
5.案例:使用ssh框架开发人员管理系统
第二章:知识回顾
2-1 ssh知识点回顾
ssh框架的知识点回顾
第三章:环境搭建
3-1 创建项目
创建web项目,引入响应的jar包
struts2框架开发的相应的jar包
hibernate框架开发的相应的jar包
spring框架开发的相应的jar包
创建一个名为ssh的maven项目,pom文件如下
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myimooc</groupId>
<artifactId>ssh</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>ssh</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<struts2.version>2.5.12</struts2.version>
<spring.version>4.3.9.RELEASE</spring.version>
<aspectj.version>1.8.10</aspectj.version>
<aopalliance.version>1.0</aopalliance.version>
<hibernate.version>5.2.9.Final</hibernate.version>
<c3p0.version>0.9.5.2</c3p0.version>
<mysql.version>5.1.41</mysql.version>
<slf4j.version>1.7.25</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- 引入struts2框架-核心jar包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- 引入struts2框架-注解开发的jar包 -->
<!-- <dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
-->
<!-- 引入struts2框架-用于整合spring的jar包 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts2.version}</version>
</dependency>
<!-- 引入spring框架-核心jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入spring框架-整合web项目的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入spring框架-整合webmvc项目的jar包 -->
<!-- <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> -->
<!-- 引入spring框架-整合context项目的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入spring框架-整合beans项目的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入spring框架-整合jdbc项目的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入spring框架-整合orm项目的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入spring框架-整合tx项目的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入spring框架-整合test项目的jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入spring框架-AOP功能相关jar包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- 引入aspectj框架-AOP功能相关jar包 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- 引入aopalliance联盟-AOP功能相关jar包 -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>${aopalliance.version}</version>
</dependency>
<!-- 引入hibernate框架 -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
</dependency>
<!-- 引入c3p0数据库连接池 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!-- 引入mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- 引入slf4j框架 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- 引入slf4j框架整合log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<!-- 引入log4j框架 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
3-2 配置文件
引入配置文件
1.struts2框架的配置文件
web.xml
struts.xml
2.hibernate框架的配置文件
hibernate.cfg.xml(在此ssh中可以被省略)
映射文件
3.spring框架的配置文件
web.xml
applicationContext.xml
3-3 分层结构
项目分层结构如下
编写Product类
package com.myimooc.ssh.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* 商品类
* @author ZhangCheng on 2017-08-16
*/
@Entity
public class Product {
@Id
@GeneratedValue
private Integer pid;
private String pname;
private Double price;
@Override
public String toString() {
return "Product [pid=" + pid + ", pname=" + pname + ", price=" + price + "]";
}
public Integer getPid() {
return pid;
}
public void setPid(Integer pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
第四章:struts2整合spring
4-1 创建页面
编写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>Insert title here</title>
</head>
<body>
<h1>保存商品页面</h1>
<s:form action="product_save.action" 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>
4-2 后端开发
代码演示
1.编写ProductDao类
package com.myimooc.ssh.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;
import com.myimooc.ssh.domain.Product;
/**
* 商品管理的数据访问类
* @author ZhangCheng on 2017-08-17
*/
@Repository
public class ProductDao{
@Autowired
private HibernateTemplate hibernateTemplate;
/**
* 数据访问层保存商品的方法
*/
public void save(Product product) {
System.out.println("Dao中的save方法执行了...");
hibernateTemplate.save(product);
}
}
2.编写ProductService类
package com.myimooc.ssh.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.myimooc.ssh.dao.ProductDao;
import com.myimooc.ssh.domain.Product;
/**
* 商品管理的业务类
* @author ZhangCheng on 2017-08-17
*/
@Service
@Transactional
public class ProductService {
// struts和spring整合,按名称自动装配
@Autowired
private ProductDao productDao;
/**
* 业务层保存商品的方法
*/
public void save(Product product) {
System.out.println("Service中的save方法执行了...");
productDao.save(product);
}
}
3.编写ProductAction类
package com.myimooc.ssh.action;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.myimooc.ssh.domain.Product;
import com.myimooc.ssh.service.ProductService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 商品管理的控制类
* @author ZhangCheng on 2017-08-17
*/
@Controller
@Scope("prototype")
public class ProductAction extends ActionSupport implements ModelDriven<Product>{
private static final long serialVersionUID = 1L;
// struts和spring整合,按名称自动装配
@Autowired
private ProductService productService;
// 模型驱动使用的类
private Product product = new Product();
@Override
public Product getModel() {
return product;
}
/**
* 保存商品的执行方法:save
*/
public String save(){
System.out.println("Action中的save方法执行了...");
productService.save(product);
return NONE;
}
}
4-3 配置spring
struts2和spring整合的两种方式
Action的类由struts2自身去创建
Action的类交给Spring框架创建
<?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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd">
<!-- 引入外部的属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 自动扫描如下包 ,将带有注解的类纳入Spring容器管理 -->
<context:component-scan base-package="com.myimooc.ssh" />
<!-- 配置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.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置hibernate的相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 基于xml开发-加载hibernate中的映射文件 -->
<!-- <property name="mappingResources">
<list>
<value>com/myimooc/ssh/domain/Product.hbm.xml</value>
</list>
</property> -->
<!-- 基于注解开发 -->
<property name="packagesToScan">
<list>
<value>com.myimooc.ssh.domain</value>
</list>
</property>
</bean>
<!-- 配置hibernate模版类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
4-4 配置stauts2
<?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.5.dtd">
<struts>
<package name="ssh" extends="struts-default" namespace="/">
<action name="product_*" class="productAction" method="{1}"></action>
</package>
<!-- <constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="true" />
<package name="default" namespace="/" extends="struts-default">
注册拦截器
<interceptors>
<interceptor name="mytimer" class="com.myimooc.interceptor.TimerInterceptor"></interceptor>
</interceptors>
<action name="timer" class="com.myimooc.action.TimerAction">
<result>/success.jsp</result>
为Action显式引用拦截器后,默认的拦截器defaultStack不再生效,需要手工引用
<interceptor-ref name="defaultStack"></interceptor-ref>
引用拦截器
<interceptor-ref name="mytimer"></interceptor-ref>
</action>
</package> -->
</struts>
第五章:spring整合hibernate
5-1 映射文件
编写Product.hbm.xml
<?xml version="1.0"?>
<!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.myimooc.ssh.domain.Product" table="PRODUCT">
<id name="pid" type="java.lang.Integer">
<column name="PID" />
<generator class="assigned" />
</id>
<property name="pname" type="java.lang.String">
<column name="PNAME" />
</property>
<property name="price" type="java.lang.Double">
<column name="PRICE" />
</property>
</class>
</hibernate-mapping>
5-2 整合Hibernate
<!-- 配置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.user}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置hibernate的相关属性 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置hibernate的属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 基于xml开发-加载hibernate中的映射文件 -->
<!-- <property name="mappingResources">
<list>
<value>com/myimooc/ssh/domain/Product.hbm.xml</value>
</list>
</property> -->
<!-- 基于注解开发 -->
<property name="packagesToScan">
<list>
<value>com.myimooc.ssh.domain</value>
</list>
</property>
</bean>
<!-- 配置hibernate模版类 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。