zoukankan      html  css  js  c++  java
  • SSM+ElementUI综合练习和Swagger和postman的使用(第二天)

    1.SSM传统项目搭建(适合小项目)

    2.SSM分模块搭建项目(适用于比较大的项目)

    a)       配置文件的拆分

       i.    Web.xml配置(web模块)

    <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"
    version="3.0">
    <display-name>crm</display-name>
    <!-- Spring的配置文件 -->
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-service.xml</param-value>
    </context-param>
    <!--Spring监听器 ApplicationContext 载入 -->
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring MVC 核心配置开始 -->
    <servlet>
    <servlet-name>springmvc</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- 编码过滤器 -->
    <filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
    </web-app>

      ii.            Spring.xml配置(applicationContext-mybatis)(Mapper模块:数据源配置,Mapper配置)

    <?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">

    <!--扫描的包-->
    <context:component-scan base-package="cn.itsource.crm.service"/>

    <!-- Jdbc配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!-- 数据源dataSource -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
    <!--maxActive: 最大连接数量 -->
    <property name="maxActive" value="150" />
    <!--minIdle: 最小空闲连接 -->
    <property name="minIdle" value="5" />
    <!--maxIdle: 最大空闲连接 -->
    <property name="maxIdle" value="20" />
    <!--initialSize: 初始化连接 -->
    <property name="initialSize" value="30" />
    <!--maxWait: 超时等待时间以毫秒为单位 1000等于60秒 -->
    <property name="maxWait" value="1000" />
    <!-- 在空闲连接回收器线程运行期间休眠的时间值,以毫秒为单位. -->
    <property name="timeBetweenEvictionRunsMillis" value="10000" />
    <!-- 在每次空闲连接回收器线程(如果有)运行时检查的连接数量 -->
    <property name="numTestsPerEvictionRun" value="10" />
    <!-- 1000 * 60 * 30 连接在池中保持空闲而不被空闲连接回收器线程 -->
    <property name="minEvictableIdleTimeMillis" value="10000" />
    <property name="validationQuery" value="SELECT NOW() FROM DUAL" />
    </bean>

    <!--Mybatis核心对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 注入数据源 -->
    <property name="dataSource" ref="dataSource" />
    <!-- 配置mybatis (mapper)映射器路径 -->
    <property name="mapperLocations" value="classpath*:cn/itsource/crm/mapper/*Mapper.xml" />
    <!-- 配置mybatis 类型别名 -->
    <property name="typeAliasesPackage">
    <value>
    cn.itsource.crm.domain
    cn.itsource.crm.query
    </value>
    </property>
    </bean>

    <!--注入映射器,一劳永逸的做法-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="cn.itsource.crm.mapper"></property>
    </bean>

    <!--事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--以注解的方式进行事务管理-->
    <tx:annotation-driven transaction-manager="transactionManager"/>


    </beans>

     iii. SpringMVC.xml配置(applicationContext-mvc)(web模块)

    <?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:mvc="http://www.springframework.org/schema/mvc"
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
    <context:component-scan base-package="cn.itsource.crm.web.controller" />

    <!--swagger交给Spring管理-->
    <context:component-scan base-package="cn.itsource.crm.web.config" />

    <!-- 启动SpringMVC的注解功能 -->
    <mvc:annotation-driven/>

    <!--静态资源放行-->
    <mvc:default-servlet-handler/>

    <!-- 定义跳转的文件的前后缀 ,视图解析器配置-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".jsp" />
    </bean>

    <!-- 配置文件上传解析器 -->
    <bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    <!-- 默认编码 -->
    <property name="defaultEncoding" value="utf-8" />
    <!-- 文件大小最大值 -->
    <property name="maxUploadSize" value="10485760000" />
    </bean>
    </beans>

     iv.  Service.xml配置(applicationContext-service)(service模块)

    <?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">

    <!--扫描的包-->
    <context:component-scan base-package="cn.itsource.crm.service"/>

    <!-- Jdbc配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties" />

    <!--导入mapper层配置-->
    <import resource="classpath:applicationContext-mybatis.xml"/>

    <!--事务管理-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>

    <!--以注解的方式进行事务管理-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

    jdbc.properties(mapper模块的resources里面)
    jdbc.driverClassName=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql://localhost:3306/aisell
    jdbc.username=root
    jdbc.password=123456

    b)       各个模块里面代码的主要内容

                         i.            注意我们父项目没有一句Java代码,只是对项目进行分类

                       ii.            模块:basic-util;工具类:Ajaxresult PageList StringUtils

                      iii.            模块:basic-core:核心模块,baseMapper,baseDomain,baseService

                      iv.            项目名-common:项目对应的公共东西,domian,query

                       v.            项目名-Mapper:映射器,*Mapper.java *Mapper.xml

                      vi.            项目名-service:业务模块

                     vii.            项目名-Web:web模块提供数据(接口层)

    c)        Basic-util工具类

       i. AjaxResult工具类

      ii. PageResult工具类

    4.Restful风格

    a)       http协议请求方式

      i.            put:添加

     ii.            get:查询一条

     iii.            post:修改

     iv.            delete:删除

    v.            patch:查询所有

    5.Swagger自动生成接口文档给前端人员

    a)       导包
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${springfox.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${springfox.version}</version>
    </dependency>

    b)       定义一个类Swagger类,交给Spring管理,配置扫描swagger
    <!--swagger交给Spring管理-->
    <context:component-scan base-package="cn.itsource.crm.web.config" />

    6.Postman后端测试(测试请求工具),后端人员不用写页面

    a) AjaxResuly工具类

    package cn.itsource;

    public class AjaxResult<T> {

    private boolean success = true;
    private String message = "操作成功!";
    public boolean isSuccess() {
    return success;
    }
    //链式编程,可以继续. 设置完成后自己对象返回
    public AjaxResult setSuccess(boolean success) {
    this.success = success;
    return this;
    }
    public String getMessage() {
    return message;
    }
    public AjaxResult setMessage(String message) {
    this.message = message;
    return this;
    }
    //默认成功
    public AjaxResult() {
    }
    //失败调用
    public AjaxResult(String message) {
    this.success = false;
    this.message = message;
    }
    //不要让我创建太多对象
    public static AjaxResult me(){
    return new AjaxResult();
    }
    public static void main(String[] args) {
    AjaxResult.me().setMessage("xxx").setSuccess(false);
    }

    b)       查询一条数据用get请求,
    Controller层请求@RequestMapping(value = "{id}", method = RequestMethod.GET)
    public AjaxResult findOne(@PathVariable Long id) {
        try {
            Dept dept = service.queryOne(id);
            System.out.println(dept);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败!");
        }
        return AjaxResult.me();
    }

    c)        查询所有数据,PATCH请求
    //    查询所有数据
    @RequestMapping(method = RequestMethod.PATCH)
    @ResponseBody
    public List<Dept> findAll(){
        List<Dept> list = service.queryAll();
        return list;
    }

    d)       删除一条数据,Delete请求
    @ResponseBody
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public AjaxResult delete(@PathVariable Long id){
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败了!");
        }
        return AjaxResult.me();
    }

    e)       修改数据,POST请求
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    //对象,传对象是用@RequestBody
    public AjaxResult update(@RequestBody Dept dept){
        try {
            service.update(dept);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("失败!");
        }
        return AjaxResult.me();
    }

    f)  保存数据用,PUT请求

    @RequestMapping(value = "/save", method = RequestMethod.PUT

    @ResponseBody

    public AjaxResult save(@RequestBody Dept dept){

        try {
            service.save(dept);
            System.out.println(dept);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("失败了").setSuccess(false);
        }
        return AjaxResult.me();
    }
    总结:参数传对象时,对象包括id,在RequestMapping里面传save路径就行,使用@RequestBody,传非对象是使用@PathVariable,在RequestMapping传入前台传的参数


    7.Postman  form表单发送请求,在传入的参数中不用加注解,直接传字段类型,如果是添加数据的话,必须用POST请求,PUT请求拿不到数据

    a)      

    b)      

  • 相关阅读:
    MongoDB Java连接---MongoDB基础用法(四)
    MongoDB用户,角色管理 --- MongoDB基础用法(三)
    Mongodb命令 --- MongoDB基础用法(二)
    MongoDB简介---MongoDB基础用法(一)
    Docker 私有仓库
    Dockerfile
    Docker部署Mysql, Tomcat, Nginx, Redis
    Docker 容器的数据卷
    封装的多功能多效果的RecyclerView
    安卓实现沉浸式效果,状态栏变色
  • 原文地址:https://www.cnblogs.com/8888-lhb/p/11417642.html
Copyright © 2011-2022 走看看