项目中为什么使用ssh框架
- struts主要起控制的作用,spring主要是解耦合的作用,hibernate主要起操作数据的作用
- struts2是一个基于mvc设计模式的web应用框架
,在mvc中struts作为控制器来建立模型与视图的数据交互,struts2以webwork为核心,采用拦截器的机制来处理用户的请求,是业务逻辑控制器与servletapi完全脱离开来
spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架,是轻量、非倾入、面向接口编程,有容器控制程序控制之间的依赖关系。当使用IOC的,一个对象依赖的其他对象会通过被动的方式传递进来,而不是这个对象自己本生去创建或者查找依赖对象。依赖注入,就是组件之间的依赖关系有容器在运行期间决定,即由容器动态的将某种依赖关系注入到组件之间起着解耦合的作用,减少代码段冗余,提高代码的复用性 - hibernate是一种数据持久化层,关系映射的工具,提供java类到数据表的映射,也提供了数据查询和恢复等机制,大大的减少了数据访问的复杂性,直接将对数据库的操作转化成对持久对象的操作。
struts spring hibernate在各个层次之间起到的作用
- struts负责WEB层,ActionFormBean接收网页中表单提交的数据然后通过Action进行处理,再Forward到对应的网页,在struts-config.xml中定义ActionServlet会加载。
- Spring负责业务层管理,即Service或Manager。
1.Service层为action提供统计的调用接口,封装持久层的DAO。
2.统一管理javaBean方法。
3.声明式事务管理。
4.集成Hiberante。 - Hiberante负责持久化层完成数据库的CRUD操作,为持久层提供OR-Mapping,它有一组*.hbm.xml文件和POJO是跟数据库中的表相对应的。
在ssh中对象调用的流程
jsp->Action->Service->DAO->Hibernate。数据的流向是ActionFormBean接受用户的数据,Action将数据从ActionFromBean中取出,封装成VO或PO,再调用业务层的Bean类,完成各种业务处理后再forward。而业务层Bean收到这个PO对象之后会调用DAO接口方法进行持久化操作。
分析整合要点
- Struts2:
jar web.xml(中央处理器) struts.xml - Hibernate:
jar hibernate.cfg.xml(spring整合后取消该文件) *.hbm.xml(映射文件) - Spring:
jar web.xml(spring容器的加载) - spring-struts2:
struts-spring-plugin.jar
constant name=”struts.objectFactory” value=”spring” (struts的对象工厂交给spring管理)
action class=”stuct”
applicationContext.xml
bean
commons-loggiog.jaar - spring-hibernate:
hibernate.cfg.xml文件内容迁移到applicationContext中
applicationContext.xml-bean name=”SessionFactory”
applicationContext.xml:对事物的控制—bean
具体实现
- 工程目录结构
- 考入jar包
1.版本控制
<properties>
<webVersion>3.1</webVersion>
<!-- 统一源码的编码方式 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!-- 统一各个框架版本 -->
<struts.version>2.5.10</struts.version>
<spring.version>4.3.8.RELEASE</spring.version>
<hibernate.version>5.1.7.Final</hibernate.version>
</properties>
2.考入jar包
<!-- SSH包 -->
<!-- Spring 核心依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring web依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring整合ORM框架依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Struts2 核心依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
</dependency>
<!-- Struts2和Spring整合依赖 -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>${struts.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>0.9.5</version>
</dependency>
<!-- AspectJ依赖 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.10</version>
</dependency>
<!-- SLF4J依赖 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- 导入java ee jar 包 -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- mysql的驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.0.8</version>
</dependency>
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-convention-plugin</artifactId>
<version>2.5.10</version>
</dependency>
- 配置web.xml文件
1.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>
2.加载spring容器
<!-- 加载spring容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value> /WEB-INF/spring.xml /WEB-INF/spring-*.xml</param-value>
</context-param>
- 配置struts.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- 认识系统中常用的常量
系统中定义了大量的常量,程序员可以通过修改常量,达到自己的需求。
-->
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<constant name="struts.action.extension" value="action,do,xc"></constant>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.objectFactory" value="spring"></constant>
<package name="stu" namespace="/stu" extends="struts-default">
<action name="studentAction_*" class="stuact" method="{1}">
<result name="success" type="redirectAction">studentAction_show.action</result>
<result name="index" >/index.jsp</result>
</action>
</package>
</struts>
- 配置数据库连接池jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
- 配置spring的spring.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 开启包扫描,并注册注解 -->
<context:component-scan base-package="com.it.*"/>
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置C3P0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 数据库连接相关信息 -->
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="driverClass" value="${jdbc.driverClass}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置Hibernate的SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 注入连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置Hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop><!-- 是否展示SQL -->
<prop key="hibernate.hbm2ddl.auto">update</prop><!-- 是否自动创建表结构 -->
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
</props>
</property>
<!-- 包扫描的方式加载注解类(推荐) -->
<property name="packagesToScan">
<list>
<value>com.it.*</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<!-- 注入SessionFactory -->
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- 配置事务增强 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="import*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
<!-- <tx:method name="*" read-only="false"/> -->
<!--这里如果觉着麻烦只要最后一行*就可以了,因为他会扫描所有的函数的 -->
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.it.services.*+.*(..))"/>
<!-- 适配切入点和事务增强 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
</aop:config>
</beans>
- 创建实体bean
package com.it.bean;
public class Student {
private String stu_id;
private String stu_name;
private String stu_sex;
private String stu_birth;
private String stu_addr;
public String getStu_id() {
return stu_id;
}
public void setStu_id(String stu_id) {
this.stu_id = stu_id;
}
public String getStu_name() {
return stu_name;
}
public void setStu_name(String stu_name) {
this.stu_name = stu_name;
}
public String getStu_sex() {
return stu_sex;
}
public void setStu_sex(String stu_sex) {
this.stu_sex = stu_sex;
}
public String getStu_birth() {
return stu_birth;
}
public void setStu_birth(String stu_birth) {
this.stu_birth = stu_birth;
}
public String getStu_addr() {
return stu_addr;
}
public void setStu_addr(String stu_addr) {
this.stu_addr = stu_addr;
}
public Student(String stu_id, String stu_name, String stu_sex, String stu_birth, String stu_addr) {
super();
this.stu_id = stu_id;
this.stu_name = stu_name;
this.stu_sex = stu_sex;
this.stu_birth = stu_birth;
this.stu_addr = stu_addr;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(String stu_id, String stu_name) {
super();
this.stu_id = stu_id;
this.stu_name = stu_name;
}
}
- Studnent.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.it.bean">
<class name="Student" table="stuinfo">
<id name="stu_id" column="stu_id">
<!-- 主键生成策略 -->
<generator class="assigned"></generator>
</id>
<property name="stu_name" column="stu_name"></property>
<property name="stu_sex" column="stu_sex"></property>
<property name="stu_birth" column="stu_birth"></property>
<property name="stu_addr" column="stu_addr"></property>
</class>
</hibernate-mapping>
- BaseDao
package com.it.dao;
import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class BaseDao<E> extends HibernateDaoSupport{
public void add(E e){
this.getHibernateTemplate().save(e);
}
@SuppressWarnings("unchecked")
public List<E> findSplit(int currentPage,int currentSize,String hql){
return (List<E>)this.getHibernateTemplate().execute(new HibernateCallback() {
@Override
public Object doInHibernate(Session session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
Query query=session.createQuery(hql);
int start=(currentPage-1)*currentSize;
query.setFirstResult(start);
query.setMaxResults(currentSize);
return query.list();
}
});
}
}
- StudentDao
package com.it.dao;
import java.util.List;
import com.it.bean.Student;
public class StudentDao extends BaseDao<Student>{
public void add(Student stu){
super.add(stu);
}
public List<Student> findSplit(int currentPage,int currentSize){
String hql="from Student";
return super.findSplit(currentPage, currentSize, hql);
}
}
- StudentService
package com.it.service;
import java.util.List;
import com.it.bean.Student;
import com.it.dao.StudentDao;
public class StudentService {
private StudentDao studao;
public void add(Student stu){
studao.add(stu);
}
public List<Student> findSplit(int currentPage,int currentSize){
return studao.findSplit(currentPage, currentSize);
}
public StudentDao getStudao() {
return studao;
}
public void setStudao(StudentDao studao) {
this.studao = studao;
}
}
- StudentAction
package com.it.action;
import java.util.List;
import com.it.bean.Student;
import com.it.service.StudentService;
import com.opensymphony.xwork2.ModelDriven;
public class StudentAction implements ModelDriven<Student>{
private Student stu=new Student();
private StudentService stusv;
private List<Student> list;
private int currentPage;
private int currentSize;
private int flag;
public String add(){
stusv.add(stu);
return "success";
}
public String show(){
if(flag==0){
currentPage=1;
currentSize=5;
}else if(flag==1){
currentSize=5;
currentPage=currentPage-1;
}else{
currentSize=5;
currentPage=currentPage+1;
}
list=stusv.findSplit(currentPage,currentSize);
return "index";
}
@Override
public Student getModel() {
// TODO Auto-generated method stub0
return stu;
}
public StudentService getStusv() {
return stusv;
}
public void setStusv(StudentService stusv) {
this.stusv = stusv;
}
public List<Student> getList() {
return list;
}
public void setList(List<Student> list) {
this.list = list;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCurrentSize() {
return currentSize;
}
public void setCurrentSize(int currentSize) {
this.currentSize = currentSize;
}
public int getFlag() {
return flag;
}
public void setFlag(int flag) {
this.flag = flag;
}
}
此刻变完成了一个ssh整合