Spring学习-10-ssh整合

1)引入SSH jar文件

Struts 核心 jar

Hibernate 核心 jar

Spring

core 核心功能

web 对web模块的支持

aop aop支持

orm 对hibernate支持

jdbc/tx  jdbc相关包、事务相关包

2)配置

web.xml

初始化 struts和spring功能

struts

配置请求路径与映.xml 射action的关系

Spring

IOC容器配置

bean-base.xml [共用信息]

bean-dao.xml

bean-service.xml

bean-action.xml

3)开发

entity/dao/service/action‘

实例代码

项目结构

entity

package com.cx.entity;

/**
 * Created by cxspace on 16-8-11.
 */
public class Dept {

    private int id;

    private String name;

    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;
    }
}

  

<?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 package="com.cx.entity">

    <class name="Dept" table="t_dept">
        <id name="id" column="deptId">
            <generator class="native"></generator>
        </id>

        <property name="name" column="deptName"></property>
    </class>

</hibernate-mapping>

  

package com.cx.entity;

/**
 * Created by cxspace on 16-8-11.
 */
public class Employee {

    private int id;
    private String empName;
    private double salary;
    private Dept dept;

    public Dept getDept() {
        return dept;
    }

    public void setDept(Dept dept) {
        this.dept = dept;
    }

    public String getEmpName() {
        return empName;
    }

    public void setEmpName(String empName) {
        this.empName = empName;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

  

<?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 package="com.cx.entity">

    <class name="Employee" table="t_employee">
        <id name="id" column="empId">
            <generator class="native"></generator>
        </id>

        <property name="empName"></property>
        <property name="salary"></property>

        <!--多对一配置-->

        <many-to-one name="dept" column="dept_id" class="Dept"></many-to-one>

    </class>

</hibernate-mapping>

  

dao

package com.cx.dao;

import com.cx.entity.Employee;
import org.hibernate.SessionFactory;

import java.io.Serializable;

/**
 * Created by cxspace on 16-8-11.
 */
public class EmployeeDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Employee findById(Serializable id){
        return (Employee)sessionFactory.getCurrentSession().get(Employee.class,id); //主键查询
    }
}

  

service

package com.cx.service;

import com.cx.dao.EmployeeDao;
import com.cx.entity.Employee;

import java.io.Serializable;

/**
 * Created by cxspace on 16-8-11.
 */
public class EmployeeService {

    private EmployeeDao employeeDao;

    public void setEmployeeDao(EmployeeDao employeeDao) {
        this.employeeDao = employeeDao;
    }

    public Employee findById(Serializable id){
        Employee emp = employeeDao.findById(id);
        return emp;
    }
}

  

action

package com.cx.action;

import com.cx.entity.Employee;
import com.cx.service.EmployeeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.util.Map;

/**
 * Created by cxspace on 16-8-11.
 */
public class EmployeeAction extends ActionSupport{

    private EmployeeService employeeService;

    public void setEmployeeService(EmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    @Override
    public String execute() throws Exception {

        int empId = 1;

        Employee employee = employeeService.findById(empId);

        //拿到request
        Map<String , Object> request = (Map<String, Object>) ActionContext.getContext().get("request");

        request.put("emp",employee);

        return SUCCESS;
    }
}

 

配置

<?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: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">

    <bean id="employeeAction" class="com.cx.action.EmployeeAction" scope="prototype">
        <property name="employeeService" ref="employeeService"></property>
    </bean>

    </beans>

  

<?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: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">

    <!--数据源对象c3p0连接池-->

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql:///learnHibernate"></property>
        <property name="user" value="root"></property>
        <property name="password" value="33269456.cx"></property>
        <property name="initialPoolSize" value="3"></property>
        <property name="maxPoolSize" value="10"></property>
        <property name="maxStatements" value="100"></property>
        <property name="acquireIncrement" value="2"></property>
    </bean>

    <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.MySQL5Dialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
            </props>
        </property>

        <!--加载的映射配置-->
        <property name="mappingLocations">

