图书管理系统(考试样品)
一、执行下面的sql
CREATE DATABASE bms CHARACTER SET utf8mb4;USE bms;DROP TABLE IF EXISTS `tb_book`;CREATE TABLE `tb_book` (`id` INT NOT NULL AUTO_INCREMENT,`name` VARCHAR(20) NOT NULL,`desc` TEXT ,`type_id` INT DEFAULT NULL,PRIMARY KEY (`id`)
) ;
INSERT INTO `tb_book` VALUES (7,'gaodengshuxue','haoshu',3),(9,'测试数据','测试描述数据',2),(16,'测试数据increase','测试数据',3),(17,'测试数据increase','测试数据',4),(18,'xiyouji','bad',2),(19,'测试数据increase','测试数据',NULL),(27,'xiyouji2','bad',2),(28,'xiyouji2','bad',2),(29,'xiyouji2','bad',2);DROP TABLE IF EXISTS `tb_type`;CREATE TABLE `tb_type` (`id` INT NOT NULL AUTO_INCREMENT,`type` VARCHAR(30) DEFAULT NULL,PRIMARY KEY (`id`)
);INSERT INTO `tb_type` VALUES (1,'计算机'),(2,'文学'),(3,'数学'),(4,'历史');
二、导入项目的基本结构
三、启动项目
四、浏览器输入http://localhost:8081/books
如果出现如下图片,那么你完成了基本操作。
五、删除某本书
1、删除的链接要怎么写?
<a th:href="@{|/delete/${book.id}|}">删除</a>
上面双竖线的作用:拼接字符串,不用自己手动拼接字符串。
2、处理删除请求的方法要怎么写?
@GetMapping("/delete/{id}")public String deleteById(@PathVariable Integer id){int i=dao.deleteById(id);return "redirect:/books";}
3、做删除操作要怎么写?
@Delete("delete from tb_book where id=#{id}")int deleteById(Integer id);
六、编辑某本书
0、编辑页面要怎么写?在该页面上你要填充某本书的数据。
<form th:action="@{/edit}" method="post"> <input type="hidden" name="id" th:value="${book.id}" /><ul><li>图书名称:<input type="text" name="name" th:value="${book.name}"></li><li>图书描述:<input type="text" name="desc" th:value="${book.desc}"></li><li>图书类型:<ul th:each="type:${types}"><li><input type="radio" name="typeId" th:value="${type.id}"th:text=${type.type}th:checked="${book.typeId==type.id}?'checked'"></li></ul></li><li><input type="submit" value="保存"></li></ul></form>
1、跳到编辑页面的链接要怎么写?
<a th:href="@{|/toEditPage/${book.id}|}">编辑</a>
2、处理跳到编辑页面的请求要怎么写?
@GetMapping("/toEditPage/{id}")public String toEditPage(@PathVariable Integer id,Model model){Book book=dao.selectById(id);model.addAttribute("book", book);List<Type> types = typeDao.selectAll();model.addAttribute("types", types);return "book_edit";}
在BookMapper中增加根据id查询书的方法:
@Select("select * from tb_book where id=#{id}")Book selectById(Integer id);
创建TypeMapper接口,增加查询所有类型的方法:
@Mapper
public interface TypeMapper { @Select("select * from tb_type")List<Type> selectAll();
}
3、在你编辑完成后,点击保存,浏览器发送保存请求,那么服务端如何处理这个请求?
@PostMapping("/edit")public String edit(Book book){int i=dao.update(book); return "redirect:/books";}
在BookMapper中添加更新方法:
@Update("update tb_book set `name`=#{name},`desc`=#{desc},type_id=#{typeId} where id=#{id}")int update(Book book);
七、新增某本书
0、编写新增页面
<form th:action="@{/add}" method="post">图书名称:<input type="text" name="name" ><br/>图书描述:<input type="text" name="desc" ><br/>图书类型:<div th:each="type:${types}"><input type="radio" name="typeId" th:text=${type.type} th:value=${type.id}></div><input type="submit" value="保存">
</form>
1、写出跳到新增页面的链接
<a th:href="@{/toAddPage}">新增</a>
2、处理跳到新增页面的请求
@GetMapping("/toAddPage")public String toAddPage(Model model){List<Type> types = typeDao.selectAll();model.addAttribute("types", types); return "book_add"; }
3、填写书的信息完毕后,点击保存,如何处理添加请求?
@PostMapping("/add")public String add(Book book){ int i=dao.add(book); return "redirect:/books"; }
在BookMapper中添加新增方法:
@Insert("insert into tb_book(name,`desc`,type_id) values(#{name},#{desc},#{typeId})")int add(Book book);