功能设计
数据库设计
书单功能实现
/**
* 转到书单详情页
* @param id
* @param model
* @return
*/
@RequestMapping("/booklist/{id}/detail")
public String detail(@PathVariable Long id,Model model){
System.out.println("跳转到详情页面");
model.addAttribute("booklist",booklistService.show(id));
model.addAttribute("listbook",booklistService.listBook(id));;
System.out.println("跳转到详情页面");
return "user/booklist/booklist_detail";
}
/**
* 删除书单
* @param id
* @param attributes
* @return
*/
@RequestMapping("/booklist/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes,Model model){
List<Book> book = booklistService.listBook(id);
for(Book s : book) {
booklistService.deletebookById(id,s.getId());
}
booklistService.delete(id);
attributes.addFlashAttribute("message","删除成功");
// BookList booklist = booklistService.show(id);
Long userid = Long.valueOf("1");
model.addAttribute("user", userService.getuserById(userid));
return "/user/booklist/my_booklist";
}
/**
* 新增图书的前置操作
* @param model
* @return
*/
@GetMapping("/booklist/input/{userid}")
public String addBook(Model model,@PathVariable Long userid) {
// 传递一个空的BookList对象 传进去所有的标签 以及图书
BookList bookList = new BookList();
model.addAttribute("booklist", bookList);
model.addAttribute("tag",tagService.listTag());
model.addAttribute("books", bookService.getBookList());
model.addAttribute("id",Long.valueOf("999"));
//传递当前用户id
model.addAttribute("user.id", userid);
System.out.println("进入新增/修改书单界面");
return "/user/booklist/add_booklist";
}
/**
* 新增书单
* @param booklist
* @param attributes
* @return
*/
@PostMapping("/booklist/add")
public String addBook(BookList booklist, RedirectAttributes attributes, Model model){
System.out.println(booklist.getUser().getId());
System.out.println(booklist.getBookIds());
System.out.println(booklist.getTag().getId());
// 得到User对象 将User对象存储到BookList对象中 原因是前台传的User对象 只有id值
booklist.setUser(userService.getuserById(booklist.getUser().getId()));
booklist.setBooks(bookService.listBooks(booklist.getBookIds()));
booklist.setTag(tagService.getTagById(booklist.getTag().getId()));
BookList booklist2 = booklistService.save(booklist);
model.addAttribute("user", userService.getuserById(booklist.getUser().getId()));
System.out.println("回到我的书单界面");
if(booklist2!=null){
attributes.addFlashAttribute("message", "添加成功");
}else{
attributes.addFlashAttribute("message", "添加失败");
}
return "/user/booklist/my_booklist";
}
/**
* 编辑书单的前置操作
* @param id
* @param model
* @return
*/
@GetMapping("/booklist/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
//找到需要修改的书单
System.out.println(Long.toString(id));
BookList booklist = booklistService.getBookListById(id);
System.out.println("编辑书单");
//传递当前用户id
System.out.println(booklist.getUser().getId());
model.addAttribute("user.id", booklist.getUser().getId());
booklist.init(); //books的list集合 转换成了 对应的字符串 bookIds
model.addAttribute("booklist", booklist);
model.addAttribute("tag",tagService.listTag());
model.addAttribute("books", bookService.getBookList());
model.addAttribute("id",id);
return "/user/booklist/add_booklist";
}
/**
* 编辑书单
* @param id
* @param booklist
* @param attributes
* @return
*/
@PostMapping("/booklist/{id}/update")
public String update(@RequestParam Long id, BookList booklist, RedirectAttributes attributes,Model model){
System.out.println(booklist.getUser().getId());
System.out.println(booklist.getBookIds());
System.out.println(booklist.getTag().getId());
// 得到User对象 将User对象存储到BookList对象中 原因是前台传的User对象 只有id值
booklist.setUser(userService.getuserById(booklist.getUser().getId()));
booklist.setBooks(bookService.listBooks(booklist.getBookIds()));
booklist.setTag(tagService.getTagById(booklist.getTag().getId()));
BookList booklist2 = booklistService.save(booklist);
model.addAttribute("user", userService.getuserById(booklist.getUser().getId()));
System.out.println("编辑书单完成");
BookList booklist1 = booklistService.updateBooklist(id, booklist);
if (booklist1 == null) {
attributes.addFlashAttribute("message", "更新失败");
} else {
attributes.addFlashAttribute("message", "更新成功");
}
return "/user/booklist/my_booklist";
}
项目总结
上课主要的流程就是老师讲知识点并且拿一个实习项目让我们跟着做,我通常习惯老师讲完后下午一边看录屏一遍敲代码,不过老师说还是要尽量在上午就跟上节奏,到后来才慢慢跟上老师讲解。
项目搭建实施。我们组是按前后端的接口来分配任务,第一阶段是前端界面,我们大致分工之后就各自按照自己的功能去找前端模板并修改了,这个还不是很难,主要是做出我们想要的界面排版和功能,在这个过程中我学习了bootstrap框架,对我熟悉前端代码很有帮助。后端的实现代码部分,数据交互只需要会简单的JSP语法就行,之后就要写一些实现局部刷新页面和一些异步数据处理,这就涉及到AJAX的事件函数,所以简单地学习了AJAX,个人认为这个东西是比较难以理解的,可能是我学习比较粗浅的原因。在这个过程中我遇到了很多困难和bug,特别是如何删除书单内书籍的问题,需要删除多对多关系。