云风笔记的登录注册接口开发完成之后,我们接下来可以开始进行笔记的添加接口以及笔记列表展示接口的开发
一、新建包note、实体类NoteManage
添加笔记所需要的字段属性主要有:
- 笔记名称:name
- 笔记分类:type
- 笔记内容:content
- 笔记备注:desc
1、 新建note包、包下新建实体类NoteManage,初始化笔记属性信息
package com.example.study.note;
//创建笔记
public class NoteManage {
private String name;//笔记名称
private String type;//笔记类型
private String content;//笔记内容
private String desc;//笔记备注
public NoteManage(String name, String type, String content, String desc) {
this.name = name;
this.type = type;
this.content = content;
this.desc = desc;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setType(String type){
this.type = type;
}
public String getType(){
return type;
}
public void setContent(String content){
this.content = content;
}
public String getContent(){
return content;
}
public void setDesc(String desc){
this.desc = desc;
}
public String getDesc(){
return desc;
}
}
2、新建note/Response类,用来处理返回信息
其中result是为了后面笔记列表接口数据做准备
package com.example.study.note;
import java.util.List;
public class Response {
private String msg;
private int code;
private Boolean success;
private Object result;
public Response(){}
public Response(Boolean success, String msg, int code) {
this.msg = msg;
this.code = code;
this.success = success;
}
public void setResponse(Boolean success,String msg, int code,Object result) {
this.msg = msg;
this.code = code;
this.success = success;
this.result = result;
}
public void setMsg(String msg) {
this.msg = msg;
}
public void setCode(int code) {
this.code = code;
}
public void setSuccess(Boolean success) {
this.success = success;
}
public void setResult(Object result) {
this.result = result;
}
public int getCode() {return code;}
public String getMsg() {return msg;}
public Boolean getSuccess() {return success;}
public Object getResult() {return result;}
}
二、服务包service下新建NoteApi定义接口
package com.example.study.service;
import com.example.study.note.NoteManage;
import java.util.List;
public interface NoteApi {
int addNote(NoteManage noteManage);
List<NoteManage> getNoteList();
}
三、在 impl 软件包下新建 NoteServiceImpl类来实现接口。
package com.example.study.service.impl;
import com.example.study.note.NoteManage;
import com.example.study.mapper.NoteMapper;
import com.example.study.service.NoteApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class NoteServiceImpl implements NoteApi {
@Autowired
NoteMapper noteMapper;
public int addNote(NoteManage noteManage){
return noteMapper.addNote(noteManage);
}
public List<NoteManage> getNoteList(){
return noteMapper.getNoteList();
}
}
四、软件包mapper下创建NoteMapper类
package com.example.study.mapper;
import com.example.study.note.NoteManage;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface NoteMapper {
int addNote(NoteManage noteManage);
List<NoteManage> getNoteList();
}
五、编写sql逻辑
在 resources/mapper 文件夹下新建相关的 NoteMapper.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.study.mapper.NoteMapper">
<resultMap id="BaseResultMap" type="com.example.study.note.NoteManage">
<result column="id" jdbcType="VARCHAR" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="type" jdbcType="VARCHAR" property="type" />
<result column="content" jdbcType="VARCHAR" property="content" />
<result column="desc" jdbcType="VARCHAR" property="desc" />
</resultMap>
<insert id="addNote" parameterType="com.example.study.note.NoteManage">
INSERT INTO `note` (`name`,`type`,`content`,`desc`) VALUES(#{name},#{type},#{content},#{desc})
</insert>
<select id="getNoteList" resultType="com.example.study.note.NoteManage">
SELECT * FROM `note`
</select >
</mapper>
六、数据库新建表note
id为主键,设置递增
七、controller软件包内新建NoteController控制类
这里新增笔记逻辑判断为名称、类型、内容、备注都不为空,且返回count大于0即为添加成功,这里根据实际情况进行更改
package com.example.study.controller;
import com.example.study.note.NoteManage;
import com.example.study.note.Response;
import com.example.study.service.NoteApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
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;
@RestController
public class NoteController {
@Autowired
public NoteApi service;
@RequestMapping(value = "/addNote",method = RequestMethod.POST)
public Response addNote(@RequestBody NoteManage noteManage){
if(noteManage.getName()!=null && noteManage.getType()!=null && noteManage.getContent()!=null && noteManage.getDesc()!=null ){
int count = service.addNote(noteManage);
if(count > 0){
return new Response(true,"添加成功",200);
}else {
return new Response(false,"添加失败",400);
}
}else {
return new Response(false,"有参数为空",400);
}
}
@RequestMapping(value = "/getNoteList",method = RequestMethod.POST)
public Response getNoteList(@RequestBody NoteManage noteManage){
Response response = new Response();
List<NoteManage> noteList = service.getNoteList();
response.setResponse(true,"查询成功",200,noteList);
return response;
}
}