Spring Boot图书管理系统项目实战-6.图书管理
导航:
pre: 5.读者管理
next:7.借阅图书
只挑重点的讲,具体的请看项目源码。
1.项目源码
2.页面设计
2.1 book.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>图书管理</title>
<link rel="stylesheet" href="/static/layui/css/layui.css" th:href="@{/static/layui/css/layui.css}">
</head>
<body>
<input type="text" id="ctx" hidden="hidden" th:value="${#request.getContextPath()}">
<!-- 内容主体区域 -->
<div>
<div class="search-div">
<div class="">
<div class="layui-inline">
<label class="layui-form-label">名称:</label>
<div class="layui-input-inline">
<input class="layui-input" id="name" name="name" autocomplete="off">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">ISBN:</label>
<div class="layui-input-inline">
<input class="layui-input" id="isbn" name="isbn" autocomplete="off">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">作者:</label>
<div class="layui-input-inline">
<input class="layui-input" id="author" name="author" autocomplete="off">
</div>
</div>
<button class="layui-btn" data-type="reload" id="search">搜索</button>
</div>
</div>
<table id="tb-book" lay-filter="tb-book"></table>
</div>
<!--编辑表单-->
<script type="text/html" id="book-toolbar">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon"></i>新增</button>
<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="remove"><i class="layui-icon"></i>删除
</button>
<button class="layui-btn layui-btn-sm" lay-event="refresh"><i class="layui-icon"></i>刷新
</button>
</div>
<!--<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
<button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
</div>-->
</script>
<script type="text/html" id="barOps">
<a class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon"></i> </a>
</script>
<script src="/static/js/jquery-1.11.3.min.js" th:src="@{/static/js/jquery-1.11.3.min.js}"></script>
<script src="/static/layui/layui.all.js" th:src="@{static/layui/layui.all.js}"></script>
<script src="/static/js/util.js" th:src="@{/static/js/util.js}"></script>
<!--ctx-->
<script th:replace="~{fragment::ctx}"/>
<script>
var element, layer, laydate, table, form,userTable;
function reload(){
userTable.reload();
}
$(function () {
// 使用模块
layui.use(['element', 'layer', 'laydate', 'table', 'form'], function () {
element = layui.element;
layer = layui.layer;
laydate = layui.laydate;
table = layui.table;
form = layui.form;
});
//第一个实例
userTable = table.render({
elem: '#tb-book'
, height: 515
, url: ctx+'api/book/list' //数据接口
, page: true //开启分页
, toolbar: '#book-toolbar'
,id: 'tb-book'
/*,request: {
pageName: 'pageNo' //页码的参数名称,默认:page
,limitName: 'pageSize' //每页数据量的参数名,默认:limit
}
, response: {
statusName: 'code', //规定返回的状态码字段为code
statusCode: 200 //规定成功的状态码为200,默认为0
}
, parseData: function (res) {
return {
"code": res.code, //解析接口状态
"msg": res.msg, //解析提示文本
"count": res.records, //解析数据长度
"data": res.rows //解析数据列表
}
}*/
, cols: [
[ //表头
{type: 'checkbox', fixed: 'left'}
, {field: 'id', title: 'ID', width: 80, sort: true, fixed: 'left',hide:true}
, {field: 'name', title: '图书名称', width: 180}
, {field: 'isbn', title: 'ISBN码', width: 160}
, {field: 'author', title: '作者', width: 80}
, {field: 'translator', title: '译者', width: 80}
, {field: 'categoryName', title: '图书分类', width: 160}
, {field: 'price', title: '图书单价', width: 120}
, {field: 'totalNumber', title: '藏书总量', width: 120}
, {field: 'leftNumber', title: '馆内剩余', width: 120}
, {field: 'publisherName', title: '出版社', width: 180}
, {field: 'pages', title: '页数', width: 80}
, {field: 'words', title: '字数', width: 80}
, {field: 'locationName', title: '存放位置', width: 120}
, {field: 'edition', title: '版次', width: 80}
, {field: 'registerTime', title: '登记日期', width: 120}
, {field: 'remark', title: '备注', width: 180}
, {fixed: 'right', title: '操作', toolbar: '#barOps', width: 120}
]
]
});
//监听表单提交
// 修改
form.on('submit(saveBook)', function (data) {
layer.alert(JSON.stringify(data.field));
$.ajax({
url: ctx+'api/book/save',
type: 'POST',
contentType: "application/json",
dataType: "json",
data: JSON.stringify(data.field),
success: function (result) {
if (result.code == 200) {
// layer.msg("修改成功!", {icon: 6});
cleanForm("#saveBook");
if(data.field.id.length>0){
layer.closeAll();//关闭所有的弹出层
}
userTable.reload();
}else {
layer.alert(result.message);
}
}
});
return false;
});
//工具栏事件
table.on('toolbar(tb-book)', function (obj) {
var checkStatus = table.checkStatus(obj.config.id);
var checkData = checkStatus.data;
var ids = [];
switch (obj.event) {
// 新增
case 'add':
cleanForm("#saveBook");
top.layer.open({
type: 2,
offset: '10px',
title: "新增图书",
area: ['800px', '660px'],
content: ['bookAdd']
});
break;
// 删除
case 'remove':
if (checkData.length == 0) {
layer.alert('请选择要操作的行');
} else {
layer.confirm('确定要删除吗?', function (index) {
for (var i = 0; i < checkData.length; i++) {
ids.push(checkData[i].id);
}
//layer.alert(JSON.stringify(ids));
$.ajax({
url: ctx+'api/book/remove',
type: 'POST',
contentType: "application/json",
dataType: "json",
data: JSON.stringify(ids),
success: function (result) {
if (result.code == 200) {
setTimeout(function () {
layer.closeAll();//关闭所有的弹出层
userTable.reload();
}, 300);
}else {
layer.msg("操作失败!", {icon: 5});
}
}
});
});
}
break;
case 'refresh':
userTable.reload();
break;
case 'getCheckData':
layer.alert(JSON.stringify(data));
break;
case 'getCheckLength':
var data = checkStatus.data;
layer.msg('选中了:' + data.length + ' 个');
break;
case 'isAll':
layer.msg(checkStatus.isAll ? '全选' : '未全选')
break;
}
;
});
// 监听工具条
table.on('tool(tb-book)', function (obj) {
var data = obj.data;
// 修改
if (obj.event === 'edit') {
top.layer.open({
type: 2,
offset: '10px',
title: "修改图书",
area: ['800px', '660px'],
content: [ctx+'bookEdit/'+data.id]
});
}
});
// 搜索
$('#search').click(function () {
var name = $('#name').val();
var isbn = $('#isbn').val();
var author = $('#author').val();
table.reload('tb-book', {
url: ctx+'api/book/list'
,where: {
name:name,isbn:isbn,author:author
}
,page: {
curr: 1
}
});
});
});
</script>
</body>
</html>
3.图书管理service
/**
* @Description: 图书服务
* @Author laoxu
* @Date 2019/5/2 9:26
**/
@Service
public class BookService extends AbstractService {
public void add(Book entity) {
//String username = SecurityUtil.getLoginUser();
insert("bookMapper.insert",entity);
}
public void modify(Book entity) {
update("bookMapper.update",entity);
}
public void remove(Long id) {
delete("bookMapper.delete",id);
}
public void removes(Long[] ids) {
delete("bookMapper.deletes",ids);
}
public Book get(Long id) {
return selectOne("bookMapper.select",id);
}
public Book getByIsbn(String isbn) {
return selectOne("bookMapper.selectByIsbn",isbn);
}
public List<Book> getParentList(Long id) {
return selectList("bookMapper.selectParentList",id);
}
public int count(Map<String, Object> param) {
return selectOne("bookMapper.count",param);
}
public List<Book> getList(Map<String, Object> param) {
return selectList("bookMapper.selectList",param);
}
public List<Book> getPageResult(Map<String, Object> param) {
return selectList("bookMapper.selectPageResult",param);
}
public int checkCode(Book entity){
return selectOne("bookMapper.countCode",entity);
}
}
4.图书管理controller
/**
* @Description: 图书控制器
* @Author laoxu
* @Date 2019/5/2 10:16
**/
@RestController
@RequestMapping("/api/book")
public class BookController {
@Autowired
BookService bookService;
/**
* 保存(新增/修改)
*
* @param entity
* @return
*/
@PostMapping("/save")
public Result<String> modify(@RequestBody Book entity) {
String code = entity.getIsbn();
int count = bookService.checkCode(entity);
if(count>0){
return ResultUtil.fail("ISBN已存在!");
}
if(entity.getId()!=null){
bookService.modify(entity);
}else{
bookService.add(entity);
}
return ResultUtil.ok();
}
@PostMapping("/remove")
public Result<String> remove(@RequestBody Long[] ids) {
bookService.removes(ids);
return ResultUtil.ok();
}
@GetMapping("/get")
public Result<Book> get(@RequestParam("id") Long id) {
Book entity = bookService.get(id);
return ResultUtil.ok(entity);
}
@GetMapping("/list")
public ResultBean<List<Book>> getPageResult(
@RequestParam(required = false) String name,
@RequestParam(required = false) String isbn,
@RequestParam(required = false) String author,
@RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "10") Integer limit) {
Map<String, Object> param = new HashMap<>();
// 计算起始行号
int offset = (page - 1) * limit;
int rows = limit;
param.put("name",name);
param.put("isbn",isbn);
param.put("author",author);
param.put("offset", offset);
param.put("rows", rows);
// 统计记录数
int totalRows = bookService.count(param);
// 获取当前页结果集
List<Book> entities = bookService.getPageResult(param);
ResultBean result = new ResultBean(0, "查询成功", totalRows, entities);
return result;
}
}