自学Java day41 图书管理系统-springboot快速开发 从jvav到架构师

前端:html + css + jvavscript + vue + ajax + axios + element ui

后端:jvav + springboot + mybatisplus + mysql数据库

项目管理工具:maven

服务器:jetty

相关功能演示

首先启动jetty服务器

 进入页面

新增图书

 

编辑图书 

查询图书

 

 删除图书

 

前端源码: 

<!DOCTYPE html>

<html>

<head>

    <!-- 页面meta -->

    <meta charset="utf-8">

    <meta http-equiv="X-UA-Compatible" content="IE=edge">

    <title>jvav.springboot</title>

    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">

    <!-- 引入样式 -->

    <link rel="stylesheet" href="../plugins/elementui/index.css">

    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">

    <link rel="stylesheet" href="../css/style.css">

</head>

<body class="hold-transition">

<div id="app">

    <div class="content-header">

        <h1>图书管理</h1>

    </div>

    <div class="app-container">

        <div class="box">

            <div class="filter-container">

                <el-input placeholder="图书名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>

                <el-button @click="getx()" class="dalfBut">查询</el-button>

                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>

            </div>

            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>

                <el-table-column type="index" align="center" label="序号"></el-table-column>

                <el-table-column prop="type" label="图书类别" align="center"></el-table-column>

                <el-table-column prop="name" label="图书名称" align="center"></el-table-column>

                <el-table-column prop="description" label="描述" align="center"></el-table-column>

                <el-table-column label="操作" align="center">

                    <template slot-scope="scope">

                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>

                        <el-button type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>

                    </template>

                </el-table-column>

            </el-table>

            <!-- 新增标签弹层 -->

            <div class="add-form">

                <el-dialog title="新增图书" :visible.sync="dialogFormVisible">

                    <el-form ref="dataAddForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">

                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="图书类别" prop="type">

                                    <el-input v-model="formData.type"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="图书名称" prop="name">

                                    <el-input v-model="formData.name"/>

                                </el-form-item>

                            </el-col>

                        </el-row>


                        <el-row>

                            <el-col :span="24">

                                <el-form-item label="描述">

                                    <el-input v-model="formData.description" type="textarea"></el-input>

                                </el-form-item>

                            </el-col>

                        </el-row>

                    </el-form>

                    <div slot="footer" class="dialog-footer">

                        <el-button @click="dialogFormVisible = false">取消</el-button>

                        <el-button type="primary" @click="handleAdd()">确定</el-button>

                    </div>

                </el-dialog>

            </div>

            <!-- 编辑标签弹层 -->

            <div class="add-form">

                <el-dialog title="编辑检查项" :visible.sync="dialogFormVisible4Edit">

                    <el-form ref="dataEditForm" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">

                        <el-row>

                            <el-col :span="12">

                                <el-form-item label="图书类别" prop="type">

                                    <el-input v-model="formData.type"/>

                                </el-form-item>

                            </el-col>

                            <el-col :span="12">

                                <el-form-item label="图书名称" prop="name">

                                    <el-input v-model="formData.name"/>

                                </el-form-item>

                            </el-col>

                        </el-row>

                        <el-row>

                            <el-col :span="24">

                                <el-form-item label="描述">

                                    <el-input v-model="formData.description" type="textarea"></el-input>

                                </el-form-item>

                            </el-col>

                        </el-row>

                    </el-form>

                    <div slot="footer" class="dialog-footer">

                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>

                        <el-button type="primary" @click="handleEdit()">确定</el-button>

                    </div>

                </el-dialog>

            </div>

        </div>

    </div>

</div>

</body>

<!-- 引入组件库 -->

<script src="../js/vue.js"></script>

<script src="../plugins/elementui/index.js"></script>

<script type="text/javascript" src="../js/jquery.min.js"></script>

<script src="../js/axios-0.18.0.js"></script>

