超详细超实用!!!零基础java开发之云风笔记笔记列表接口开发(七)

server/2024/9/23 11:59:44/

云风网
云风笔记
云风知识库

云风笔记的登录注册接口开发完成之后,我们接下来可以开始进行笔记的添加接口以及笔记列表展示接口的开发

在这里插入图片描述

在这里插入图片描述

一、新建包note、实体类NoteManage

添加笔记所需要的字段属性主要有:

  1. 笔记名称:name
  2. 笔记分类:type
  3. 笔记内容:content
  4. 笔记备注:desc
1、 新建note包、包下新建实体类NoteManage,初始化笔记属性信息
javascript">package com.example.study.note;//创建笔记
public class NoteManage {private static String name;//笔记名称private static String type;//笔记类型private static String content;//笔记内容private static String desc;//笔记备注public NoteManage(String name, String type, String content, String desc) {NoteManage.name = name;NoteManage.type = type;NoteManage.content = content;NoteManage.desc = desc;}public static void setName(String name){NoteManage.name = name;}public String getName(){return name;}public static void setType(String type){NoteManage.type = type;}public String getType(){return type;}public static void setContent(String content){NoteManage.content = content;}public String getContent(){return content;}public static void setDesc(String desc){NoteManage.desc = desc;}public String getDesc(){return desc;}
}
2、新建note/Response类,用来处理返回信息

其中result是为了后面笔记列表接口数据做准备

javascript">package com.example.study.note;public class Response {private static String msg;private static int code;private static Boolean success;private static Object result;public Response(Boolean success,String msg, int code) {Response.msg = msg;Response.code = code;Response.success = success;}public void setResponse(Boolean success,String msg, int code,Object result) {Response.msg = msg;Response.code = code;Response.success = success;Response.result = result;}public static void setMsg(String msg) {Response.msg = msg;}public static void setCode(int code) {Response.code = code;}public static void setSuccess(Boolean success) {Response.success = success;}public static void setResult(Object result) {Response.result = result;}public int getCode() {return code;}public String getMsg() {return msg;}public Boolean getSuccess() {return success;}public Object getResult() {return result;}
}

二、服务包service下新建NoteApi定义接口

javascript">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类来实现接口。

javascript">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 {@AutowiredNoteMapper noteMapper;public int addNote(NoteManage noteManage){return noteMapper.addNote(noteManage);}public List<NoteManage> getNoteList(){return noteMapper.getNoteList();}
}

四、软件包mapper下创建NoteMapper类

javascript">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 文件

javascript"><?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即为添加成功,这里根据实际情况进行更改

javascript">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 {@Autowiredpublic 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(false,"",400);List<NoteManage> noteList = service.getNoteList();response.setResponse(true,"查询成功",200,noteList);return response;}
}

八、验证接口是否正常请求

1、添加笔记addNote

在这里插入图片描述

1、获取笔记列表数据getNoteList

在这里插入图片描述


http://www.ppmy.cn/server/116254.html

相关文章

Centos使用阿里云镜像安装docker及docker hub下载失败解决方案

一 配置阿里云的Yum镜像源 配置阿里云的Yum镜像源可以提高下载速度&#xff0c;尤其是在国内网络环境下。以下是配置阿里云Yum镜像源的步骤&#xff1a; 1. 备份原有的Yum源配置文件 首先&#xff0c;备份系统现有的Yum源配置文件&#xff0c;以防出现问题时可以还原&#x…

鸿蒙轻内核M核源码分析系列十二 事件Event

往期知识点记录&#xff1a; 鸿蒙&#xff08;HarmonyOS&#xff09;应用层开发&#xff08;北向&#xff09;知识点汇总 轻内核M核源码分析系列一 数据结构-双向循环链表 轻内核M核源码分析系列二 数据结构-任务就绪队列 鸿蒙轻内核M核源码分析系列三 数据结构-任务排序链表 轻…

《中外食品工业》是什么级别的期刊?是正规期刊吗?能评职称吗?

​问题解答 问&#xff1a;《中外食品工业》是不是核心期刊&#xff1f; 答&#xff1a;不是&#xff0c;是知网收录的正规学术期刊。 问&#xff1a;《中外食品工业》级别&#xff1f; 答&#xff1a;国家级。主管单位&#xff1a; 中国轻工业联合会 …

手机玩机常识-------诺基亚系列机型3/5/6/7/8详细的刷机教程步骤 手机参考救砖刷机教程

诺基亚手机 诺基亚&#xff08;Nokia Corporation&#xff09;&#xff0c;成立于1865年&#xff0c;是一家主要从事移动通信设备生产和相关服务的手机公司 &#xff0c;总部位于芬兰埃斯波 。从1996年开始&#xff0c;诺基亚手机连续15年占据手机市场份额第一位置&…

LeetCode之二叉搜索树

530. 二叉搜索树的最小绝对差 /*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val val; }* TreeNode(int val, TreeNode left, TreeNode…

Linux TCP服务器和客户端学习

socket 这里实现的是单连接的情况&#xff0c;即一个服务器只能连接一个客户端。实现的功能是 服务器端&#xff1a;等待客户端连接&#xff0c;连接后显示客户端发送的数据&#xff0c;并将数据原样发送给客户端。 客户端&#xff1a;连接服务器&#xff0c;然后向服务器发送…

ubuntu24.04 为什么扬声器没有声音,但是戴上耳机有声音

扬声器在 Ubuntu 24.04 下没有声音&#xff0c;但耳机有声音&#xff0c;可能是由于以下几个原因造成的&#xff1a; 1. 输出设备设置问题 系统可能将默认输出设备设置为耳机&#xff0c;而非扬声器。你可以检查或更改音频输出设备&#xff1a; 打开“设置” -> “声音”…

ubuntu 22.04 编译安装新内核

1、普通用户登录系统 查看当前内核版本 $ uname -r 5.15.0-118-generic 2、下载内核源码 www.kernel.org 用户home目录新建子目录linux&#xff0c;下载并解压 linux-5.15.165.tar.xz 3、创建起始的配置文件.config Configuration targets &#xff08;见linux kernel i…