1 maven版本的ssm

1.1 最简单的版本步骤:

(1) 创建maven web项目

(2) 在pom.xml中导入依赖的jar包

(3) 再写配置文件:

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 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.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>

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

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

    <!-- Jdbc配置文件 -->
    <context:property-placeholder location="classpath:db.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>

applicationContext-mvc.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: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" />

    <!-- 启动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>

db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///crm?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

log4j.properties

log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE
log4j.logger.cn.itsource=TRACE
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

(4) 搭建service层,mapper层,controller层

(5) 进行测试

1.2 抽取一些公共的内容的版本

(1) 抽取BaseMapper

  把公共的crud方法抽取到BaseMapper里面。

(2) 抽取IBaseService和BaseServiceImpl

  把公共的crud方法抽取到BaseService里面,通过BaseServiceImpl去实现BaseService里面的方法。

(3) 抽取BaseDomain

  抽取公共的id。

2 maven项目的分模块开发

2.1 为什么需要分模块开发

使用传统的ssm结构,在随着项目的进行,我们可能遇到下面一系列的问题:

(1) 大部分的domain或者一些service以及mapper在多个项目中是通用的

(2) pom.xml中的依赖越来越长

(3) build整个项目的时间越来越长,尽管你只是一直在web层工作,但是不得不build整个项目

(4)  某个模块,比如mapper,你只想让一些经验丰富的人来维护,但是现在每个开发者都能修改这个模

块,这导致关键模块的代码质量达不到你的要求

2.2 什么是分模块开发

    一个大项目拆分为多个小项目(maven模块)组成,而且它们是有依赖关系的。

2.3 怎么去分模块开发

写项目写代码尽量满足以下内容:

(1) 开闭原则

  对扩展开放

  对修改关闭-->公共的不要乱修改

(2) 高内聚:比如一个方法(方法就应该完成一个方法该干的事情) -- 最多40行

  低耦合:尽量的分层开发  mapper  service  controller(方便维护)

拆分:

  按照层次结构拆分

  按照业务功能拆分:电商、订单、物流……

(1) 代码拆分

  basic-util 工具类

  basic-core 公共内容

  crm-common 具体项目公共内容

  crm-mapper 项目里面具体mapper

  crm-service 项目里面service类

  crm-web 项目的controller层

  ……

  pss-common

 

 

(2) 配置文件拆分

web.xml  /  applicationContext-mvc.xml --> crm-web模块

applicationContext.xml --> crm-service模块

(3) 效果

 

 

 3 RESTful风格

  RESTful是一种开发理念是设计风格而不是标准。 REST描述的是在网络中client和server的一种交互形式;REST本身不实用,实用的是如何设计 RESTful API(REST风格的网络接口),一种万维网软件架构风格。

前端代码 --> 后端代码进行交互,交互的时候前端代码(axios)发送请求到后端代码

axios --> get / post / put / delete / patch

3.1 RESTful风格理解

<a href="/xxx"> --> get

<form method="post"> --> post

ajax --> get/post

除了get/post以外,还支持其他请求 put/delete/patch  --> http协议扩展出来的

没有RESTful以前:

  /addUser?name='Bob'&age=38  -- get

  /delete?id=1

=====================================华丽的分割线=============================================

RESTful风格:传输请求风格写法  --  http get/post/patch/put/delete  完成增删改查:

(1) put动作 + /user(资源路径) {"name":"Bob","age":28} --> 新增

(2) post动作 + /user(资源路径) {"name":"Bob","age":28} --> 修改

(3) delete 动作 +/user/1 --> 表示删除id为1的用户

(4) get 动作 + /user/10 --> 查询id为10的用户

(5) patch 动作 + /user --> 批量查询

 

那么为什么要使用RESTful?

安全性好一点,现在比较流行的风格,不会暴露动作

3.2 RESTful代码实现

以下为测试代码:

新增:

/**
 * 新增数据
 */
@RequestMapping(method = RequestMethod.PUT)
@ResponseBody
public AjaxResult save(@RequestBody Department department) {
    System.out.println("新增数据为:" + department);
    return new AjaxResult();
}

修改:

/**
 * 修改数据
 */
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public AjaxResult update(@RequestBody Department department) {
    System.out.println("修改数据为:" + department);
    return new AjaxResult();
}

删除:

/**
 * 删除数据
 */
@RequestMapping(value = "{id}", method = RequestMethod.DELETE)
@ResponseBody
public AjaxResult delete(@PathVariable Long id) {
    System.out.println("删除一条数据id为:" + id);
    return new AjaxResult();
}

查询一条:

/**
 * 查询一条数据
 */
@RequestMapping(value = "{id}", method = RequestMethod.GET)
@ResponseBody
public AjaxResult findOne(@PathVariable Long id) {
    System.out.println("查询一条数据id为:" + id);
    return new AjaxResult();
}

查询所有:

/**
 * 查询所有
 */
@RequestMapping(value = "/list",method = RequestMethod.PATCH)
@ResponseBody
public List<Department> findAll() {
    return departmentService.findAll();
}

4 swagger(前端人员使用很多)

实现

  <springfox.version>2.4.0</springfox.version>

    <dependencies>
        <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>
package cn.itsource.crm.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration //相当于写一个配置文件 application.xml
@EnableWebMvc // 开启springmvc
@EnableSwagger2 //开启swagger2
@ComponentScan(basePackages="cn.itsource.crm.web.controller")
public class SwaggerConfig {
    //相当于 <bean ><property name="">   </bean>
    @Bean
    public Docket api(){
        //生成接口信息
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.itsource.crm.web.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    //api的 注解 javaweb 文档的描述信息
    private ApiInfo apiInfo(){
        @SuppressWarnings("deprecation")
        ApiInfo info=new ApiInfo(
                "API接口测试文档",
                "接口测试",
                "1.0",
                "http://www.itsource.cn",
                "itsource",
                "123",
                "http://www.itsource.cn");
        return info;
    }
}

和spring整合,根据controller 生成的接口的文档,通过页面访问

运行:http://localhost/swagger-ui.html

5 postman

postman就是一个工具,可以来发送各种http请求,可以用它来测试http协议接口。

postman就是http协议接口测试工具。

测试 put/get/post/delete/patch这些请求:

6 前端vue-element-admin

基于 vue-cli 和 elementui 搭建出来一个模块框架,框架基本功能,路由,插件,国际化等

搭建模板:

(1) 解压文件 --修改名称

(2) 使用idea 打开

(3) 执行命令 npm install 安装依赖

(4) 启动 npm run dev

(5) 访问

 posted on 2019-12-05 20:00  wings丶xh  阅读(1061)  评论(0编辑  收藏  举报