完成一個springboot項目的完整總結-------二

來源:互聯網
上載者:User

標籤:The   request   錯誤   TE   位置   冗餘   object   color   cti   

我們接著上篇繼續寫,繼續進行springboot項目

一. swagger2
介面描述,測試每個介面是否有效

1. 添加依賴 pom.xml
在編輯pom.xml之前,要先關閉spring-boot項目
eclipse 編輯pom.xml。當編輯完畢儲存pom.xml後eclipse會幫我們自動下載依賴。eclipse這個自動化操作可能會出現一些問題。

mvn install

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>

2. 添加設定檔

 

WebConfig.java

package com.briup.apps.poll.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET","POST","PUT","OPTIONS","DELETE","PATCH").allowCredentials(true).maxAge(3600);
}
}

 

Swagger2.java

/**
* Project Name:poll
* File Name:Swagger2.java
* Package Name:com.briup.apps.poll.config
* Date:2018年6月10日下午6:22:51
* Copyright (c) 2018, [email protected] All Rights Reserved.
*
*/

package com.briup.apps.poll.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
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;

/**
* ClassName:Swagger2 <br/>
* Function: TODO ADD FUNCTION. <br/>
* Reason: TODO ADD REASON. <br/>
* Date: 2018年6月10日 下午6:22:51 <br/>
* @author lichunyu
* @version
* @since JDK 1.6
* @see
*/
@Configuration
@EnableSwagger2
public class Swagger2 {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.briup.apps.poll.web"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("yqg,課調系統API")
.description("Google搜尋引擎-請FQ進入,http://www.google.com")
.termsOfServiceUrl("http://www.google.com")
.version("1.0")
.build();
}
}

 

然後在瀏覽器輸入http://localhost:8080/swagger-ui.html,你就可以訪問你的介面,並且進行測試

補充一下:

package com.briup.apps.poll.config;import org.mybatis.spring.annotation.MapperScan;import org.springframework.context.annotation.Configuration;@Configuration@MapperScan("com.briup.apps.poll.dao")public class MybatisConfig {    }

這個設定檔MybatiesConfig.java的作用是為了能夠自動找到dao層中的Mapper

 

二.如果你是一個好的程式員或者想成為一個好的程式員,github是你必須用的工具

在eclipse上使用git的功能實現本地和遠程倉庫代碼的上傳和下載,首先你需要一個github帳號,建立一個倉庫和你的項目名一樣,然後在eclipse上開啟windows->preperences->git->configuation,選擇Reposity Settings,選擇你要上傳的項目,會自動給你配置一個github設定檔的位置,點擊open,進行配置

[core]    symlinks = false    repositoryformatversion = 0    filemode = false    logallrefupdates = true[remote "origin"]    url = https://github.com/qingguoYan/poll3.git    fetch = +refs/heads/*:refs/remotes/origin/*[branch "master"]    remote = origin    merge = refs/heads/master

注意:url是你的項目的github倉庫地址,配置完成後儲存,右擊項目->team->share Project,再次點擊team->commit,選擇你unstaged檔案下拉到staged,輸入commit message,提交你的代碼到本地倉庫,然後才能提交到遠程倉庫。

提交到遠程倉庫:team->removeto->push,填寫你要上傳的遠程倉庫地址,next,選擇master,add,next,輸入兩次上傳密碼,完成後再github上重新整理你的倉庫,發現你的倉庫有你的項目了,大功告成!

 

三.使用mybatis generator構建資料庫訪問層,但是只適用對單表的操作

 編寫資料訪問層代碼三種方式
1. 手動編寫(風格不統一,時間較長,冗餘過高)
2. 自己封裝
BaseDao
3. Mybatis generator(最優!)
根據表自動產生pojo,映射介面,對應檔
mybatis swagger ,外掛程式(用一次)
1) 在pom.xml添加外掛程式
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
</plugin>
2) 添加設定檔
1. 根據哪些表產生
2. 產生出來的pojo放到哪裡
3. 產生出來的Mapper介面放到哪裡
4. 產生出來的Mapper檔案放到哪裡
5. 如何產生
...

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><!-- mybatis-generator的核心設定檔 --><generatorConfiguration>  <classPathEntry location="C:\briup\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar"/>  <context id="DB2Tables" targetRuntime="MyBatis3">    <jdbcConnection driverClass="com.mysql.jdbc.Driver"        connectionURL="jdbc:mysql://127.0.0.1:3306/poll2.0"        userId="root"        password="root">    </jdbcConnection>    <!--指定產生的類型為java類型,避免資料庫中number等類型欄位 -->    <javaTypeResolver >      <property name="forceBigDecimals" value="false" />    </javaTypeResolver>    <!--自動產生的實體的存放包路徑 -->    <javaModelGenerator targetPackage="com.briup.apps.poll.bean" targetProject="./src/main/java">      <property name="enableSubPackages" value="true" />      <property name="trimStrings" value="true" />    </javaModelGenerator>    <!--自動產生的*Mapper.xml檔案存放路徑 -->    <sqlMapGenerator targetPackage="mapper"  targetProject="./src/main/resources">      <property name="enableSubPackages" value="true" />    </sqlMapGenerator>    <!--自動產生的*Mapper.java存放路徑 -->    <javaClientGenerator type="XMLMAPPER" targetPackage="com.briup.apps.poll.dao"  targetProject="./src/main/java">      <property name="enableSubPackages" value="true" />    </javaClientGenerator>    <!-- 映射配置 -->                 <table tableName="poll_school" domainObjectName="School" ></table>    <table tableName="poll_grade" domainObjectName="Grade" ></table>    <table tableName="poll_clazz" domainObjectName="Clazz" ></table>    <table tableName="poll_course" domainObjectName="Course" ></table>    <table tableName="poll_user" domainObjectName="User" ></table>    <table tableName="poll_options" domainObjectName="Options" ></table>    <table tableName="poll_question" domainObjectName="Question" ></table>    <table tableName="poll_questionnaire" domainObjectName="Questionnaire" ></table>    <table tableName="poll_qq" domainObjectName="QuestionnaireQuestion" ></table>    <table tableName="poll_survey" domainObjectName="Survey" ></table>    <table tableName="poll_answers" domainObjectName="Answers" ></table>  </context></generatorConfiguration>

 


3) 開始工作(關閉掉應用程式)
$ mvn -Dmybatis.generator.overwrite=true mybatis-generator:generate

完成後你會發現你的項目自動產生bean,dao,mapper.xml,非常節約時間,便於對單表進行操作。

四.統一開發相應介面

 

後台開發---》前端開發的響應模式統一,返回狀態有兩種
1.{
status:200
message:‘success‘
data:[]
}

2.{
status:500
message:‘資料庫異常‘
data:null
}

package com.briup.apps.poll.util;public class MsgResponse {    private Integer stauts;    //狀態代碼    200 成功 500代碼異常    private String message;    //錯誤、成功資訊    private Object data;    //資料    500 null        public static MsgResponse success(String message, Object data){        MsgResponse response = new MsgResponse();        response.setStauts(200);        response.setMessage(message);        response.setData(data);        return response;    }        public static MsgResponse error(String message){        MsgResponse response = new MsgResponse();        response.setStauts(500);        response.setMessage(message);        response.setData(null);        return response;    }            public Integer getStauts() {        return stauts;    }    public void setStauts(Integer stauts) {        this.stauts = stauts;    }    public String getMessage() {        return message;    }    public void setMessage(String message) {        this.message = message;    }    public Object getData() {        return data;    }    public void setData(Object data) {        this.data = data;    }}

 

完成一個springboot項目的完整總結-------二

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.