笔记管理
一、当根据笔记本创建了笔记,用户可以通过选择笔记本来加载笔记本下的所有笔记,并可以选中笔记进行修改保存笔记内容。加载笔记列表流程:选择笔记本 -> 发送加载笔记列表 -> 过滤拦截请求 -> 分配控制器 -> 执行controller.execute()方法 -> 返回数据 -> 动态生成笔记列表保存笔记流程:选择笔记 -> 编辑笔记内容 点击保存发送请求-> 过滤拦截请求 -> 分配控制器 -> 执行 controller.execute()方法 -> 返回数据。
package com.noteserves.controller;
import java.util.List;
import java.util.UUID;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.noteserves.entity.Note;
import com.noteserves.param.NoteParam;
import com.noteserves.result.LayPage;
import com.noteserves.result.NoteResult;
import com.noteserves.service.NoteService;
import com.noteserves.util.JsonResult;
@Controller//将该类扫描到Spring容器中
@RequestMapping("/note")//UserController类的映射路径
public class NoteController {
@Autowired
NoteService noteService;
/**
* 获取笔记信息
* @return
*/
@RequestMapping("/getnote")
@ResponseBody
public JsonResult getNote(NoteParam param) {
param.setPage((param.getPage()-1)*param.getLimit());
//System.out.println("page:"+page+" limit:"+limit);
List<NoteResult> list=noteService.getNote(param);
int count = noteService.getCount(param);
//构建一个对象
LayPage lp = new LayPage();
lp.setCount(count);
lp.setList(list);
return new JsonResult(JsonResult.STATE_SUCCESS,"",lp);
}
/**
* 添加笔记
* @return
*/
@RequestMapping("/addnote")
@ResponseBody
public JsonResult addNote(Note note) {
note.setId(UUID.randomUUID().toString());
note.setAddtime(System.currentTimeMillis());
note.setIsdelete(0);
//note.setUserid();
int result=noteService.addNote(note);
if(result==1) {
return new JsonResult(JsonResult.STATE_SUCCESS,"添加成功",null);
}else {
return new JsonResult(JsonResult.STATE_ERROR,"添加失败",null);
}
}
/**
* 获取单个笔记信息
* @return
*/
@RequestMapping("/getdetail")
@ResponseBody
public JsonResult getNoteDetail(Note note) {
List<Note> list=noteService.getNoteDetail(note);
if(list!=null && !list.isEmpty()) {
return new JsonResult(JsonResult.STATE_SUCCESS,"",list.get(0));
}else {
return new JsonResult(JsonResult.STATE_ERROR,"未查询到笔记",null);
}
}
/**
* 修改笔记
* @return
*/
@RequestMapping("/updatenote")
@ResponseBody
public JsonResult updateNote(Note note) {
int result=noteService.updateNote(note);
if(result==1) {
return new JsonResult(JsonResult.STATE_SUCCESS,"修改成功",null);
}else {
return new JsonResult(JsonResult.STATE_ERROR,"修改失败",null);
}
}
/**
* 删除笔记
* @return
*/
@RequestMapping("/deletenote")
@ResponseBody
public JsonResult deleteNote(Note note) {
int result=noteService.deleteNote(note);
if(result==1) {
return new JsonResult(JsonResult.STATE_SUCCESS,"删除成功",null);
}else {
return new JsonResult(JsonResult.STATE_ERROR,"删除失败",null);
}
}
}
package com.noteserves.service;
import java.util.List;
import com.noteserves.entity.Note;
import com.noteserves.param.NoteParam;
import com.noteserves.result.NoteResult;
public interface NoteService {
public int getCount(NoteParam param);
/**
* 获取笔记信息
* @return
*/
public List<NoteResult> getNote(NoteParam param);
/**
* 添加笔记信息
* @param Note 要添加的笔记本
* @return
*/
public int addNote(Note note);
/**
* 查询单个笔记信息
* @param Note 根据Note的id
* @return
*/
public List<Note> getNoteDetail(Note note);
/**
* 更新笔记信息
* @param Note 要更新的笔记本
* @return
*/
public int updateNote(Note note);
/**
* 删除笔记
* @param Note 要删除的笔记本
* @return
*/
public int deleteNote(Note note);
}
package com.noteserves.serviceimpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.noteserves.dao.NoteDao;
import com.noteserves.entity.Note;
import com.noteserves.param.NoteParam;
import com.noteserves.result.NoteResult;
import com.noteserves.service.NoteService;
@Service
public class NoteImpl implements NoteService {
@Autowired
NoteDao noteDao;
public int getCount(NoteParam param) {
return noteDao.getCount(param);
}
/**
* 获取笔记信息
* @return
*/
public List<NoteResult> getNote(NoteParam param) {
return noteDao.getNote(param);
}
/**
* 添加笔记信息
* @param note 要添加的笔记
* @return
*/
public int addNote(Note note) {
return noteDao.addNote(note);
}
/**
* 查询单个笔记信息
* @param note 根据note的id
* @return
*/
public List<Note> getNoteDetail(Note note) {
return noteDao.getNoteDetail(note);
}
/**
* 更新笔记信息
* @param note 要更新的笔记
* @return
*/
public int updateNote(Note note) {
return noteDao.updateNote(note);
}
/**
* 删除笔记
* @param note 要删除的笔记
* @return
*/
public int deleteNote(Note note) {
return noteDao.deleteNote(note);
}
}
package com.noteserves.entity;
import java.io.Serializable;
public class Note implements Serializable{
/**
*
*/
private static final long serialVersionUID = -4890986115166876236L;
public static int DELETE_YES=1;
public static int DELETE_NO=0;
private String id; //id
private String name;//笔记本名称
private String content;//笔记内容
private long addtime;//添加时间
private int isdelete;//是否删除标志,0-未删除,1-删除
private String notebookid;//所属笔记本id
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getAddtime() {
return addtime;
}
public void setAddtime(long addtime) {
this.addtime = addtime;
}
public int getIsdelete() {
return isdelete;
}
public void setIsdelete(int isdelete) {
this.isdelete = isdelete;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getNotebookid() {
return notebookid;
}
public void setNotebookid(String notebookid) {
this.notebookid = notebookid;
}
@Override
public String toString() {
return "NoteBook [id=" + id + ", name=" + name + ", addtime=" + addtime + ", isdelete=" + isdelete + ", userid="
+ content + "]";
}
}
package com.noteserves.dao;
import java.util.List;
import com.noteserves.entity.Note;
import com.noteserves.param.NoteParam;
import com.noteserves.result.NoteResult;
public interface NoteDao {
/**
* 获取记录数
* @return
*/
public int getCount(NoteParam param);
/**
* 获取笔记本信息
* @return
*/
public List<NoteResult> getNote(NoteParam param);
/**
* 添加笔记本信息
* @param Note 要添加的笔记本
* @return
*/
public int addNote(Note note);
/**
* 查询单个笔记本信息
* @param Note 根据Note的id
* @return
*/
public List<Note> getNoteDetail(Note Note);
/**
* 更新笔记本信息
* @param Note 要更新的笔记本
* @return
*/
public int updateNote(Note Note);
/**
* 删除笔记本
* @param Note 要删除的笔记本
* @return
*/
public int deleteNote(Note Note);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.noteserves.dao.NoteDao">
<select id="getCount" resultType="Integer" parameterType="com.noteserves.param.NoteParam">
select count(*)
from note_t_notes notes,note_t_notebooks notebooks
where notebooks.id=notes.notebookid and notebooks.isdelete=0 and notes.isdelete=0 and notebooks.userid=#{userid}
<if test="content!=null and content !=''">
<bind name="bindcontent" value="'%'+content+'%'" />
and notes.content like #{bindcontent}
</if>
</select>
<select id="getNote" resultType="com.noteserves.result.NoteResult">
select notes.id,notes.content,notes.name,notes.addtime,notes.isdelete,notebooks.name as "notebooksname"
from note_t_notes notes,note_t_notebooks notebooks
where notebooks.id=notes.notebookid and notebooks.isdelete=0 and notes.isdelete=0 and notebooks.userid=#{userid}
<if test="content!=null and content !=''">
<bind name="bindcontent" value="'%'+content+'%'"></bind>
and notes.content like #{bindcontent}
</if>
order by addtime desc limit #{page},#{limit}
</select>
<insert id="addNote" parameterType="com.noteserves.entity.Note">
insert into note_t_notes (id,name,addtime,isdelete,content,notebookid) values(#{id},#{name},#{addtime},#{isdelete},#{content},#{notebookid})
</insert>
<select id="getNoteDetail" parameterType="com.noteserves.entity.Note" resultType="com.noteserves.entity.Note">
select id,name,addtime,isdelete,content,notebookid from note_t_notes where id=#{id}
</select>
<!-- <update id="updateNote" parameterType="com.noteserves.entity.Note">
update note_t_notes set name=#{name},content=#{content},notebookid=#{notebookid} where id=#{id}
</update> -->
<update id="updateNote" parameterType="com.noteserves.entity.Note">
update note_t_notes
<set>
<if test="name!=null and name!=''">
name=#{name},
</if>
<if test="content!=null and content!=''">
content=#{content},
</if>
<if test="notebookid!=null and notebookid!=''">
notebookid=#{notebookid}
</if>
</set>
where id=#{id}
</update>
<update id="deleteNote" parameterType="com.noteserves.entity.Note">
update note_t_notes set isdelete=1 where id=#{id}
</update>
</mapper>