SSM+ElementUI綜合練習和Swagger和postman的使用(次日)

1.SSM傳統項目搭建(適合小項目)前端

2.SSM分模塊搭建項目(適用於比較大的項目)java

a)       配置文件的拆分mysql

   i.    Web.xml配置(web模塊)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配置)spring

<?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模塊)sql

<?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模塊)apache

<?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 StringUtilsspring-mvc

                  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)      

相關文章
相關標籤/搜索