• SSH(Spring Struts2 Hibernate)框架整合(xml版)


    案例描述:使用SSH整合框架实现部门的添加功能

    工程: Maven

    数据库:Oracle

    案例架构:

    1.依赖jar包pom.xml

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      2          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
      3     <parent>
      4         <artifactId>01MyBatis</artifactId>
      5         <groupId>cn.happy</groupId>
      6         <version>1.0-SNAPSHOT</version>
      7     </parent>
      8     <modelVersion>4.0.0</modelVersion>
      9     <artifactId>10SSHXML</artifactId>
     10     <packaging>war</packaging>
     11     <name>10SSHXML Maven Webapp</name>
     12     <url>http://maven.apache.org</url>
     13     <dependencies>
     14         <!--单测-->
     15         <dependency>
     16             <groupId>junit</groupId>
     17             <artifactId>junit</artifactId>
     18             <version>4.3</version>
     19             <scope>test</scope>
     20         </dependency>
     21         <!--spring配置-->
     22         <dependency>
     23             <groupId>org.springframework</groupId>
     24             <artifactId>spring-context</artifactId>
     25             <version>4.2.0.RELEASE</version>
     26         </dependency>
     27         <!--aop使用的jar-->
     28         <dependency>
     29             <groupId> org.aspectj</groupId >
     30             <artifactId> aspectjweaver</artifactId >
     31             <version> 1.8.7</version >
     32         </dependency>
     33 
     34         <!--SpringWeb-->
     35         <dependency>
     36             <groupId>org.springframework</groupId>
     37             <artifactId>spring-web</artifactId>
     38             <version>4.1.8.RELEASE</version>
     39         </dependency>
     40 
     41         <!--JavaEE-->
     42         <dependency>
     43             <groupId>javaee</groupId>
     44             <artifactId>javaee-api</artifactId>
     45             <version>5</version>
     46         </dependency>
     47 
     48         <dependency>
     49             <groupId>javax.servlet</groupId>
     50             <artifactId>jstl</artifactId>
     51             <version>1.2</version>
     52             <scope>runtime</scope>
     53         </dependency>
     54 
     55         <dependency>
     56             <groupId>org.springframework</groupId>
     57             <artifactId>spring-tx</artifactId>
     58             <version>4.2.5.RELEASE</version>
     59         </dependency>
     60 
     61         <!--c3p0-->
     62         <dependency>
     63             <groupId>com.mchange</groupId>
     64             <artifactId>c3p0</artifactId>
     65             <version>0.9.5.2</version>
     66         </dependency>
     67 
     68         <!--hibernate jar包-->
     69         <!--jta的jar包-->
     70         <dependency>
     71             <groupId>javax.transaction</groupId>
     72             <artifactId>jta</artifactId>
     73             <version>1.1</version>
     74         </dependency>
     75 
     76         <dependency>
     77             <groupId>org.hibernate</groupId>
     78             <artifactId>hibernate-core</artifactId>
     79             <version>5.0.6.Final</version>
     80         </dependency>
     81 
     82         <!--Spring-ORM-->
     83         <dependency>
     84             <groupId>org.springframework</groupId>
     85             <artifactId>spring-orm</artifactId>
     86             <version> 4.2.2.RELEASE</version>
     87         </dependency>
     88 
     89         <!--Oracle驱动的jar-->
     90         <dependency>
     91             <groupId>com.oracle</groupId>
     92             <artifactId>ojdbc6</artifactId>
     93             <version>11.2.0.1.0</version>
     94         </dependency>
     95 
     96         <!--struts2-->
     97         <dependency>
     98             <groupId>org.apache.struts</groupId>
     99             <artifactId>struts2-core</artifactId>
    100             <version>2.3.4.1</version>
    101         </dependency>
    102 
    103         <dependency>
    104             <groupId>org.apache.struts.xwork</groupId>
    105             <artifactId>xwork-core</artifactId>
    106             <version>2.3.4.1 </version>
    107         </dependency>
    108 
    109         <!-- struts2整合spring -->
    110         <dependency>
    111             <groupId>org.apache.struts</groupId>
    112             <artifactId>struts2-spring-plugin</artifactId>
    113             <version>2.3.4.1</version>
    114         </dependency>
    115 
    116         <!-- struts注解核心包 -->
    117         <dependency>
    118             <groupId>org.apache.struts</groupId>
    119             <artifactId>struts2-convention-plugin</artifactId>
    120             <version>2.3.4.1</version>
    121         </dependency>
    122     </dependencies>
    123     <build>
    124         <resources>
    125             <resource>
    126                 <directory>src/main/java</directory>
    127                 <includes>
    128                     <include>**/*.xml</include>
    129                 </includes>
    130             </resource>
    131         </resources>
    132     </build>
    133 </project>

    2.web.xml文件配置

     1 <!DOCTYPE web-app PUBLIC
     2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
     4 
     5 <web-app>
     6   <display-name>Archetype Created Web Application</display-name>
     7   <context-param>
     8     <param-name>contextConfigLocation</param-name>
     9     <param-value>classpath:applicationContext.xml</param-value>
    10   </context-param>
    11 
    12   <filter>
    13     <filter-name>struts</filter-name>
    14     <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    15   </filter>
    16   <filter-mapping>
    17     <filter-name>struts</filter-name>
    18     <url-pattern>/*</url-pattern>
    19   </filter-mapping>
    20 
    21   <!--监听器-->
    22   <listener>
    23     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    24   </listener>
    25 </web-app>

    3.resource目录配置文件

    3.1 applicationContext.xml

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:context="http://www.springframework.org/schema/context"
     4        xmlns:tx="http://www.springframework.org/schema/tx"
     5        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     6        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     7      http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd
     8      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
     9 ">
    10     <!--1.Datasource-->
    11     <!--1.配置数据源c3p0-->
    12     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    13         <property name="driverClass" value="${jdbc.driverClassName}"/>
    14         <property name="user" value="${jdbc.username}"/>
    15         <property name="password" value="${jdbc.password}"/>
    16         <property name="jdbcUrl" value="${jdbc.url}"/>
    17     </bean>
    18 
    19     <!--jdbc.properties-->
    20     <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    21 
    22     <!--2.SessionFactory         类:Local-->
    23     <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    24         <property name="dataSource" ref="dataSource"></property>
    25         <property name="hibernateProperties">
    26             <props>
    27                 <!--hibernate.xxxxxx必须以hibernate-->
    28                 <prop key="hibernate.show_sql">true</prop>
    29                 <prop key="hibernate.format_sql">true</prop>
    30                 <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
    31                 <!--with current thread bind session和线程绑定的session-->
    32                 <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
    33             </props>
    34         </property>
    35         <!--扫描小配置文件 所有的hbm文件-->
    36         <property name="mappingDirectoryLocations" value="classpath:cn/happy/entity"></property>
    37     </bean>
    38 
    39     <!--3.dao-->
    40     <bean id="deptDAO" class="cn.happy.dao.DeptDAOImpl">
    41         <property name="sessionFactory" ref="sessionFactory"></property>
    42     </bean>
    43 
    44     <!--4.service-->
    45     <bean id="deptService" class="cn.happy.service.DeptServiceImpl">
    46         <property name="deptDAO" ref="deptDAO"></property>
    47     </bean>
    48 
    49     <bean id="deptAction" class="cn.happy.action.DeptAction" scope="prototype">
    50         <property name="deptService" ref="deptService"></property>
    51     </bean>
    52 
    53     <!--  5.事务管理器    -->
    54     <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    55         <property name="sessionFactory" ref="sessionFactory"></property>
    56     </bean>
    57 
    58     <!--6.事务-->
    59     <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    60     <!--  <tx:advice id="txAdvice" transaction-manager="transactionManager">
    61           <tx:attributes>
    62               <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
    63           </tx:attributes>
    64       </tx:advice>
    65 
    66       <aop:config>
    67           &lt;!&ndash;配置了切点Pointcut&ndash;&gt;
    68          <aop:pointcut id="mypoint" expression="execution(* *..service.*.*(..))"/>
    69         &lt;!&ndash; 顾问&ndash;&gt;
    70          <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"></aop:advisor>
    71      </aop:config>-->
    72 </beans>

     3.2 jdbc.properties

    1 jdbc.driverClassName=oracle.jdbc.OracleDriver
    2 jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
    3 jdbc.username=liutao
    4 jdbc.password=liutao

    3.3  struts.xml

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE struts PUBLIC
     3         "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
     4         "http://struts.apache.org/dtds/struts-2.3.dtd">
     5 
     6 <struts>
     7     <constant name="struts.devMode" value="true"></constant>
     8     <!--将对象工厂的生成全交给Spring-->
     9     <constant name="struts.objectFactory" value="spring"></constant>
    10 
    11     <package name="default" namespace="/" extends="struts-default">
    12         <action name="add" class="deptAction" method="addDept">
    13             <result>/jsp/success.jsp</result>
    14         </action>
    15     </package>
    16 </struts>

    4.分层架构:

    entity:

        Dept:

     1 /**
     2  * Created by Administrator on 2017/12/24.
     3  * 部门表
     4  */
     5 public class Dept {
     6     private Integer deptno;  //部门编号
     7     private String dname;  //部门名称
     8     private String loc;   //部门位置
     9    
    10     //省略set  get方法
    11 }

       Dept.hbm.xml:

     1 <?xml version="1.0"?>
     2 <!DOCTYPE hibernate-mapping PUBLIC
     3         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     4         "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
     5 
     6 <hibernate-mapping package="cn.happy.entity">
     7     <!--实体 name=实体端内容  column=DB端内容-->
     8     <class name="Dept" table="DEPT" dynamic-update="true">
     9         <!--底层数据表对应的主键-->
    10         <id name="deptno" column="DEPTNO">
    11             <!--主键生成策略: assigned  程序员手动给值-->
    12             <generator class="native"/>
    13         </id>
    14         <property name="dname" column="DNAME"></property>
    15         <property name="loc" column="LOC"></property>
    16     </class>
    17 
    18 </hibernate-mapping>

    dao:

       IDeptDAO:

     1 public interface IDeptDAO {
     2     /**
     3      * @author: liutao
     4      * @Description: 添加部门
     5      * @param:
     6      * @return:
     7      * @throws:
     8      * @date: 2018/2/26 16:23
     9      */
    10     public void addDept(Dept dept);
    11 }

    DeptDAOImpl:

     1 public class DeptDAOImpl implements IDeptDAO{
     2     //植入sessionFactory
     3     private SessionFactory sessionFactory;
     4     public void addDept(Dept dept) {
     5         Session session = sessionFactory.getCurrentSession();
     6         session.save(dept);
     7     }
     8 
     9     public SessionFactory getSessionFactory() {
    10         return sessionFactory;
    11     }
    12 
    13     public void setSessionFactory(SessionFactory sessionFactory) {
    14         this.sessionFactory = sessionFactory;
    15     }
    16 }

    service:

       IDeptService:

     1 public interface IDeptService {
     2     /**
     3      * @author: liutao
     4      * @Description: 添加部门
     5      * @param:
     6      * @return:
     7      * @throws:
     8      * @date: 2018/2/26 16:23
     9      */
    10     public void addDept(Dept dept);
    11 }

       DeptServiceImpl:

     1 public class DeptServiceImpl implements IDeptService {
     2     private IDeptDAO deptDAO;
     3     @Transactional
     4     public void addDept(Dept dept) {
     5         deptDAO.addDept(dept);
     6     }
     7 
     8     public IDeptDAO getDeptDAO() {
     9         return deptDAO;
    10     }
    11 
    12     public void setDeptDAO(IDeptDAO deptDAO) {
    13         this.deptDAO = deptDAO;
    14     }
    15 }

    Action:

      DeptAction:

     1 public class DeptAction extends ActionSupport implements ModelDriven<Dept>{
     2     private Dept dept=new Dept();
     3     private IDeptService deptService;
     4 
     5     public String addDept(){
     6         deptService.addDept(dept);
     7         return SUCCESS;
     8     }
     9 
    10     public Dept getDept() {
    11         return dept;
    12     }
    13 
    14     public void setDept(Dept dept) {
    15         this.dept = dept;
    16     }
    17 
    18     public IDeptService getDeptService() {
    19         return deptService;
    20     }
    21 
    22     public void setDeptService(IDeptService deptService) {
    23         this.deptService = deptService;
    24     }
    25 
    26     public Dept getModel() {
    27         return dept;
    28     }
    29 }

    jsp页面:

    add.jsp

     1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
     2 <html>
     3 <head>
     4     <title>添加部门</title>
     5 </head>
     6 <body>
     7 <form method="post" action="/add">
     8 部门名称:<input name="dname" type="text"/>
     9     <input type="submit" value="保存"/>
    10 </form>
    11 </body>
    12 </html>

    success.jsp

    1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    2 <html>
    3 <head>
    4     <title>Title</title>
    5 </head>
    6 <body>
    7 <h1>添加成功</h1>
    8 </body>
    9 </html>
  • 相关阅读:
    Design + Code (iOS)
    Objective-C Programming (2nd Edition)
    iOS Programming The Big Nerd Ranch Guide (4th Edition)
    反射
    面向对象
    人狗大战
    数据结构初识(三级菜单)
    面向对象(组合)
    练习
    re模块
  • 原文地址:https://www.cnblogs.com/liutao1122/p/8492986.html
Copyright © 2020-2023  润新知