完成一个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;
    }
}

原文地址:https://www.cnblogs.com/zzuli/p/9250376.html

时间: 2024-07-30 15:12:35

完成一个springboot项目的完整总结-------二的相关文章

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

一. 项目的基础环境的搭建 1.javaJDK的安装和配置环境变量 2.mysql 3.eclipse 二.项目高级环境的搭建 使用maven前,一定要先安装JDK 1) 解压maven到briup目录下 2) 配置环境变量 MVN_HOME Path 3) 配置maven的本地仓库地址和远程镜像地址,打来maven的conf目录下的settings.xml进行配置 localRepository   用来存放依赖库 mirror   用来下载依赖 4).安装spring-boot脚手架 spr

SpringBoot专栏(二) -- 搭建第一个SpringBoot项目

解决问题: 搭建第一个SpringBoot项目? 怎样使用Maven进行Jar管理? SpringBoot Jar包简述? SpringBoot重要注解简述? 注意:使用SpringBoot2.0,JDK的版本必须在1.8以上,不然会出现一些不兼容的问题.搭建第一个SpringBoot项目(使用IDEA) 1. 搭建第一个SpringBoot项目(使用IDEA) 1.1 使用IDEA创建一个Maven工程 GroupId和ArtifactId两个加起来可以理解为项目的"坐标",他们保证

四、创建第一个springboot项目

简介 spring boot 它的设计目的就是为例简化开发,开启了各种自动装配,你不想写各种配置文件,引入相关的依赖就能迅速搭建起一个web工程.它采用的是建立生产就绪的应用程序观点,优先于配置的惯例. 建构准备 jdk 1.8 或以上 maven 3.0+ IntelliJ IDEA 打开Idea-> new Project ->Spring Initializr ->填写group.artifact ->钩上web(开启web功能)->点下一步就行了.创建完工程,工程的目

Idea新建一个SpringBoot项目

本文阅读加实践,耗时约一分钟 点击New Project 选择默认的即可. 选择项目中需要的maven依赖,这里我选择了web依赖(Springmvc) 然后下一步.下一步即可创建完成. 创建完成后,等springboot导入maven依赖. 启动main函数,在idea的控制台没有看到错误日志,访问 http://localhost:8080/ 至此一个SpringBoot项目就创建完成. 原文地址:https://www.cnblogs.com/gj-blog/p/10803591.html

第一个springboot项目启动报错Failed to configure a DataSource: &#39;url&#39; attribute is not specified and no embedded datasource could be configured.

报错内容具体如下 *************************** APPLICATION FAILED TO START *************************** Description: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Failed to determine a

springboot入门一,使用myEclipse新建一个springboot项目

1.环境信息 MyEclipse2015,jdk1.8,tomcat8 2.新建maven项目 2.1 新建一个web项目 2.2 生成的项目结构如下 3.配置pom.xml文件 3.1 pom.xml完整信息 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http

SpringBoot学习1:创建第一个SpringBoot项目

一.新建项目 二.打开项目的pom文件,在里面添加maven依赖 1 <!--springboot项目依赖的父项目--> 2 <parent> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-parent</artifactId> 5 <version>2.0.0.RELEASE</version>

SpringBoot入门篇--使用IDEA创建一个SpringBoot项目

随着技术的更新对于开发速度的追求,我们越来越不能忍受的是Spring框架对于集成开发以后大量的配置问题.所以SprigBoot应运而生,SpringBoot框架其实就是在Spring框架的外边包裹上了一层纸,包括减少配置文件,内置Tomcat服务器等等.在这里我们就使用IDEA工具为代表讲解一下SpringBoot在开发过程中会使用到的开发技术.官方推荐的编辑器是STS,STS就是对Eclipes做了封装,其实没有什么具体的改变,所以这里就是用更加快捷方便的开发工具IDEA,没有多大的影响. 创

用Eclipse+Maven快速创建一个SpringBoot项目

1. 去https://start.spring.io/默认下载一个maven项目 Generate Project(懂的话也可以更改其中的配置). 2. 打开Eclipse,导入刚刚解压的Maven项目. 3. 在demo下建立一个controller包,简单显示一点数据: package com.example.demo.controller; import org.springframework.boot.autoconfigure.SpringBootApplication; impor