zoukankan      html  css  js  c++  java
  • SSH整合XML版(Spring+Struts2+Hibernate)

    整体架构:

    pom.xml 引入maven节点:

     <dependencies>
            <!--单测-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.3</version>
                <scope>test</scope>
            </dependency>
            <!--spring配置-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.2.0.RELEASE</version>
            </dependency>
            <!--aop使用的jar-->
            <dependency>
                <groupId> org.aspectj</groupId >
                <artifactId> aspectjweaver</artifactId >
                <version> 1.8.7</version >
            </dependency>
    
            <!--SpringWeb-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>4.1.8.RELEASE</version>
            </dependency>
    
            <!--JavaEE-->
            <dependency>
                <groupId>javaee</groupId>
                <artifactId>javaee-api</artifactId>
                <version>5</version>
            </dependency>
    
            <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>jstl</artifactId>
                <version>1.2</version>
                <scope>runtime</scope>
            </dependency>
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-tx</artifactId>
                <version>4.2.5.RELEASE</version>
            </dependency>
    
            <!--c3p0-->
            <dependency>
                <groupId>com.mchange</groupId>
                <artifactId>c3p0</artifactId>
                <version>0.9.5.2</version>
            </dependency>
    
            <!--hibernate jar包-->
            <!--jta的jar包-->
            <dependency>
                <groupId>javax.transaction</groupId>
                <artifactId>jta</artifactId>
                <version>1.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.hibernate</groupId>
                <artifactId>hibernate-core</artifactId>
                <version>5.0.6.Final</version>
            </dependency>
    
            <!--Spring-ORM-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-orm</artifactId>
                <version> 4.2.2.RELEASE</version>
            </dependency>
    
            <!--Oracle驱动的jar-->
            <dependency>
                <groupId>com.oracle</groupId>
                <artifactId>ojdbc6</artifactId>
                <version>11.2.0.1.0</version>
            </dependency>
    
            <!--struts2-->
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-core</artifactId>
                <version>2.3.4.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.apache.struts.xwork</groupId>
                <artifactId>xwork-core</artifactId>
                <version>2.3.4.1 </version>
            </dependency>
    
            <!-- struts2整合spring -->
            <dependency>
                <groupId>org.apache.struts</groupId>
                <artifactId>struts2-spring-plugin</artifactId>
                <version>2.3.4.1</version>
            </dependency>
    
            <!-- struts注解核心包 -->
            <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>**/*.xml</include>
                    </includes>
                </resource>
            </resources>
        </build>
    </project>
    

      

    bean层:

    Dog:

    
    
    public class Dog {
    
    
        private Integer did;
    
        private String dname;
    
        private Integer dage;
    
    
    
        public Integer getDid() {
            return did;
        }
    
        public void setDid(Integer did) {
            this.did = did;
        }
    
        public String getDname() {
            return dname;
        }
    
        public void setDname(String dname) {
            this.dname = dname;
        }
    
        public Integer getDage() {
            return dage;
        }
    
        public void setDage(Integer dage) {
            this.dage = dage;
        }
    }
    

      



    Dog.hbm.xml:
    <?xml version="1.0"?>
    <!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.bean">
    
        <!--实体 name=实体端内容 column=DB端内容-->
        <class name="Dog" table="Dog" >
            <id name="did" column="did">
                <!--主键生成策略 addignde 手动给赋值-->
                <generator class="native"></generator>
            </id>
            <property name="dname" column="dname"></property>
            <property name="dage" column="dage"></property>
    
      
        </class>
    
    
    </hibernate-mapping>
    

      



    Dao层:
    IDogDao:
    
    
    public interface IDogDao {
    
        //添加部门
        public void addDog(Dog dog);
    
    
    
    }
    

      


    IDogDaoImpl:
    public class IDogDaoImpl implements IDogDao {
    
        private SessionFactory sessionFactory;
    
    
        public void addDog(Dog dog) {
            Session session = sessionFactory.getCurrentSession();
            session.save(dog);
    
    
        }
    
        public SessionFactory getSessionFactory() {
            return sessionFactory;
        }
    
        public void setSessionFactory(SessionFactory sessionFactory) {
            this.sessionFactory = sessionFactory;
        }
    }
    

      



    service层:

    service:
    public interface IDogServices {
        //添加部门
        public void addDog(Dog dog);
    }
    

      



    IDogServicesImpl:
    public class IDogServicesImpl implements IDogServices {
    
        private IDogDao dao;
        @Transactional
        public void addDog(Dog dog) {
            dao.addDog(dog);
    
        }
    
        public IDogDao getDao() {
            return dao;
        }
    
        public void setDao(IDogDao dao) {
            this.dao = dao;
        }
    }
    

      



    action层:
    public class DogAction extends ActionSupport {
        private Dog dog;
        private IDogServices dogServices;
    
        public String addDog(){
            dogServices.addDog(dog);
    
    
            return SUCCESS;
        }
    
        public Dog getDog() {
            return dog;
        }
    
        public void setDog(Dog dog) {
            this.dog = dog;
        }
    
        public IDogServices getDogServices() {
            return dogServices;
        }
    
        public void setDogServices(IDogServices dogServices) {
            this.dogServices = dogServices;
        }
    }
    

      

    配置文件:

    applicationContext.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:context="http://www.springframework.org/schema/context"
           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/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    
    
    
    
    
       <!-- 配置数据源c3p0-->
       <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
          <property name="driverClass" value="${jdbc.driverClassName}"></property>
          <property name="jdbcUrl" value="${jdbc.url}"></property>
          <property name="user" value="${jdbc.username}"></property>
          <property name="password" value="${jdbc.password}"></property>
       </bean>
    <!--识别到jdbc.property-->
       <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    
    <!--SssionFactory -->
       <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="format_sql">true</prop>
                <prop key="dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <!--和线程绑定的Session-->
                <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
             </props>
          </property>
          <!--扫描小配置文件-->
          <property name="mappingDirectoryLocations" value="classpath:cn/happy/bean"></property>
          <!--扫描指定包下的注解类-->
          <!--<property name="packagesToScan">-->
             <!--<list>-->
                <!--<value>cn.happy.bean</value>-->
             <!--</list>-->
          <!--</property>-->
    
    
    
       </bean>
       <!--3.DAO    没有实现类-->
       <bean id="dogDao" class="cn.happy.dao.IDogDaoImpl">
          <property name="sessionFactory" ref="sessionFactory"></property>
       </bean>
    
       <!--4.service-->
       <bean id="dogServices" class="cn.happy.service.IDogServicesImpl">
          <property name="dao" ref="dogDao"></property>
       </bean>
    
       <bean id="dogAction" class="cn.happy.action.DogAction" scope="prototype">
          <property name="dogServices" ref="dogServices"></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>
    
    jdbc.properties:
    jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
    jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl
    jdbc.username=scott
    jdbc.password=tiger
    
    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>
        <!-- 配置文件中只要添加以下配置,那么以后修改配置文件不用重启tomcat -->
        <constant name="struts.devMode" value="true" />
        <constant name="struts.objectFactory" value="spring" />
    <package name="default" namespace="/" extends="struts-default">
    
        <action name="addDog" class="dogAction" method="addDog">
            <result name="success">/jsp/index.jsp</result>
        </action>
    
    
    </package>
    
    
    </struts>
    

      


    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>
    
      <!--上下文-->
      <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>
    

      



    login.jsp:
    <%@ taglib prefix="s" uri="/struts-tags" %>
    <%@ page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false"
             pageEncoding="UTF-8"%>
    <html>
    <body>
    <h2>你好,世界</h2>
    <div>
    
        <s:form action="addDog" method="POST" namespace="/">
          请输入用户名:  <s:textfield name="dog.dname"></s:textfield></br>
            请输入密码:<s:password name="dog.dage"></s:password>
            <s:submit value="登陆"></s:submit>
        </s:form>
    
    
    </div>
    
    </body>
    </html>
    

      

    
    



     
  • 相关阅读:
    node读写文件
    idea下建立bootstrap项目
    webpack
    Bootstrap-javascript插件
    Bootstrap-other内置组件
    Centos 修改当前路径显示为全路径
    深入理解java虚拟机(4)类加载的过程
    深入理解java虚拟机(3)垃圾收集器与内存分配策略
    深入理解java虚拟机(2)
    Scala学习笔记(3)
  • 原文地址:https://www.cnblogs.com/zjl0202/p/8508836.html
Copyright © 2011-2022 走看看