简单描述:因为前后端分离,开发完模块之后,接到team leader的指令,我这个渣渣javaer需要给前端人员返回一个接口,具体内容是课程列表json和分类列表json。emmmm,第一次写接口,心理是有点啪啪啪的,手误,怕怕的,完全不知道应该怎么写。不过,程序员从来都不会说做不到,能做到的是想方设法的去搞定他。最终,还是把它搞出来了,哈哈哈哈,experience有增长了一点点。

过程:

创建实体类,并且使用@Table和数据库表相对应,@Getter@Setter都是lombok包下的 @Id@Table@Column是javax.persistence包下的

//实体类Course @Column中的是表字段 要注意的是必须和表完全对应,表里有多少字段,类就有多少属性 
@Getter
@Setter
@Table(name = "table_course")
public class Course {
@Id
@Column(name = "course_id")
private String courseId;
@Column(name = "course_name")
private String courseName;
@Column(name = "course_code")
private String courseCode;
@Column(name = "course_type")
private String courseType;
@Column(name = "is_del")
private String isDel;//未删除 1 已删除 0
}  //实体类Dictionary
import lombok.Getter;
import lombok.Setter; import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Table; @Getter
@Setter
@Table(name = "table_dict")
public class Dictionary {
@Id
@Column(name = "dict_id")
private String dictId;
@Column(name = "dict_name")
private String dictName;
@Column(name = "dict_code")
private String dictCode;
@Column(name = "sort_no")
private String sortNo;
@Column(name = "parent_id")
private String parentId;
@Column(name = "description")
private String description;

Controller层

//controller层 
import xxx.xx.xxxxx.xxxx.xxxxx.ResultDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.List;
import java.util.Map;
@Api(value = "CourseController",tags = "课程相关查询")
@RestController
@RequestMapping("/course")
public class CourseController extends BaseController {
@Autowired
private CourseService courseService; @ApiOperation(value = "获取课程列表" notes= "获取课程列表")
@RequestMapping(value = "/queryList", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public ResultDto queryList(){
List<CourseVo> list = null;
try {
list = courseService.queryList();
if(list != null){
if(list.size()<1){
return ResultDto.success("返回结果无内容");
}
}
} catch (Exception e) {
e.printStackTrace();
return ResultDto.error();
}
return ResultDto.success(list);
} @ApiOperation(value = "获取课程分类列表",notes = "根据课程编码查询")
@RequestMapping(value = "/getCourseType",method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public ResultDto getCourseType(String code){
List<Map<String,String>> list = null;
try {
if (!"".equals(code) && code != null) {
list = courseService.getCourseType(code);
if(list != null){
if(list.size()<1){
return ResultDto.success("返回结果无内容");
}
}
}else{
return ResultDto.error("参数错误");
}
} catch (Exception e) {
e.printStackTrace();
return ResultDto.error();
}
return ResultDto.success(list);
}

Service层

//service层
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example; import javax.annotation.Resource; @Service
public class CourseService {
@Resource
private CourseMapper courseMapper;
@Resource
private DictionaryMapper dictionaryMapper; public List<CourseVo> queryList()throws Exception{
Example example = new Example(Course.class);
example.createCriteria().andEqualTo("isDel",1);
List<Course> list = courseMapper.selectByExample(example);
List<CourseVo> reList = new ArrayList<>();
if (list !=null){
for(Course obj:list){
CourseVo course = new CourseVo();
BeanUtils.copyProperties(obj,course);
reList.add(course);
}
}
return reList;
} public List<Map<String,String>> getCourseType(String code) throws Exception{
Dictionary obj = new Dictionary();
obj.setDictCode(code);
Dictionary dict = dictionaryMapper.selectOne(obj);
Example example = new Example(Dictionary.class);
example.createCriteria().andEqualTo("parentId",dict.getDictId());
List<Dictionary> list = DictionaryMapper.selectByExample(example);
List<Map<String,String>> reList = new ArrayList<>();
if(list != null){
for(Dictionary test:list){
Map<String,String> map = new HashMap<>();
map.put(test.getDictName(),test.getDictCode());
reList.add(map);
}
}
return reList;
}

对应的Mapper接口

//CourseMapper接口
import xxx.xx.xxxxxx.xxxx.util.MyMapper; public interface CourseMapper extends MyMapper<Course> {
}  //DictionaryMapper接口
import xxx.xx.xxxxxx.xxxx.util.MyMapper; public interface DictionaryMapper extends MyMapper<Dictionary>{
}

MyMapper<T>

//MyMapper接口
/*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016 abel533@gmail.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/ package xxx.xx.xxxxxx.xxxxx.util; import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper; /**
* 继承自己的 MyMapper
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
//TODO
//FIXME 特别注意,该接口不能被扫描到,否则会出错

注意:可以使用google浏览器的postman插件  或者 火狐浏览器的RestClient插件来进行接口的测试工作。输入地址的时候要确保和配置文件里的端口号对应。

总结:第一次开发接口,能顺利的搞出来真的是挺开心的,以前的时候就在好奇,经常听接口开发之类的,也会有疑问 如果自己来开发接口的话能不能开发出来。

相关注解详细参考:https://blog.csdn.net/xiaojin21cen/article/details/78654652

浅谈rest風格的接口开发的更多相关文章

  1. 浅谈使用 PHP 进行手机 APP 开发(API 接口开发)

    做过 API 的人应该了解,其实开发 API 比开发 WEB 更简洁,但可能逻辑更复杂,因为 API 其实就是数据输出,不用呈现页面,所以也就不存在 MVC(API 只有 M 和 C),那么我们来探讨 ...

  2. 以用户注册功能模块为例浅谈MVC架构下的JavaWeb开发流程

    JavaWeb应用开发,撇开分布式不谈,只讨论一个功能服务应用的开发,无论是使用原生的Servlet/JSP方案,还是时下的SSM架构,都有一套经过工程实践考验的最佳实践,这综合考虑了团队协作.项目管 ...

  3. 浅谈Bootstrap自适应功能在Web开发中的应用

    随着移动端市场的强势崛起,web的开发也变得愈发复杂,对于个体开发者来说,自己开发的网站,在电脑.手机.Pad等上面都要有正常的显示以及良好的用户体验.如果每次都要自己去调整网页去匹配各个不同的客户端 ...

  4. 浅谈C#抽象类和C#接口

    原文地址:http://www.cnblogs.com/zhxhdean/archive/2011/04/21/2023353.html 一.C#抽象类: C#抽象类是特殊的类,只是不能被实例化:除此 ...

  5. 浅谈Nutch插件机制(含开发实例)

    plugin(插件)为nutch提供了一些功能强大的部件,举个例子,HtmlParser就是使用比较普遍的用来分析nutch抓取的html文件的插件. 为什么nutch要使用这样的plugin系统? ...

  6. Android安全开发之启动私有组件漏洞浅谈

    0x00 私有组件浅谈 android应用中,如果某个组件对外导出,那么这个组件就是一个攻击面.很有可能就存在很多问题,因为攻击者可以以各种方式对该组件进行测试攻击.但是开发者不一定所有的安全问题都能 ...

  7. Salesforce 生命周期管理(一)应用生命周期浅谈

    本篇参考: https://trailhead.salesforce.com/en/content/learn/trails/determine-which-application-lifecycle ...

  8. 示例浅谈PHP与手机APP开发,即API接口开发

    示例浅谈PHP与手机APP开发,即API接口开发 API(Application Programming Interface,应用程序接口)架构,已经成为目前互联网产品开发中常见的软件架构模式,并且诞 ...

  9. 浅谈 PHP 与手机 APP 开发(API 接口开发) -- 转载

    转载自:http://www.thinkphp.cn/topic/5023.html 这个帖子写给不太了解PHP与API开发的人 一.先简单回答两个问题: 1.PHP 可以开发客户端? 答:不可以,因 ...

随机推荐

  1. Linux下截取指定时间段日志并输出到指定文件

    sed -n '/2019-04-22 16:10:/,/2019-04-22 16:20:/p' log.log > bbb.txt

  2. Linux-文件管理

    文件管理 创建.复制.删除.移动.查看.编辑.压缩.查找 Linux目录结构 Windows: 以多根的方式组织文件 C:\ D:\ E:\Linux: 以单根的方式组织文件 / /目录结构: FSH ...

  3. yii2 gridview默认排序

    Yii2 GridView 使用起来很方便,但是默认排序很是个问题,数据默认按 主键 正序排列 但是在使用过程中,大多数数据默认是 倒序才符合正常思维的. 第一次 的解决方法是在 直接为 Model添 ...

  4. Mysql 查询当月时间数据

    SELECTDATE_FORMAT(CURDATE(), '%Y%m'), DATE_FORMAT(t.transactiontime, '%Y%m'),t.*FROM ttransactions t ...

  5. vue中的computed(计算属性)和watch(监听属性)的特点,以及深度监听

    //计算属性是根据data中已有的属性,计算得到一个新的属性, <div>全名:{{fullName}}</div> 创建计算属性通过computed关键字,它是一个对象 计算 ...

  6. 2017-12-20python全栈9期第五天第二节之可变 数据类型和不可变数据类型

  7. 编写高质量的Python代码系列(六)之内置模块

    Python预装了许多写程序时会用到的重要模块.这些标准软件包与通常意义上的Python语言联系得非常精密,我们可以将其当成语言规范的一部分.本节将会讲解基本的内置模块. 第四十二条:用functoo ...

  8. JDK环境部署

    JDK环境部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 说起JDK想必大家并不陌生,作为运维的小伙伴,谁不层接触过Java程序员呢?而且在搭建服务上时也必须得接触他,比如to ...

  9. Ansible-基础

    Ansible架构 Inventory   主机清单,可以对主机分组 ansible-hoc   ansible的命令,适用临时场景 ansible-playbook   ansible是一个场景的集 ...

  10. I/O模型系列之五:IO多路复用 select、poll、epoll

    IO多路复用之select.poll.epoll IO多路复用:通过一种机制,一个进程可以监视多个描述符,一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作. 应用:适用于针 ...