Spring + Spring MVC + Mybatis 云笔记项目(六)添加笔记

开发工具:eclipse + navicat 

项目源码:CSDN下载地址

Github:Github源码地址

一、当创建完毕笔记本之后,可以在笔记本下面创建不同的笔记。

流程:选择笔记本 -> 添加笔记本 -> 提交数据 -> DispatcherServlet 过滤拦截请求 -> 分配控制器 -> 执行controller.execute() 方法 ->返回操作结果 -> ajax接受返回结果 status = 0 添加笔记成功 status = 1 添加笔记失败 -> 刷新界面 动态生成笔记列表。

二、选择要在哪个笔记本下面添加笔记,再添加笔记之前要先选择笔记本,否则会报错提示先选择笔记本

选择好笔记本之后,点击添加笔记 + 号,填写笔记名称 、提交数据,

返回操作结果

 

三、添加笔记 js note.js

//创建笔记按钮的点击事件
function addNote(){
	//获取请求参数
	//获取笔记标题
	var title=$("#input_note").val().trim();
	//获取用户ID
	var userId=getCookie("userId");
	//获取笔记本ID
	var $li=$("#book_ul a.checked").parent();
	var bookId=$li.data("bookId");
	//数据格式检查
	var ok=true;
	if(title==""){//判断是否为空
		ok=false;
		$("#title_span").html("标题不能为空");
	}
	if(userId==null){//检查是否生效
		ok=false;
		window.location.href="log_in.html";
	}
	if(ok){
		//发送ajax请求
		$.ajax({
			url:base_path+"/note/add.do",
			type:"post",
			data:{"userId":userId,"bookId":bookId,"title":title},
			dataType:"json",
			success:function(result){
				var note=result.data;
				if(result.status==0){
					var id=note.cn_note_id;
					var title=note.cn_note_title;
					createNoteLi(id,title);
					alert(result.msg);
				}
			},
			error:function(){
				alert("创建笔记失败");
			}
		});
	}
};

 添加笔记Controller AddNoteController.java,调用 noteService 接口,执行AddNote()方法,参数,用户编号,笔记本编号,笔记本名称。

package com.sjh.cloud_note.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sjh.cloud_note.entity.Note;
import com.sjh.cloud_note.service.NoteService;
import com.sjh.cloud_note.util.NoteResult;

@Controller
@RequestMapping("/note")
public class AddNoteController {

	@Resource
	private NoteService noteService;
	
	@RequestMapping("/add.do")
	@ResponseBody
	public NoteResult<Note> execute (String userId,String bookId,String title){
		NoteResult<Note> result = noteService.AddNote(userId, bookId, title);
		return result;
	}
}

接口 NoteService.java,总包含 根据笔记本编号加载笔记、根据笔记编号加载笔记、添加笔记、删除笔记、更新笔记、移动笔记

package com.sjh.cloud_note.service;

import java.util.List;
import java.util.Map;

import com.sjh.cloud_note.entity.Note;
import com.sjh.cloud_note.util.NoteResult;

public interface NoteService {

	public NoteResult<List<Map>> loadNotesByBookId(String bookId);
	
	public NoteResult<Note> loadNoteByNoteId(String noteId);
	
	public NoteResult<Note> AddNote(String userId,String bookId,String title);
	
	public NoteResult<Object> DeleteNote(String noteId);
	
	public NoteResult<Object> UpdateNote(String noteId,String title,String body);
	
	public NoteResult<Object> MoveNote(String noteId,String bookId);
	
	
}

实现接口

package com.sjh.cloud_note.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sjh.cloud_note.dao.NoteDao;
import com.sjh.cloud_note.entity.Note;
import com.sjh.cloud_note.util.NoteResult;
import com.sjh.cloud_note.util.NoteUtil;

@Service("noteService")
@Transactional
public class NoteServiceImpl implements NoteService {
	
	@Resource
	private NoteDao noteDao;

	//根据笔记本加载笔记
	public NoteResult<List<Map>> loadNotesByBookId(String bookId) {
		NoteResult<List<Map>> result = new NoteResult<List<Map>>();
		List<Map> list = noteDao.findByBookId(bookId);
		result.setStatus(0);
		result.setMsg("笔记加载成功");
		result.setData(list);
		return result;
	}

	//根据笔记id加载笔记
	public NoteResult<Note> loadNoteByNoteId(String noteId) {
		NoteResult<Note> result = new NoteResult<Note>();
		
		Note note = noteDao.findByNoteId(noteId);
		result.setStatus(0);
		result.setMsg("笔记加载成功");
		result.setData(note);
		return result;
	}

