云风网
云风笔记
云风知识库
一、新建包note、实体类NoteManage
添加笔记所需要的字段属性主要有:
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;}
}