SSH整合

来源:互联网 发布:网络负面新闻消除方案 编辑:程序博客网 时间:2024/06/06 20:13
SSH整合:    1)引入SSHjar文件    Struts核心jar    Hibernate核心jar    Spring        Core 核心功能        WEB 对web功能支持        orm 对hibernate支持        jdbc/tx jdbc支持包,事务相关包    2)配置    web.xml        初始化struts功能,spring容器    Struts.xml 配置请求路径与映射action的关系    Spring.xml IOC容器的配置        bean-base.xml【配置公用信息】        bean-dao.xml        bean.service.xml        bean-action.xml    3)开发        Entity/Dao/Service/action

Entity.java

package cn.itcast.entity;/** * Created by 朱博文 on 2017/7/8. */public class Employee {    private int id;    private String empName;    private double salary;    private Dept dept;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getEmpName() {        return empName;    }    public void setEmpName(String empName) {        this.empName = empName;    }    public double getSalary() {        return salary;    }    public void setSalary(double salary) {        this.salary = salary;    }    public Dept getDept() {        return dept;    }    public void setDept(Dept dept) {        this.dept = dept;    }}----------

Employee.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 package="cn.itcast.entity">    <class name="Employee" table="t_employee">        <id name="id" column="empId">            <generator class="native"/>        </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>

EmployeeDao.java

package cn.itcast.dao;import cn.itcast.entity.Employee;import com.sun.xml.internal.ws.server.sei.SEIInvokerTube;import org.hibernate.SessionFactory;import java.io.Serializable;/** * Created by 朱博文 on 2017/7/8. */public class EmployeeDao {    private SessionFactory sessionFactory;    public void setSessionFactory(SessionFactory sessionFactory) {        this.sessionFactory = sessionFactory;    } public Employee findById(Serializable id) {     return sessionFactory.getCurrentSession().get(Employee.class, id); }}

EmployeeAction.java

package cn.itcast.action;import cn.itcast.entity.Employee;import cn.itcast.service.EmployeeService;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;import org.hibernate.engine.spi.Mapping;import java.util.Map;/** * Created by 朱博文 on 2017/7/8. */public class EmployeeAction extends ActionSupport {    private EmployeeService employeeService;    public void setEmployeeService(EmployeeService employeeService) {        this.employeeService = employeeService;    }    @Override    public String execute() throws Exception {        int empid = 13 ;        // 调用Service        Employee emp  = employeeService.findById(empid);        //保存到request        Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");        request.put("emp", emp);        return SUCCESS;    }}

bean-base.xml

<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       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.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">    <!--1.)数据源配置-->    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">        <property name="user" value="root"></property>        <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>        <property name="password" value="root"></property>        <property name="maxPoolSize" value="5"></property>        <property name="initialPoolSize" value="2"></property>        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>        <property name="acquireIncrement" value="2"></property>        <property name="maxStatements" value="100"/>    </bean>    <!--2).sessionFactory实例配置(不需要hibernate.cfg.xml文件)-->    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">        <!--a.dataSouece配置-->        <property name="dataSource" ref="dataSource"></property>        <!--b.bhibernate常用配置-->        <property name="hibernateProperties">            <props>                <prop key="dialect" >org.hibernate.dialect.MySQL5Dialect</prop>                <prop key="show_sql">true</prop>                <prop key="hibernate.hbm2ddl.auto">update</prop>            </props>        </property>        <!--c.映射配置-->        <property name="mappingLocations">            <list>                <value>classpath:cn/itcast/entity/*.hbm.xml</value>            </list>        </property>    </bean>    <!--################################################-->    <!--3).事务配置-->    <!--a 配置事务管理器类-->    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>    <!--b.配置事务增强(拦截到方法后如何管理事务)-->    <tx:advice transaction-manager="transactionManager" id="tx-advice">        <tx:attributes>            <tx:method name="*" read-only="false"/>        </tx:attributes>    </tx:advice>    <!--c.aop配置-->    <aop:config >        <aop:pointcut id="pt" expression="execution(* cn.itcast.service.*.*(..))"></aop:pointcut>        <aop:advisor advice-ref="tx-advice" pointcut-ref="pt"></aop:advisor>    </aop:config>    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven></beans>

bean-dao.xml

<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       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.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">    <bean id="emloyeeDao" class="cn.itcast.dao.EmployeeDao">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean></beans>

bean-service.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: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.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">    <bean id="employeeService" class="cn.itcast.service.EmployeeService">        <property name="employeeDao" ref="emloyeeDao"></property>    </bean></beans>

bean-action.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: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.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"><bean id="EmployeeAction" class="cn.itcast.action.EmployeeAction" scope="prototype">    <property name="employeeService" ref="employeeService"></property></bean></beans>

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>    <package name="emp" extends="struts-default">        <action name="show" class="cn.itcast.action.EmployeeAction" method="execute">            <result name="success">/index.jsp</result>        </action>    </package></struts>

web.xml

<?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的OpenSerssionInView模式【目的:jsp页面访问懒加载数据】-->    <filter>        <filter-name>OpenSessionInView</filter-name>        <filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>OpenSessionInView</filter-name>        <url-pattern>*.action</url-pattern>    </filter-mapping>    <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-base.xml</param-value>    </context-param>    <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener></web-app>
原创粉丝点击