	//添加笔记
	public NoteResult<Note> AddNote(String userId, String bookId, String title) {
		NoteResult<Note> result = new NoteResult<Note>();
		Note note = new Note();
		String noteId = NoteUtil.createId();
		
		long time = System.currentTimeMillis();
		note.setCn_note_id(noteId);
		note.setCn_user_id(userId);
		note.setCn_notebook_id(bookId);
		note.setCn_note_title(title);
		note.setCn_note_body("");
		note.setCn_note_create_time(time);
		note.setCn_note_last_modify_time(time);
		note.setCn_note_type_id("1");
		note.setCn_note_status_id("1");
		noteDao.save(note);
		
		result.setStatus(0);
		result.setMsg("添加笔记成功");
		result.setData(note);		
		return result;
	}
 
	//删除笔记
	public NoteResult<Object> DeleteNote(String noteId){
		NoteResult<Object> result = new NoteResult<Object>();
		
		Note note = noteDao.findByNoteId(noteId);
		
		note.setCn_note_status_id("2");
		int rows =  noteDao.dynamicUpdate(note);
		if(rows >= 1) {
			result.setStatus(0);
			result.setMsg("删除笔记成功");
		}
		else {
			result.setStatus(1);
			result.setMsg("删除笔记失败");
		}		
		return result;
	}

	//更新笔记标题或者笔记内容
	public NoteResult<Object> UpdateNote(String noteId, String title, String body) {
		NoteResult<Object> result = new NoteResult<Object>();
		Note note = noteDao.findByNoteId(noteId);
		
		long time = System.currentTimeMillis();
		note.setCn_note_title(title);
		note.setCn_note_body(body);
		note.setCn_note_last_modify_time(time);
		
		int rows = noteDao.dynamicUpdate(note);
		if(rows >= 1) {
			result.setStatus(0);
			result.setMsg("笔记更新成功");
		}
		else {
			result.setStatus(1);
			result.setMsg("笔记更新失败");
		}
		return result;
	}

	//移动笔记到其他笔记本
	public NoteResult<Object> MoveNote(String noteId, String bookId) {
		
		NoteResult<Object> result = new NoteResult<Object>();
		Note note = noteDao.findByNoteId(noteId);
		
		note.setCn_notebook_id(bookId);
		int rows = noteDao.dynamicUpdate(note);
		
		if(rows >= 1) {
			result.setStatus(0);
			result.setMsg("笔记移动成功");
		}
		else {
			result.setStatus(1);
			result.setMsg("笔记移动失败");
		}
		return result;
	}
}

NoteDao.java

package com.sjh.cloud_note.dao;

import java.util.List;
import java.util.Map;

import com.sjh.cloud_note.entity.Note;

public interface NoteDao {

	public List<Map> findByBookId(String bookId);
	
	public Note findByNoteId(String id);
	
	public int updateNote(Note note);
	
	public void save(Note note);
	
	public int dynamicUpdate(Note note);
}

NoteMapper.xml

<?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.sjh.cloud_note.dao.NoteDao">
	<select id="findByBookId" parameterType="string" resultType="Map">
		select cn_note_id,cn_note_title from cn_note where cn_notebook_id=#{bookId} and cn_note_status_id='1' 
	</select>
	<select id="findByNoteId" parameterType="String" resultType="com.sjh.cloud_note.entity.Note">
		select *from cn_note where cn_note_id=#{noteId}
	</select>
	<update id="updateNote" parameterType="com.sjh.cloud_note.entity.Note">
		update cn_note set cn_note_title=#{cn_note_title},
		cn_note_body=#{cn_note_body},
		cn_note_last_modify_time=#{cn_note_last_modify_time}
		where cn_note_id=#{cn_note_id}
	</update>
	<insert id="save" parameterType="com.sjh.cloud_note.entity.Note">
		insert into cn_note(
		cn_note_id,
		cn_notebook_id,
		cn_user_id,
		cn_note_status_id,
		cn_note_type_id,
		cn_note_title,
		cn_note_body,
		cn_note_create_time,
		cn_note_last_modify_time) 
		values(
		#{cn_note_id},
		#{cn_notebook_id},
		#{cn_user_id},
		#{cn_note_status_id},
		#{cn_note_type_id},
		#{cn_note_title},
		#{cn_note_body},
		#{cn_note_create_time},
		#{cn_note_last_modify_time}
		)
 	</insert>
 	
 	<!-- 动态更新 -->
	<update id="dynamicUpdate" parameterType="com.sjh.cloud_note.entity.Note">
		update cn_note
		<set>
			<if test="cn_note_type_id!=null">
		 		cn_note_type_id=#{cn_note_type_id},
			</if>
			<if test="cn_note_status_id!=null">
				cn_note_status_id=#{cn_note_status_id},
			</if>
			<if test="cn_notebook_id!=null">
				cn_notebook_id=#{cn_notebook_id},
			</if>
			<if test="cn_note_title!=null">
				cn_note_title=#{cn_note_title},
			</if>
			<if test="cn_note_body!=null">
				cn_note_body=#{cn_note_body},
			</if>
			<if test="cn_note_last_modify_time!=null">
				cn_note_last_modify_time=#{cn_note_last_modify_time}
			</if>
		</set>
		where cn_note_id=#{cn_note_id}
	</update>
</mapper>


 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值