SSHXML---sprong+springmvc+struts+hibirthary整合

项目目录

1.jar包  主要有struts和hibernte的jar和sprng-orm的jar

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>Y2166</artifactId>
        <groupId>cn.happy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>02SSHXML</artifactId>
    <packaging>war</packaging>
    <name>02SSHXML Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.3</version>
            <scope>test</scope>
        </dependency>
        <!--spring的jar包-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <!--SpringWeb-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.8.RELEASE</version>
        </dependency>
        <!--ServletAPI-->
        <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-api</artifactId>
            <version>7.0</version>
            <scope>provided</scope>
        </dependency>
        <!--jstl表达式-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
            <scope>runtime</scope>
        </dependency>
        <!--druid数据库驱动-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.29</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.2.5.RELEASE</version>
        </dependency>
        <!--spring orm-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>
        <!--hibernate核心jar-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.6.Final</version>
        </dependency>
        <!--oracle 事务-->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <!--oracle jdbc-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
        <!--Struts2-core-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>
        <!--xwork-core-->
        <dependency>
            <groupId>org.apache.struts.xwork</groupId>
            <artifactId>xwork-core</artifactId>
            <version>2.3.4.1</version>
        </dependency>
        <!--struts和spring整合-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>
        <!--struts注解支持jar包-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.4.1</version>
        </dependency>

    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

2.Dept实体类

public class Dept {
    private Integer deptno;
    private  String deptname;

3.xml文件Dept.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.happy.entity">
    <class name="Dept" table="Dept" schema="happytwo" >
        <id name="deptno"  column="deptno">
            <generator class="native"/>
        </id>
        <property name="deptname"/>
    </class>
</hibernate-mapping>

4.dao----》IDeptDao

package cn.happy.dao;

import cn.happy.entity.Dept;

/**
 * Created by CY on 2018/1/5.
 */
public interface IDeptDao {
    public  void  addDept(Dept dept);
}

 5.DeptDaoImpl

package cn.happy.dao;

import cn.happy.entity.Dept;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 * Created by CY on 2018/1/5.
 */
public class DeptDaoImpl implements IDeptDao {
    private SessionFactory sessionFactory;
    public void addDept(Dept dept) {
        Session session = sessionFactory.getCurrentSession();
        session.save(dept);
    }

    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }

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

6.service--->IDeptService

package cn.happy.service;

import cn.happy.entity.Dept;

/**
 * Created by CY on 2018/1/5.
 */
public interface IDeptService {
    public  void  addDept(Dept dept);
}

7.DeptServiceImpl

package cn.happy.service;

import cn.happy.entity.Dept;
import cn.happy.dao.IDeptDao;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by CY on 2018/1/5.
 */
public class DeptServiceImpl implements IDeptService {
    private IDeptDao deptDao;

    @Transactional
    public void addDept(Dept dept) {
         deptDao.addDept(dept);
    }

    public IDeptDao getDeptDao() {
        return deptDao;
    }

    public void setDeptDao(IDeptDao deptDao) {
        this.deptDao = deptDao;
    }
}

8.DeptAction

package cn.happy.action;

import cn.happy.entity.Dept;
import cn.happy.service.IDeptService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Created by CY on 2018/1/5.
 */
public class DeptAction extends ActionSupport {
    private Dept dept;
    private IDeptService deptService;
    public  String add(){
        deptService.addDept(dept);
        return SUCCESS;
    }

    public Dept getDept() {
        return dept;
    }

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

    public IDeptService getDeptService() {
        return deptService;
    }

    public void setDeptService(IDeptService deptService) {
        this.deptService = deptService;
    }
}

9.resources配置文件

1)applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.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">


    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!--2.识别到jdbc.properties文件-->
   <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>






    <bean  id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</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="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property>




    </bean>

    <!--dao-->
    <bean id="deptDao" class="cn.happy.dao.DeptDaoImpl" >
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>


    <!--service-->
    <bean id="deptService" class="cn.happy.service.DeptServiceImpl">
        <property name="deptDao" ref="deptDao"></property>
    </bean>

    <!--struct2配置-->
  <bean id="deptAction" class="cn.happy.action.DeptAction" scope="prototype">
      <property name="deptService" ref="deptService"></property>
  </bean>

    <!--事务管理器-->
    <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>


    <!--事务-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>






</beans>

2)jdbc.properties

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
jdbc.username=happytwo
jdbc.password=happytwo

3)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>


    <constant name="struts.devMode" value="true"></constant>
    <constant name="struts.objectFactory" value="spring"></constant>
    <package name="default" namespace="/" extends="struts-default">
        <action name="add" class="deptAction" method="add">
          <result name="success">/success.jsp</result>
        </action>

    </package>

</struts>

10.web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <!--上下文-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <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>
  <!--监听器-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

11.add.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加部门</title>
</head>
<body>
<form method="post" action="/add">
    部门名称:<input name="dept.deptname">
    <input type="submit" value="添加">
</form>
</body>
</html>

12.index.jsp

<%@taglib prefix="s" uri="/struts-tags" %>
<%@page contentType="text/html; charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加部门</title>
</head>
<body>
<h2>添加成功</h2>
</body>
</html>

13.运行结果

 

posted @ 2018-03-02 13:40  快乐的小银龙  阅读(320)  评论(0编辑  收藏  举报