1.ssh整合可以分为两种,一种是普通整合
一.普通模式
1.引入相关的jar包
1.1 引入spring的jar
1.2 引入hibernate的jar
1.3引入struts2的相关jar(注意struts2的中的javassist_jar包hibernate也拥有。所以可以不添加进入)
1.4加入struts与spring集成的jar
1.5 加入数据库连接,目前使用的mysql所以jar是如下
2. 搭建struts的环境以及配置spring容器的配置文件的监听
在web.xml文件中写入配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>ssh3</display-name>
<!-- 配置struts的核心过滤器 -->
<filter>
<filter-name>struts</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 配置spring的监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 默认写法。加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
</web-app>
3.配置struts的配置文件,搭建工程对应的架构
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><!-- 这个配置将struts的跳转交给spring容器接管。-->
<constant name="struts.objectFactory" value="spring" />
<include file="com/seve/xml/dept.xml"></include>
</struts>
4、编写hibernate核心文件,并且引入hibernate引擎工具,快速搭建hibernate实体和数据库映射
hibernate.cfg.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test2</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
</session-factory>
</hibernate-configuration>
5.创建spring配置文件此时拥有三个配置文件
1.公共部分配置 applicationContext-commons.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!--1.配置数据源 -->
<bean class="com.mchange.v2.c3p0.ComboPooledDataSource" id="comboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test2"></property>
<property name="user" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 创建sessionFactory id为sessionFactory与util的hibernateUtils匹配 -->
<bean class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" id="sessionFactory">
<!-- 引入数据源 -->
<property name="dataSource" ref="comboPooledDataSource"></property>
<!-- 加载hibernate.cfg.xml文件 -->
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
<!--加入映射实体 -->
<property name="packagesToScan">
<list>
<value>com.seve.entity</value>
</list>
</property>
</bean>
<!-- 配置事务管理器 -->
<bean class="org.springframework.orm.hibernate4.HibernateTransactionManager" id="transactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务传播 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置aop -->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* com.seve.impl.*.*(..))" id="allmethods"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allmethods"/>
</aop:config></beans>
2.业务逻辑层 applicationContext-beans.xml
3.action控制层 applicationContext-actions.xml
此时这两个配置文件需要逻辑层代码才能编写。
所有先写接口
6.编写对应的接口
Deptservice和Empservice
public interface DeptService
{
//全查询
public List<Dept> getAll();
//增加
public void add(Dept dept);//查询一个
public Dept getOne(int pid);
//修改
public void update(Dept dept);
}public interface EmpService
{
// 全查询
public List<Emp> getAll();// 增加
public void add(Emp emp);// 查询一个
public Emp getOne(int eid);// 修改
public void update(Emp emp);// 删除
public void del(int eid);
}
7.编写hibernate数据库访问工具类
public class HibernateUtils
{
@Resource
private SessionFactory sessionFactory;public SessionFactory getSessionFactory()
{
return sessionFactory;
}public void setSessionFactory(SessionFactory sessionFactory)
{
this.sessionFactory = sessionFactory;
}public Session getSession()
{
return this.getSessionFactory().getCurrentSession();
}
}
8.编写dao
//注解表明这是数据访问层。能够获得session
@Repository
public class DeptDAO extends HibernateUtils
{
// 全查询
public List<Dept> getAll()
{
return this.getSession().createQuery("from Dept").list();
}// 增加
public void add(Dept dept)
{
this.getSession().save(dept);
}// 查询一个
public Dept getOne(int pid)
{
return (Dept) this.getSession().get(Dept.class, new Integer(pid));
}// 修改
public void update(Dept dept)
{
this.getSession().update(dept);
}
}
package com.seve.dao;
import java.util.List;
import org.springframework.stereotype.Repository;
import com.seve.entity.Emp;
import com.seve.util.HibernateUtils;//注解表明这是数据访问层。能够获得session
@Repository
public class EmpDAO extends HibernateUtils
{
// 全查询
public List<Emp> getAll()
{
List<Emp> ar=this.getSession().createQuery("from Emp a inner join fetch a.dept").list();
for (Emp emp : ar)
{
int x=emp.getEsex();
if(x==1)
{
emp.setSex("男");
}
else
{
emp.setSex("女");
}
}
return ar;
}// 增加
public void add(Emp emp)
{
this.getSession().save(emp);
}// 查询一个
public Emp getOne(int eid)
{
Emp emp=(Emp) this.getSession().get(Emp.class, new Integer(eid));
emp.setHob(emp.getEhob().split("、"));
return emp;
}// 修改
public void update(Emp emp)
{
this.getSession().update(emp);
}
//删除
public void del(int eid)
{
this.getSession().delete(this.getSession().get(Emp.class,new Integer(eid)));
}
}
9、编写实现类。引入dao
public class Deptimpl implements DeptService
{// 引入dao 进行set注入。交给spring容器
private DeptDAO deptdao;
public DeptDAO getDeptdao()
{
return deptdao;
}public void setDeptdao(DeptDAO deptdao)
{
this.deptdao = deptdao;
}@Override
public List<Dept> getAll()
{return deptdao.getAll();
}@Override
public void add(Dept dept)
{deptdao.add(dept);
}@Override
public Dept getOne(int pid)
{return deptdao.getOne(pid);
}@Override
public void update(Dept dept)
{
deptdao.update(dept);
}}
public class Empimpl implements EmpService
{
// set注入 交给spring容器接管
private EmpDAO empdao;public EmpDAO getEmpdao()
{
return empdao;
}public void setEmpdao(EmpDAO empdao)
{
this.empdao = empdao;
}@Override
public List<Emp> getAll()
{return empdao.getAll();
}@Override
public void add(Emp emp)
{
empdao.add(emp);}
@Override
public Emp getOne(int eid)
{return empdao.getOne(eid);
}@Override
public void update(Emp emp)
{empdao.update(emp);
}@Override
public void del(int eid)
{
empdao.del(eid);
}}
10.编写初步控制层action
public class DeptAction
{
private DeptService deptService;public DeptService getDeptService()
{
return deptService;
}public void setDeptService(DeptService deptService)
{
this.deptService = deptService;
}
}public class EmpAction
{
private EmpService empService;
private DeptService deptService;
public EmpService getEmpService()
{
return empService;
}
public void setEmpService(EmpService empService)
{
this.empService = empService;
}
public DeptService getDeptService()
{
return deptService;
}
public void setDeptService(DeptService deptService)
{
this.deptService = deptService;
}
}
11.此时我们就可以开始编写剩下的两个spring配置文件
springContext-beans.xml
<!-- 配置业务逻辑 -->
<bean class="com.seve.dao.DeptDAO" id="deptDAO">
<!-- 引入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean class="com.seve.dao.EmpDAO" id="empDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean class="com.seve.impl.Empimpl" id="empimpl">
<!-- 控制反转依赖的对象 -->
<property name="empdao" ref="empDAO"></property>
</bean>
<bean class="com.seve.impl.Deptimpl" id="deptimpl">
<!-- 控制反转依赖的对象 -->
<property name="deptdao" ref="deptDAO"></property>
</bean>
applicationContext-actions.xml
<!-- 控制 --> <!--scope 此处必须要用原型模式。强制刷新,默认为单例模式 -->
<bean class="com.seve.action.DeptAction" id="deptAction" scope="prototype">
<property name="deptService" ref="deptimpl"></property>
</bean>
<!--scope 此处必须要用原型模式。强制刷新,默认为单例模式 -->
<bean class="com.seve.action.EmpAction" id="empAction" scope="prototype">
<property name="deptService" ref="deptimpl"></property>
<property name="empService" ref="empimpl"></property>
</bean>
12.编写struts的各个模块xml文件
<struts>
<package name="dept" extends="struts-default">
<!-- 由于被spring容器接管,所以引用的是spring容器的id名 -->
<action name="dept_*" method="{1}" class="deptAction">
<result name="myall">/dept/all.jsp</result>
</action>
</package>
</struts>
剩余剩下操作web层,编写jsp页面。至此spring的应用完毕。