SSH整合步骤

来源:互联网 发布:淘宝店铺宝贝详情模板 编辑:程序博客网 时间:2024/06/03 21:24

1.导入jar包

2.编写实体类

  package com.tripleSix.tmallSSH.pojo;    public class Category {    private String name;    private int id;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    @Override    public String toString() {        return "Category [name=" + name + ", id=" + id + "]";    }    }

3.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="com.tripleSix.tmallSSH.pojo">    <class name="Category" table="category">        <id name="id" column="id" type="integer">        <generator class="native"></generator>        </id>        <property name="name" type="java.lang.String"></property>    </class>    </hibernate-mapping>

4.dao类

  package com.tripleSix.tmallSSH.dao.impl;    import org.hibernate.SessionFactory;    import org.springframework.orm.hibernate3.HibernateTemplate;    /**     * @author ly     *     */    public class DaoImpl extends HibernateTemplate{    /*private SessionFactory sessionFactory;*/    public void setSessionFactory(SessionFactory sessionFactory) {        //this.sessionFactory = sessionFactory;        super.setSessionFactory(sessionFactory);    }    }

5.service类

 package com.tripleSix.tmallSSH.service.impl;    import java.util.List;    import org.hibernate.criterion.DetachedCriteria;    import org.hibernate.criterion.Order;    import com.tripleSix.tmallSSH.dao.impl.DaoImpl;    import com.tripleSix.tmallSSH.pojo.Category;    import com.tripleSix.tmallSSH.service.CategoryService;    public class CategoryServiceImpl implements CategoryService {    private DaoImpl dao;    public void setDao(DaoImpl dao) {        this.dao = dao;    }    @Override    public List list() {        //创建一个DetachedCriteria实例        DetachedCriteria dc = DetachedCriteria.forClass(Category.class);        //结果集进行排序        dc.addOrder(Order.desc("id"));        return dao.findByCriteria(dc);    }    }

6.action类

package com.tripleSix.tmallSSH.action;    import java.util.List;    import java.util.Map;    import com.opensymphony.xwork2.ActionContext;    import com.tripleSix.tmallSSH.pojo.Category;    import com.tripleSix.tmallSSH.service.CategoryService;    public class CategoryAction {    private CategoryService categoryService;    public void setCategoryService(CategoryService categoryService) {        this.categoryService = categoryService;    }    public String list(){        List<Category> categorys = categoryService.list();        Map<String,Object> request = (Map<String, Object>) ActionContext.getContext().get("request");        request.put("categorys",categorys);        return "listCategory";    }    }

7.web.xml

主要配置了:
1.struts核心过滤器
2.spring监听器

`<?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" id="WebApp_ID" version="3.0">      <display-name>tmallSSH</display-name>    <welcome-file-list>        <welcome-file>index.jsp</welcome-file>    </welcome-file-list>      <!-- struts2核心过滤器 -->      <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>      <!--编码过滤  -->      <filter>        <filter-name>encodingFilter</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>      </filter>      <filter-mapping>        <filter-name>encodingFilter</filter-name>        <url-pattern>/*</url-pattern>      </filter-mapping>      <!-- spring 监听器 -->      <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>classpath:applicationContext.xml</param-value>      </context-param>      <context-param>          <param-name>webAppRootKey</param-name>          <param-value>tmallSSH.root</param-value>      </context-param>      <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>      </listener>      <listener>        <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>      </listener>    </web-app>

8.struts.xml

主要是对action的配置,action要放在package里。

`<?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>        <constant name="struts.i18n.encoding" value="UTF-8"></constant>        <package name="basicstruts" extends="struts-default">            <action name="listCategory" class="categoryAction" method="list">            <result name="listCategory">listCategory.jsp</result>            </action>        </package>    </struts>`

9.applicationContext.xml

spring和hibernate整合:
 1.把sessionFactory创建交给spring 2.使用spring的声明式事务
spring和structs整合:action的创建交给spring

主要配置了:
1.action,service,dao的创建
2.sessionfactory的创建,包括datasource,hibernate其他一些配置,mapping(原本在hibernate.cfg.xml中配置)
3.事务的配置,包括事务管理器,事务增强,切点

 <?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="categoryAction" class="com.tripleSix.tmallSSH.action.CategoryAction" scope="prototype">            <property name="categoryService" ref="categoryService"></property>         </bean>         <bean id="categoryService" class="com.tripleSix.tmallSSH.service.impl.CategoryServiceImpl">            <property name="dao" ref="dao"></property>         </bean>         <bean id="dao" class="com.tripleSix.tmallSSH.dao.impl.DaoImpl">            <property name="sessionFactory" ref="sessionFactory"></property>         </bean>         <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">            <property name="dataSource" ref="ds"></property>            <property name="mappingResources">                <value>com/tripleSix/tmallSSH/pojo/Category.hbm.xml</value>            </property>            <property name="hibernateProperties">            <!-- 其他配置 -->            <value>                hibernate.dialect=org.hibernate.dialect.MySQLDialect                hibernate.show_sql=true                hbm2ddl.auto=update            </value>            </property>         </bean>    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>        <property name="url" value="jdbc:mysql://localhost:3306/tmall?characterEncoding=UTF-8"></property>        <property name="username" value="root"></property>        <property name="password" value="root"></property>     </bean>    <!-- 事务管理器-->    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory"></property>    </bean>         <!-- 配置事务属性 -->     <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="*"/>        </tx:attributes>    </tx:advice>     <!-- 配置事务切点,并把切点和事务属性关联起来 -->      <aop:config proxy-target-class="true">        <aop:pointcut expression="execution(* com.tripleSix.tmallSSH.dao.impl.*.*(..))" id="txPointcut"/>        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>    </aop:config>    </beans>

10.jsp页面

 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>    <%@taglib uri="/struts-tags" prefix="s" %>    <table>    <tr>    <th>id</th>    <th>name</th>    </tr>    <s:iterator value="#request.list" var="c">        <tr>            <td>${c.id}</td>            <td>${c.name}</td>        </tr>    </s:iterator>    </table>
原创粉丝点击