            <list>
                <value>classpath:com/cx/entity/*.hbm.xml</value>
            </list>

        </property>

      </bean>

    <!--事务配置-->
    <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="*" read-only="false"/>
        </tx:attributes>
    </tx:advice>

    <!--aop配置-->

    <aop:config>
        <aop:pointcut id="pt" expression="execution(* com.cx.service.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>

</beans>

  

<?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: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">

    <bean id = "employeeDao" class="com.cx.dao.EmployeeDao">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

</beans>

  

<?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: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">

    <bean id = "employeeService" class="com.cx.service.EmployeeService">
        <property name="employeeDao" ref="employeeDao"></property>
    </bean>

</beans>

  

<?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>

    <package name="emp" extends="struts-default">

        <action name="show" class="employeeAction" method="execute">
            <result name="success">/index.jsp</result>
        </action>

    </package>

</struts>

  

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!-- 配置spring的OpenSessionInView模式 【目的:JSP页面访问懒加载数据】 -->

    <filter>
        <filter-name>OpenSessionInView</filter-name>
        <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>OpenSessionInView</filter-name>
        <url-pattern>*.action</url-pattern>
    </filter-mapping>

    <!--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>

    <!--spring配置-->

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

</web-app>

  

<%--
  Created by IntelliJ IDEA.
  User: cxspace
  Date: 16-8-11
  Time: 下午9:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>查到用户信息</title>
  </head>
  <body>
   员工:${emp.empName}

   部门:${emp.dept.name}
  </body>
</html>

  

时间: 2024-10-06 22:33:20

Spring学习-10-ssh整合的相关文章

spring的使用-ssh整合

ssh整合-xml方式: 1.需要记住的三个jar包: spring-web-4.2.4.RELEASE.jar           ---保证项目启动时就实例化spring配置的对象(通过一个servletContext监听器ContextLoaderListener实现),保证整个项目只有一个工厂. struts2-spring-plugin-2.3.24.jar ---解决了struts2和spring的整合问题,将struts2中的action交给spring创建 spring-orm-

【j2ee spring】10、整合SSH框架(3)

整合SSH框架(3) Spring4+hibernate4+Struts2的整合,整合完成后我会把这个项目上传上去,但是我的建议是最好还是自己在自己的电脑上自己整合一下,我不保证一定没问题 前面那个,我们已经基本整合了SSH框架,但是还是有一些小小的瑕疵, 比如:PersonAction.java里面的 //获取实例,方法1 ServletContext sc = ServletActionContext.getRequest().getSession().getServletContext()

(摘录笔记)JAVA学习笔记SSH整合搭建项目

1:当然是导jar包啦: struts2: spring: hibernate: 至于这些jar包是什么作用,我想就不必我解释了,大家都懂得,ssh2基本的jar包: 还有一些其他jar包:struts2-spring-plugin-2.1.8.1.jar(struts2-spring整合使用的jar包) , c3p0-0.9.2-pre1.jar(使用链接池链接数据库) 2:添加struts.xml文件 <?xml version="1.0" encoding="UT

Spring学习笔记之整合struts

1.现有项目是通过 <action    path="/aaaaAction"                type="org.springframework.web.struts.DelegatingActionProxy"                name="plDraftPrLineForm"                scope="request"                parameter=&

Spring 学习笔记之整合Hibernate

Spring和Hibernate处于不同的层次,Spring关心的是业务逻辑之间的组合关系,Spring提供了对他们的强大的管理能力, 而Hibernate完成了OR的映射,使开发人员不用再去关心SQL语句,直接与对象打交道. Spring提供了对Hibernate的SessionFactory的集成功能. 1.建立Spring的bean.xml文件,在其里面配置 SessionFactory 以及事务,在hibernate.cfg.xml中不需要配置什么. <!-- 配置 Hibernate

Spring学习笔记之整合hibernate

1.web.xml里边要配置好对应的springxml的路径 <context-param> <param-name>contextConfigLocation</param-name> <param-value> conf/kernel/spring_kernel/spring-*.xml, conf/business/spring_business/spring-*.xml, conf/custom/spring_custom/spring-*.xml

【Java EE 学习第58-67天】【OA项目练习】【SSH整合JBPM工作流】【JBPM项目实战】

一.SSH整合JBPM JBPM基础见http://www.cnblogs.com/kuangdaoyizhimei/p/4981551.html 现在将要实现SSH和JBPM的整合. 1.添加jar包 (1)jbpm项目/lib目录下的所有jar包和根目录下的jbpm.jar包放入/WEB-INF/lib文件夹下,同时删除tomcat服务器/lib文件夹中的el-api.jar包. 注意:必须删除el-api.jar包,该jar包和jbpm项目中需要使用到的三个jar包冲突了:juel-api

SSH整合(struts2.3.24+hibernate3.6.10+spring4.3.2+mysql5.5+myeclipse8.5+tomcat6+jdk1.6)

终于开始了ssh的整合,虽然现在比较推崇的是,ssm(springmvc+spring+mybatis)这种框架搭配确实比ssh有吸引力,因为一方面springmvc本身就是遵循spring标准,所以不用像struts那样添加jar包去管理,其次是mybatis不能算一个完全的orm框架(因为mybatis依旧写的是面向关系的sql)但是相比ssh更加灵活和优化更加容易. 貌似偏题了,重新说回ssh整合. 首先引入jar包:(这里需要新建一个web项目,如果你不知道怎么在myeclipse怎么新

SSH整合 第四篇 Spring的IoC和AOP

这篇主要是在整合Hibernate后,测试IoC和AOP的应用. 1.工程目录(SRC) 2.IoC 1).一个Service测试类 1 /* 2 * 加入spring容器 3 */ 4 private ApplicationContext applicationContext = 5 6 new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml"); 7 public static void

【j2ee spring】12、整合SSH框架(终结版)

[j2ee spring]12.整合SSH框架(终结版) 最后,我们把整个项目的截图,代码发一下,大家不想下载那个项目的话,可以在这里看到所有的代码(因为那个项目需要一个下载积分,真不多= =,我觉得我搞了那么久,收点积分应该不过分吧...嘿嘿) 这里,我尽量用截图来搞,免得复制粘贴,怪烦的 一.项目整体截图 二.开始全部代码 Person.java Person.hbm.xml PersonService.java package cn.cutter_point.service; import