基于springboot的接口开发示例
最近接触了一些java开发的东西,为了能够更加深入的了解接口,更好的做接口测试,本人决定亲自去开发接口,了解接口内部的实现原理,废话不多说,直接上示例:
这次开发的是一个分页查询的接口,方法为get,基于http协议,为了增强以后接口开发的熟练度,特此记录一下springboot开发过程中,每一层都需要干些什么:
1.Controller层
这层专注于请求的处理以及请求参数的接收,同时会调用service层中实现好的方法,具体参考如下:
/**
* 添加Controller注解,声明此类为前置控制器
*/
@Controller
public class ApiCaseController {
/**
* 注入service类
*/
@Autowired
ApiService apiService;
/**
* 前端分别传入了apiUrl,project,pageSize,offset,sort,sortOrder参数
* 注意:@RequestParam(value = "project",required = false)这表示接收到的project参数赋值给了形参project
* required=false表明该参数可传可不传,如果没有该参数,就必须传入,不然就会报错
* @ResponseBody 该注解表示把返回的实体转化为响应实体,如把HashMap转化成Json,或者直接把String返回给前端,而不通过视图解析器解析
* @param apiUrl
* @param project
* @param pageSize
* @param offset
* @param sort
* @param sortOrder
* @return
*/
@ApiOperation("获取接口列表")
@RequestMapping(value="/getApiList",method = RequestMethod.GET)
public @ResponseBody Map<String,Object> getApiList(@RequestParam(value = "address",required = false) String apiUrl,
@RequestParam(value = "project",required = false) String project,
int pageSize, int offset, String sort, String sortOrder){
Map<String,Object> map=null;
map=apiService.selectAllApi(apiUrl,project,pageSize,offset,sort,sortOrder);
return map;
}
}
2.Service层
service层主要是业务逻辑层,是最重要的一部分,夹在Controller层和Dao层之间。
@Service
public interface ApiService {
Map<String,Object> selectAllApi(String apiUrl, String project, int pageSize, int offset, String sort, String sortOrder);
}
//
service层实现类,集成该service接口
/**
* @Service 注解声明此类为service层,赋值为apiService,以便注入到Controller层
*/
@Service(value = "apiService")
public class ApiServiceImpl implements ApiService{
@Autowired
APIDao apiDao;
@Override
public Map<String,Object> selectAllApi(String apiUrl, String project, int pageSize, int start, String sort, String sortOrder) {
Map<String,Object> page=new HashMap<String,Object>();
Map<String,Object> content1=new HashMap<String, Object>();
Map<String,Object> content2=new HashMap<String, Object>();
content1.put("apiUrl",apiUrl);
content1.put("project",project);
content1.put("pageSize",pageSize);
content1.put("start",start);
content1.put("sort",sort);
content1.put("sortOrder",sortOrder);
List<APIModel> apiList=apiDao.selectAllApi(content1);
content2.put("apiUrl",apiUrl);
content2.put("project",project);
int total=apiDao.countApiList(content2);
page.put("data",apiList);
page.put("total",total);
return page;
}
}
3.Dao接口
用于和数据库交互
/**
* Mapper注解
*/
@Mapper
public interface APIDao {
/**
* 根据项目id查询关联api
* @param proid
* @return
*/
List<APIModel> selectAPIByPro(String proid);
/**
* 根据条件查询接口列表,分页
* @param content
* @return
*/
List<APIModel> selectAllApi(Map<String, Object> content);
int countApiList(Map<String, Object> content2);
}
4.Mapper文件
<?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="cps.fs.APImanagerSys.dao.APIDao">
<sql id="table">
t_api
</sql>
<sql id="column">
frowid,faid,fproid,fapiname,faurl,ftype,fcontenttype,fcasenum,fcreatetime,fupdatetime
</sql>
<sql id="updatecolum">
faid,fproid,fapiname,faurl,ftype,fcontenttype,fcasenum,fcreatetime,fupdatetime
</sql>
<select id="selectAllApi" resultType="cps.fs.APImanagerSys.model.APIModel" parameterType="java.util.Map">
SELECT
<include refid="column"></include>
FROM
<include refid="table"></include>
<where>
<if test="apiUrl !=null">
AND faurl like CONCAT("%",#{apiUrl},"%")
</if>
<if test="project !=null">
AND project like CONCAT("%",#{project},"%")
</if>
</where>
ORDER BY #{sort} #{sortOrder} LIMIT #{start},#{pageSize}
</select>
<select id="countApiList" resultType="Integer" parameterType="java.util.Map">
SELECT count(*) FROM <include refid="table"></include>
<where>
<if test="apiUrl !=null">
AND faurl like CONCAT("%",#{apiUrl},"%")
</if>
<if test="project !=null">
AND project like CONCAT("%",#{project},"%")
</if>
</where>
</select>
</mapper>
/
转载至CSDN,作者:江楷丶
原文链接:https://blog.csdn.net/qq_39421886/article/details/82912018