<script>
    var vue = new Vue({

        el: '#app',
        data: {
            pagination: {},
            dataList: [],//当前页要展示的列表数据
            formData: {},//表单数据
            dialogFormVisible: false,//控制表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见
            rules: {//校验规则
                type: [{required: true, message: '图书类别为必填项', trigger: 'blur'}],
                name: [{required: true, message: '图书名称为必填项', trigger: 'blur'}]
            }
        },

        //钩子函数,VUE对象初始化完成后自动执行
        created() {
            this.getAll();
        },

        methods: {

            //查询
            getx(){
                //console.log(this.pagination.queryString)
                axios.post("/books",{name:this.pagination.queryString}).then((res) => {
                    this.dataList = res.data.data;
                    //console.log(this.dataList)
                });
            },

            //列表
            getAll() {
                axios.get("/books").then((res) => {
                    this.dataList = res.data.data;
                });
            },

            //弹出添加窗口
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm()
            },

            //重置表单
            resetForm() {
                this.formData = {}
            },

            //添加
            handleAdd() {
                axios.post("/books", this.formData).then((res) => {
                    if (res.data.code == 20011) {
                        this.dialogFormVisible = false;
                        this.$message.success("添加成功")
                    } else if (res.data.code == 20010) {
                        this.$message.error("添加失败")
                    } else {
                        this.$message.error(res.data.msg)
                    }
                }).finally(() => {
                    this.getAll();
                })
            },

            //弹出编辑窗口
            handleUpdate(row) {
                this.dialogFormVisible4Edit = true
                this.formData = row
            },

            //编辑
            handleEdit() {
                axios.put("/books", this.formData).then((res) => {
                    if (res.data.code == 20031) {
                        this.dialogFormVisible = false;
                        this.$message.success("修改成功")
                    } else if (res.data.code == 20030) {
                        this.$message.error("修改失败")
                    } else {
                        this.$message.error(res.data.msg)
                    }
                }).finally(() => {
                    this.getAll();
                })
            },

            // 删除
            handleDelete(row) {


                this.$confirm("此操作将永久删除当前数据,是否继续?", "提升", {
                    type: 'info'
                }).then(() => {
                    //删除业务

                    axios.delete("/books/" + row.id).then((res) => {

                        if (res.data.code == 20021) {
                            this.dialogFormVisible = false;
                            this.$message.success("删除成功")
                        } else if (res.data.code == 20020) {
                            this.$message.error("删除失败")
                        } else {
                            this.$message.error(res.data.msg)
                        }

                    }).finally(() => {
                        this.getAll();
                    })

                }).catch(() => {
                    //取消删除
                    this.$message.info("取消删除操作")
                })


            }
        }
    })

</script>

</html>

 

后端源码:

        Spring Boot 简化了 Spring 应用繁琐的搭建和开发过程,去除了大量的 XML 配置文件,简化了复杂的依赖管理。

Service实现类:

SQL语句由mybatisplus生成

package com.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.company.controller.Code;
import com.company.dao.BookDao;
import com.company.domain.Book;
import com.company.excetption.BusinessException;
import com.company.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookServiceImpl implements BookService {

    @Autowired
    BookDao bookDao;

    public boolean add(Book book) {
        return bookDao.insert(book) > 0;
    }

    public boolean deleteById(int id) {
        if (id <= 0) {
            throw new BusinessException(Code.BUSINESS_ERR, "你丫的传的些什么");
        }
        return bookDao.deleteById(id) > 0;
    }

    public boolean update(Book book) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", book.getId());
        return bookDao.update(book, queryWrapper) > 0;
    }

    public Book getById(int id) {
        return bookDao.selectById(id);
    }

    public List<Book> select() {
        return bookDao.selectList(new QueryWrapper());
    }

    public List<Book> getx(Book book) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(book.getName() != "", "name", book.getName());
        return bookDao.selectList(queryWrapper);
    }

}

实例类:

相关方法由@Data注解生成

package com.company.domain;

import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;

@Data
public class Book {
    //@TableId(type = IdType.AUTO)
    private Integer id;
    private String type;
    private String name;
    private String description;
    @TableLogic(value = "0" ,delval = "1")
    private Integer deleted;
    
}

 

异常处理:

package com.company.controller;

import com.company.excetption.BusinessException;
import com.company.excetption.SystemException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ProjectErr {

    @ExceptionHandler(BusinessException.class)
    public Result businessErr(BusinessException e) {
        return new Result(e.getCode(), null, e.getMessage());
    }

    @ExceptionHandler(SystemException.class)
    public Result systemErr(SystemException e) {
        return new Result(e.getCode(), null, e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result err(Exception e) {
        return new Result(Code.SYSTEM_UNKNOW_ERR, null, "系统繁忙,请稍后再试");
    }
}

因采用mybatisplus逻辑删除,因此相关数据未真正删除,依然保留在数据库中,方便日后管理

至此,已完成对jvav se 、jvav sql、jvav web、jvav spring系的学习

完整源码过于啰嗦,这里就不放了,有需要者欢迎评论/私信获取

世界线回溯,从jvav到架构师

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值