一、新建项目
1:
点击next
2、
3、下一步选择插件(可不选)
web下我选择spring web
项目新建完成
此时我的项目如下图
二、相关项目配置
1:打开swagger web服务
我们需要在Application启动程序上添加@EnableSwagger2注解同时添加swagger依赖,否则打开页面会显示
a、添加依赖1
<dependency>
<groupId>io.github.wilson-he</groupId>
<artifactId>swagger2-spring-boot-starter</artifactId>
<version>LATEST</version>
</dependency>
并且在yml文件中添加下面配置
swagger:
print-init: true
enabled: true
security-configuration:
client-id: client-1
client-secret: secretA
scope-separator: \,
use-basic-authentication-with-access-code-grant: true
dockets:
docket-api:
base-package: cn.yufeng.transport.controller
group-name: api
api-info:
title: 要展示的接口名称
description: RESTful接口描述
version: 版本号
license: 版权
global-parameters:
- name: token
description: Access Token
param-type: header
swagger为
b、添加依赖2
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
swagger页面
两种依赖任选一种
三、环境管理配置
1、application.yml与对应版本yml文件配置
到此可以启动开始写接口了
四、书写一个接口案例
1、项目目录设置如下
实体类:
package com.example.myproject.pojo;
import lombok.Data;
/**
* @Title: Company
* @author: andy
* @date: 2020/1/8 11:35
*/
@Data
public class Company {
private String id;
private String mySubjectId;
private String custSubjectName;
private String companyContact;
private String contactWay;
}
创建CompanyInfoMapper.java
package com.example.myproject.mapper;
import com.example.myproject.bean.DataResult;
/**
* @Title: CompanyInfoMapper
* @author: andy
* @date: 2020/1/8 11:44
*/
public interface CompanyInfoMapper {
DataResult getCompanyList();
}
创建对应的CompanyInfoMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.example.myproject.mapper.CompanyInfoMapper">
<resultMap id="baseResultMap" type="com.example.myproject.pojo.Company">
<id column="id_" property="id" jdbcType="VARCHAR"/>
<result column="my_subject_id" property="mySubjectId" />
<result column="company_contact" property="companyContact" jdbcType="VARCHAR" />
<result column="contact_way" property="contactWay" jdbcType="VARCHAR" />
<result column="cust_subject_name" property="custSubjectName" jdbcType="VARCHAR" />
</resultMap>
<select id="getCompanyList" resultMap="baseResultMap" >
select id_, my_subject_id, company_contact, contact_way, cust_subject_name from set_company_info
</select>
</mapper>
创建CompanyInfoService.java
package com.example.myproject.service;
import com.example.myproject.bean.DataResult;
/**
* @Title: CompanyInfoService
* @author: andy
* @date: 2020/1/8 15:12
*/
public interface CompanyInfoService {
DataResult getCompanyList();
}
CompanyInfoServiceImpl.java
package com.example.myproject.service.impl;
import com.example.myproject.bean.DataResult;
import com.example.myproject.mapper.CompanyInfoMapper;
import com.example.myproject.service.CompanyInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @Title: CompanyInfoServiceImpl
* @author: andy
* @date: 2020/1/8 17:21
*/
@Service
public class CompanyInfoServiceImpl implements CompanyInfoService {
@Autowired
private CompanyInfoMapper companyInfoMapper;
@Override
public DataResult getCompanyList() {
return companyInfoMapper.getCompanyList();
}
}
创建CompanyInfoController.java
package com.example.myproject.controller;
import com.example.myproject.bean.DataResult;
import com.example.myproject.service.CompanyInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @Title: CompanyInfoController
* @author: andy
* @date: 2020/1/8 18:29
*/
@RestController
@RequestMapping("/companyInfo")
public class CompanyInfoController {
@Autowired
private CompanyInfoService companyInfoService;
@ApiOperation("获取企业列表")
@PostMapping(value = "/v1/getCompanyList")
public DataResult getCompanyList() {
return companyInfoService.getCompanyList();
}
}
到此一个完整的接口开发完了,启动项目swagger显示如图
要记得在yml下添加mapper.xml的扫描
mybatis-plus:
mapper-locations: /mybatis/mapper/*Mapper.xml(mapper.xml的地址)
如果有参数填写完参数点击try it out 就能访问你的接口了
如果觉得不好,不要喷,请指出了,谢谢
来源:CSDN
作者:qq_29401089
链接:https://blog.csdn.net/qq_29401089/article/details/103913021