基于springboot的接口开发示例
最近接触了一些java开发的东西,为了能够更加深入的了解接口,更好的做接口测试,本人决定亲自去开发接口,了解接口内部的实现原理,废话不多说,直接上示例:

这次开发的是一个分页查询的接口,方法为get,基于http协议,为了增强以后接口开发的熟练度,特此记录一下springboot开发过程中,每一层都需要干些什么:

1.Controller层

这层专注于请求的处理以及请求参数的接收,同时会调用service层中实现好的方法,具体参考如下:

  1. /**
  2. * 添加Controller注解,声明此类为前置控制器
  3. */
  4. @Controller
  5. public class ApiCaseController {
  6. /**
  7. * 注入service类
  8. */
  9. @Autowired
  10. ApiService apiService;
  11. /**
  12. * 前端分别传入了apiUrl,project,pageSize,offset,sort,sortOrder参数
  13. * 注意:@RequestParam(value = "project",required = false)这表示接收到的project参数赋值给了形参project
  14. * required=false表明该参数可传可不传,如果没有该参数,就必须传入,不然就会报错
  15. * @ResponseBody 该注解表示把返回的实体转化为响应实体,如把HashMap转化成Json,或者直接把String返回给前端,而不通过视图解析器解析
  16. * @param apiUrl
  17. * @param project
  18. * @param pageSize
  19. * @param offset
  20. * @param sort
  21. * @param sortOrder
  22. * @return
  23. */
  24. @ApiOperation("获取接口列表")
  25. @RequestMapping(value="/getApiList",method = RequestMethod.GET)
  26. public @ResponseBody Map<String,Object> getApiList(@RequestParam(value = "address",required = false) String apiUrl,
  27. @RequestParam(value = "project",required = false) String project,
  28. int pageSize, int offset, String sort, String sortOrder){
  29. Map<String,Object> map=null;
  30. map=apiService.selectAllApi(apiUrl,project,pageSize,offset,sort,sortOrder);
  31. return map;
  32. }
  33. }

2.Service层

service层主要是业务逻辑层,是最重要的一部分,夹在Controller层和Dao层之间。

  1. @Service
  2. public interface ApiService {
  3. Map<String,Object> selectAllApi(String apiUrl, String project, int pageSize, int offset, String sort, String sortOrder);
  4. }

//

service层实现类,集成该service接口

  1. /**
  2. * @Service 注解声明此类为service层,赋值为apiService,以便注入到Controller层
  3. */
  4. @Service(value = "apiService")
  5. public class ApiServiceImpl implements ApiService{
  6. @Autowired
  7. APIDao apiDao;
  8. @Override
  9. public Map<String,Object> selectAllApi(String apiUrl, String project, int pageSize, int start, String sort, String sortOrder) {
  10. Map<String,Object> page=new HashMap<String,Object>();
  11. Map<String,Object> content1=new HashMap<String, Object>();
  12. Map<String,Object> content2=new HashMap<String, Object>();
  13. content1.put("apiUrl",apiUrl);
  14. content1.put("project",project);
  15. content1.put("pageSize",pageSize);
  16. content1.put("start",start);
  17. content1.put("sort",sort);
  18. content1.put("sortOrder",sortOrder);
  19. List<APIModel> apiList=apiDao.selectAllApi(content1);
  20. content2.put("apiUrl",apiUrl);
  21. content2.put("project",project);
  22. int total=apiDao.countApiList(content2);
  23. page.put("data",apiList);
  24. page.put("total",total);
  25. return page;
  26. }
  27. }

//

3.Dao接口

用于和数据库交互

  1. /**
  2. * Mapper注解
  3. */
  4. @Mapper
  5. public interface APIDao {
  6. /**
  7. * 根据项目id查询关联api
  8. * @param proid
  9. * @return
  10. */
  11. List<APIModel> selectAPIByPro(String proid);
  12. /**
  13. * 根据条件查询接口列表,分页
  14. * @param content
  15. * @return
  16. */
  17. List<APIModel> selectAllApi(Map<String, Object> content);
  18. int countApiList(Map<String, Object> content2);
  19. }

4.Mapper文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="cps.fs.APImanagerSys.dao.APIDao">
  4. <sql id="table">
  5. t_api
  6. </sql>
  7. <sql id="column">
  8. frowid,faid,fproid,fapiname,faurl,ftype,fcontenttype,fcasenum,fcreatetime,fupdatetime
  9. </sql>
  10. <sql id="updatecolum">
  11. faid,fproid,fapiname,faurl,ftype,fcontenttype,fcasenum,fcreatetime,fupdatetime
  12. </sql>
  13. <select id="selectAllApi" resultType="cps.fs.APImanagerSys.model.APIModel" parameterType="java.util.Map">
  14. SELECT
  15. <include refid="column"></include>
  16. FROM
  17. <include refid="table"></include>
  18. <where>
  19. <if test="apiUrl !=null">
  20. AND faurl like CONCAT("%",#{apiUrl},"%")
  21. </if>
  22. <if test="project !=null">
  23. AND project like CONCAT("%",#{project},"%")
  24. </if>
  25. </where>
  26. ORDER BY #{sort} #{sortOrder} LIMIT #{start},#{pageSize}
  27. </select>
  28. <select id="countApiList" resultType="Integer" parameterType="java.util.Map">
  29. SELECT count(*) FROM <include refid="table"></include>
  30. <where>
  31. <if test="apiUrl !=null">
  32. AND faurl like CONCAT("%",#{apiUrl},"%")
  33. </if>
  34. <if test="project !=null">
  35. AND project like CONCAT("%",#{project},"%")
  36. </if>
  37. </where>
  38. </select>
  39. </mapper>

/
转载至CSDN,作者:江楷丶
原文链接:https://blog.csdn.net/qq_39421886/article/details/82912018