云笔记——笔记本添加
增加笔记
- 数据库笔记本表设计
- 笔记本bean
private String notebookId;
private String noteTypeId;
private String userId;
private String notebookName;
get/set
- bean的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.lee.com.lee.dao.NotebookDao">
<sql id="tem">
u.t_notebook_id As notebookId,
u.t_notebook_type_id As noteTypeId,
u.t_user_id As userId,
u.t_notebook_name As notebookName
</sql>
<insert id="addNotebook" parameterType="com.lee.com.lee.pojo.Notebook">
insert into t_notebook(t_notebook_id,t_notebook_type_id,t_user_id,t_notebook_name) values(#{notebookId},#{noteTypeId},#{userId},#{notebookName})
</insert>
<select id="findOne" parameterType="com.lee.com.lee.pojo.Notebook" resultType="com.lee.com.lee.pojo.Notebook">
select <include refid="tem"/> from t_notebook u where t_user_id=#{userId} and t_notebook_name =#{notebookName}
</select>
<update id="updateNotebook" parameterType="com.lee.com.lee.pojo.NotebookType">
update t_notebook set t_notebook_name = #{notebookName} where t_notebook_id =#{notebookId}
</update>
<delete id="deleteByNotebookId" parameterType="string">
delete from t_notebook where t_notebook_id = #{notebookId}
</delete>
<select id="findByUserId" parameterType="string" resultType="com.lee.com.lee.pojo.Notebook">
select <include refid="tem"/> from t_notebook u where t_user_id =#{userId}
</select>
</mapper>
-
数据库笔记本类型表的设计
笔记本类型默认值
-
笔记本类型bean
private String notebookTypeId;
private String notebookTypeCode;
private String notebookTypeName;
get/set
- 笔记本类型的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.lee.com.lee.dao.NotebookTypeDao">
<sql id="tem">
u.t_notebook_type_id As notebookTypeId,
u.t_notebook_type_code AS notebookTypeCode,
u.t_notebook_type_name As notebookTypeName
</sql>
<select id="getSpecialType" resultType="com.lee.com.lee.pojo.NotebookType">
select <include refid="tem"/> from t_notebook_type u where t_notebook_type_code !='normal'
</select>
<select id="getNormalType" resultType="com.lee.com.lee.pojo.NotebookType">
select <include refid="tem"/> from t_notebook_type u where t_notebook_type_code = 'normal'
</select>
</mapper>
- 笔记本类型的dao
public List<NotebookType> getSpecialType();
// 获去普通笔记本类型
public NotebookType getNormalType();
- 笔记本的dao
// 添加笔记本
public void addNotebook(Notebook notebook);
// 通过笔记本id删除笔记本
public void deleteByNotebookId(String notebookId);
// 更新笔记本
public void updateNotebook(Notebook notebook);
// 通过用户id查找该用户的笔记本列表
public List<Notebook> findByUserId(String userId);
// 查找笔记本
public Notebook findOne(Notebook notebook);
- 前端代码
<!-- 全部笔记本 -->
<div class="row" style='padding:0;' id='center'>
<!-- alert_background-->
<div class="opacity_bg" style='display:none'></div>
<!-- alert_notebook -->
<div id="can"></div>
<div class="col-xs-2" style='padding:0;' id='pc_part_1'>
<!-- side-right -->
<div class="pc_top_first">
<h3>全部笔记本</h3>
<button type="button" class="btn btn-default btn-xs btn_plus" id='add_notebook'><i class="fa fa-plus"></i></button>
</div>
<aside class="side-right" id='first_side_right'>
<div class="module" data-toggle="niceScroll">
<div class="chat-contact">
<div class="contact-body">
<ul class="contacts-list">
<li class="online">
<a class='checked'>
<i class="fa fa-book" title="online" rel="tooltip-bottom"></i> 默认笔记本
</a>
</li>
<!--<li class="online">
<a class='unchecked'>
<i class="fa fa-book" title="笔记本" rel="tooltip-bottom"></i> Spring
<button type="button" class="btn btn-default btn-xs btn_position btn_delete"><i class="fa fa-times"></i></button>
</a>
</li>-->
</ul>
</div>
</div>
</div>
</aside>
<div class="row clear_margin">
<div class="col-xs-4 click" id='rollback_button' title='回收站'><i class='fa fa-trash-o' style='font-size:20px;line-height:31px;'></i></div>
<div class="col-xs-4 click" id='like_button' title='收藏笔记本'><i class='fa fa-star' style='font-size:20px;line-height:31px;'></i></div>
<div class="col-xs-4 click" id='action_button' title='参加活动笔记'><i class='fa fa-users' style='font-size:20px;line-height:30px;'></i></div>
</div>
</div>
- 添加笔记本事件
点击添加笔记本按钮触发Ajax发送请求
//----创建笔记本
$(document).on("click", "#modalBasic .btn.btn-primary.sure", function() {
addNoteBook();
});
// 调用方法发送ajax请求
function addNoteBook(){
var oInput_notebook = $("#input_notebook").val();
oInput_notebook.trim();
$("#error_info").text(oInput_notebook);
if(oInput_notebook == null || oInput_notebook==""){
alert("笔记本名称不能为空");
}else{
$.ajax({
url:"/notebook/addNotebook",
type:"post",
data:{"notebookName":oInput_notebook,"id":getCookie("id")},
success:function (result) {
if(result.success){
var notebook = result.data;
$("#first_side_right ul li:first").after('<li class="online">' +
'<a class="unchecked">' +
'<i class="fa fa-book" title="笔记本" rel="tooltip-bottom"></i> '+notebook.notebookName+
'<button type="button" class="btn btn-default btn-xs btn_position btn_delete"><i class="fa fa-times"></i></button>' +
'</a></li>');
// 将数据绑定在li中方便笔记模块使用
$("#first_side_right ul li:first").next().data("notebook",notebook);
// 去掉li中的选中状态
$("#first_side_right ul li").children('a').removeClass('checked');
// 给刚刚添加的li赋予选中状态
$("#first_side_right ul li:first").next().children('a').addClass('checked');
// $("#modalBasic").modal('hide');
// 模拟点击模态框关闭
$('.close,.cancle').trigger('click');
// 获取当前的笔记本的笔记列表
//tips: 此项功能现在还没有实现
// getNoteList();
}else{
alert(result.msg);
}
}
})
}
}
- 访问控制层(controller)
通过登录模块的登录成功后将用户存到session中,现在从session中获取用户的信息,进行添加时需要用户的信息,否则数据库将此笔记本不知道是哪个用户的笔记本,还需要验证该数据库中是否已经存在该用户的该笔记本,虽然前端已经进行了用户名不能为空的验证,但是后端也需要进行验证,双重保险更稳妥
@RequestMapping("/addNotebook")
public Result addNotebook(String notebookName, HttpSession session) {
notebookName.trim();
Result result = new Result();
if (notebookName == null || notebookName.length() == 0) {
result.setMsg("笔记本名称不能为空");
result.setSuccess(false);
} else {
User loginUser = (User) session.getAttribute("loginUser");
Notebook notebook = new Notebook();
notebook.setNotebookName(notebookName);
notebook.setUserId(loginUser.getId());
Notebook notebook1 = notebookService.addNotebook(notebook);
if (notebook1 == null) {
result.setSuccess(false);
result.setMsg("笔记本名称已经存在");
} else {
result.setMsg("创建成功");
result.setData(notebook1);
result.setSuccess(true);
}
}
return result;
}
- 调用service层的接口
@Autowired
private NotebookDao notebookDao;
@Autowired
private NotebookTypeDao notebookTypeDao;
@Autowired
private NoteDao noteDao;
@Override
public Notebook addNotebook(Notebook notebook) {
//在添加之前先去数据库查询一遍,看数据库中是否存在
Notebook no = notebookDao.findOne(notebook);
if(no!=null){
return null;
}else{
// 生成一个随机的id
notebook.setNotebookId(UUID.randomUUID().toString());
/*
此处需要查询数据库笔记本为普通的类型,
*/
NotebookType normalType = notebookTypeDao.getNormalType();
String notebookTypeId = normalType.getNotebookTypeId();
notebook.setNoteTypeId(notebookTypeId);
notebookDao.addNotebook(notebook);
}
return notebook;
}
创建笔记本结束