完成一个springboot项目的完整总结-------二

我们接着上篇继续写,继续进行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("谷歌搜索引擎-请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;
    }
}

猜你喜欢

转载自www.cnblogs.com/zzuli/p/9250376.html