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

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

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

在这里插入图片描述

在这里插入图片描述

一、新建包note、实体类NoteManage

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

  1. 笔记名称:name
  2. 笔记分类:type
  3. 笔记内容:content
  4. 笔记备注: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;
    }
}


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

1、添加笔记addNote

在这里插入图片描述

1、获取笔记列表数据getNoteList

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值