zoukankan      html  css  js  c++  java
  • SSH整合(注解式)

    SSH

    一 Spring

    1. Spring框架核心配置文件

        1.1 名称和位置没有固定要求,官方推荐使用applicationContext.xml作为配置文件名

        1.2 在Spring核心配置文件中引入Schema约束

    2. 创建对象

        2.1 xml配置方式:<bean id="" class="" />

        2.2 注解方式:四个注解,Component,Service,Controller,Repository

    3. 注入属性

        3.1 xml配置方式

        3.2 注解方式:两个直接,Resource,Autowired

    4. 使用ServletContext对象和监听器实现

        4.1 在服务器启动的时候,加载Spring,监听器的配置在web.xml中

        4.2 配置Spring的监听器

        4.3 指定Spring配置文件的位置

        4.4 要导入一个Spring整合web项目的jar包

    5. AOP的思想以及JdbcTemplate的使用

    二 Struts

    Struts是一个mvc框架

    1. Action相关的操作

        1.1 action创建三种方式:

            写一个POJO,并在其中写上 public String execute();方法

            创建一个类并实现Action接口

            使用的最多的方法是集成类ActionSupport

        1.2 配置action访问路径

            创建struts.xml配置文件,这个文件名称和位置固定src下面

        1.3 配置访问action的多个方法

            使用通配符的方式配置比较常见

        1.4 在action获取表单提交数据

            获取request对象,Struts2提供的API有ActionContext和ServletActionContext

            属性封装

            模型驱动,要实现ModelDriven接口

        1.5 在action中操作域对象

            使用ServletActionContext获取域对象

        1.6 在web.xml中配置Struts提供的过滤器

    三 Hibernate

    1. Hibernate的核心配置文件

        1.1 数据库信息、连接池配置

        1.2 Hibernate信息

        1.3 映射配置

        1.4 Hibernate核心配置文件

    如果单纯使用Hibernate框架,核心配置文件名称hibernate.cfg.xml并且一定要放在src下面,而hibernate和spring整合的时候,hibernate核心配置文件名称和位置没有固定要求的(因为到时候会有一个参数指定其位置)。

    2. Hibernate映射配置文件

        2.1 实体类和数据库表映射关系:使用的是ORM思想

    3. hibernate操作的步骤

        3.1 在Spring框架对hibernate框架进行封装,使用HibernateTemplate类

    接下来用代码来解释

    下面整合需要导入的包

       <!--Spring-ORM-->
    
            <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version> 4.2.2.RELEASE</version>
        </dependency>
    
      <!--Struts2注解支持jar包-->
    
            <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-convention-plugin</artifactId>
            <version>2.3.4.1</version>
           </dependency>

    spring的applicationContext.xml配置

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop"
           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/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
    ">
    
        <context:component-scan base-package="cn.happy"></context:component-scan>
    
    
    <!--
    1.数据源  c3p0
    -->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClass}"></property>
            <property name="jdbcUrl" value="${jdbc.url}"></property>
            <property name="user" 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>
        <!--
     3.形成SessionFactory
    -->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <!--方言-->
                  <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                    <!--是否打印sql-->
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
                </props>
            </property>
            <!--关联小配置-->
             <property name="packagesToScan" value="cn.happy.entity"></property>
        </bean>
    
    <!--
    7.事务管理器
    -->
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
    <!--
    8.事务真实配置
    -->
        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    
    
    </beans>

    entity中的实体类

    package cn.happy.entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity
    public class Dept {
        @Id
        @GeneratedValue
        private Integer deptno;
        @Column
        private String deptname;
    
        public Integer getDeptno() {
            return deptno;
        }
    
        public void setDeptno(Integer deptno) {
            this.deptno = deptno;
        }
    
        public String getDeptname() {
            return deptname;
        }
    
        public void setDeptname(String deptname) {
            this.deptname = deptname;
        }
    }


    package cn.happy.action;
    
    import cn.happy.entity.Dept;
    import cn.happy.service.IDeptService;
    import com.opensymphony.xwork2.Action;
    import org.apache.struts2.convention.annotation.Namespace;
    import org.apache.struts2.convention.annotation.ParentPackage;
    import org.apache.struts2.convention.annotation.Result;
    import org.springframework.context.annotation.Scope;
    import org.springframework.stereotype.Controller;
    
    import javax.annotation.Resource;
    
    @Controller("deptAction")
    @ParentPackage("struts-default")
    @Namespace("/")
    @Scope("prototype")
    public class DeptAction implements Action {
        private Dept dept;
        @Resource(name = "deptService")
        IDeptService service;
        @org.apache.struts2.convention.annotation.Action(value = "/add",results={@Result(name = "success",location = "/jsp/index.jsp")})
        public String execute() throws Exception {
            service.addDept(dept);
            return SUCCESS;
        }
    
        public Dept getDept() {
            return dept;
        }
    
        public void setDept(Dept dept) {
            this.dept = dept;
        }
    
        public IDeptService getService() {
            return service;
        }
    
        public void setService(IDeptService service) {
            this.service = service;
        }
    }

    添加成功后

    数据库

  • 相关阅读:
    springboot初始篇(一)
    SpringBoot使用数据库JdbcTemplate(三)
    java实现分页查询
    设计模式之单例模式
    ❤️考研数学公式❤️
    ❤️图的遍历❤️
    图的存储
    图的基本概念
    森林与二叉树的应用
    树相关的代码题
  • 原文地址:https://www.cnblogs.com/xuhaifeng017/p/8510252.html
Copyright © 2011-2022 走看看