整合系列停更,后期打算用boot重构该demo

一、数据库、功能设计

1、图书分类

分类.jpg

2、图书信息

图书.jpg

3、功能设计

管理.jpg

二、分类entity

package cn.kexing.pojo;

public class BooksKind {
    private int bookKindID;
    private String bookKindName;

    public BooksKind() {
    }

    public BooksKind(int bookKindID, String bookKindName) {
        this.bookKindID = bookKindID;
        this.bookKindName = bookKindName;
    }

    public int getBookKindID() {
        return bookKindID;
    }

    public void setBookKindID(int bookKindID) {
        this.bookKindID = bookKindID;
    }

    public String getBookKindName() {
        return bookKindName;
    }

    public void setBookKindName(String bookKindName) {
        this.bookKindName = bookKindName;
    }

    @Override
    public String toString() {
        return "BooksKind{" +
                "bookKindID=" + bookKindID +
                ", bookKindName='" + bookKindName + '\'' +
                '}';
    }
}

三、Dao层

1、接口

package cn.kexing.dao;

import cn.kexing.pojo.BooksKind;

import java.util.List;

public interface BooksKindMapper {
    //查询所有分类
    List<BooksKind> selectAllKinds();

    //根据ID查询分类信息
    List<BooksKind> selectKindBookByID(int bookKindID);

    //修改书籍分类
    int updateKindsBook(BooksKind booksKind);

    //根据id删除分类
    int deleteKind(int bookKindID);

    //查找图书中有没有该分类
    Integer isInBook(int bookKindID);

    //新增分类
    int addKind(String bookKindName);

    //模糊查询
    List<BooksKind> selectForName(String bookKindName);

}

2、mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="cn.kexing.dao.BooksKindMapper">

<select id="selectAllKinds" resultType="BooksKind">
    select * from bookskind
</select>

    <select id="selectKindBookByID" parameterType="int" resultType="BooksKind">
        select * from bookskind where bookKindID = #{bookKindID}
    </select>

    <update id="updateKindsBook" parameterType="BooksKind">
        update bookskind set bookKindName = #{bookKindName} where bookKindID = #{bookKindID}
    </update>

    <delete id="deleteKind" parameterType="int">
        delete from bookskind where bookKindID=#{bookKindID}
    </delete>

    <select id="isInBook" resultType="Integer">
        select max(bookID) from books where bookKindID=#{bookKindID}
    </select>

    <insert id="addKind" parameterType="String">
        INSERT INTO bookskind(bookKindName) VALUE(#{bookKindName})
    </insert>

    <select id="selectForName" resultType="BooksKind" parameterType="String">
        select * from bookskind
        <where>
            <if test=" bookKindName != null or bookKindName !='' ">
                AND bookKindName like "%"#{bookKindName}"%"
            </if>
        </where>
    </select>

</mapper>

3、Impl

package cn.kexing.dao;

import cn.kexing.pojo.BooksKind;
import org.mybatis.spring.SqlSessionTemplate;

import java.util.List;

public class BooksKindMapperImpl implements BooksKindMapper {
    private SqlSessionTemplate sqlSession;

    public void setSqlSession(SqlSessionTemplate sqlSession) {
        this.sqlSession = sqlSession;
    }

    public List<BooksKind> selectAllKinds() {
        BooksKindMapper mapper = sqlSession.getMapper(BooksKindMapper.class);
        return mapper.selectAllKinds();
    }

    public List<BooksKind> selectKindBookByID(int bookKindID) {
        BooksKindMapper mapper = sqlSession.getMapper(BooksKindMapper.class);
        return mapper.selectKindBookByID(bookKindID);
    }

    public int updateKindsBook(BooksKind booksKind) {
        BooksKindMapper mapper = sqlSession.getMapper(BooksKindMapper.class);
        return mapper.updateKindsBook(booksKind);
    }

    public int deleteKind(int bookKindID) {
        BooksKindMapper mapper = sqlSession.getMapper(BooksKindMapper.class);
        return mapper.deleteKind(bookKindID);
    }

    public Integer isInBook(int bookKindID) {
        BooksKindMapper mapper = sqlSession.getMapper(BooksKindMapper.class);
        return mapper.isInBook(bookKindID);
    }

    public int addKind(String bookKindName) {
        BooksKindMapper mapper = sqlSession.getMapper(BooksKindMapper.class);
        return mapper.addKind(bookKindName);
    }

    public List<BooksKind> selectForName(String bookKindName) {
        BooksKindMapper mapper = sqlSession.getMapper(BooksKindMapper.class);
        return mapper.selectForName(bookKindName);
    }
}

四、service层

1、接口

package cn.kexing.service;

import cn.kexing.pojo.BooksKind;

import java.util.List;

public interface BooksKindService {
    //查询所有分类
    List<BooksKind> selectAllKinds();

    //根据ID查询分类信息
    List<BooksKind> selectKindBookByID(int bookKindID);

    //修改书籍分类
    int updateKindsBook(BooksKind booksKind);

    //根据id删除分类
    int deleteKind(int bookKindID);

    Integer isInBook(int bookKindID);

    //新增分类
    int addKind(String bookKindName);

    //模糊查询
    List<BooksKind> selectForName(String bookKindName);
}

2、Impl

package cn.kexing.service;

import cn.kexing.dao.BooksKindMapperImpl;
import cn.kexing.pojo.BooksKind;

import java.util.List;

public class BooksKindServiceImpl implements BooksKindService{
    private BooksKindMapperImpl booksKindMapper;

    public void setBooksKindMapper(BooksKindMapperImpl booksKindMapper) {
        this.booksKindMapper = booksKindMapper;
    }

    public List<BooksKind> selectAllKinds() {
        return booksKindMapper.selectAllKinds();
    }

    public List<BooksKind> selectKindBookByID(int bookKindID) {
        return booksKindMapper.selectKindBookByID(bookKindID);
    }

    public int updateKindsBook(BooksKind booksKind) {
        return booksKindMapper.updateKindsBook(booksKind);
    }

    public int deleteKind(int bookKindID) {
        return booksKindMapper.deleteKind(bookKindID);
    }

    public Integer isInBook(int bookKindID) {
        return booksKindMapper.isInBook(bookKindID);
    }

    public int addKind(String bookKindName) {
        return booksKindMapper.addKind(bookKindName);
    }

    public List<BooksKind> selectForName(String bookKindName) {
        return booksKindMapper.selectForName(bookKindName);
    }
}

五、controller层

这里的CRUD和之前的没多大差别,这里有个小细节要处理,因为图书表引用了分类表的外籍,在删除分类时需要判断是否已有书籍引用该分类,如果有则不能删除,必须先删除该分类下的所有图书
删除功能.jpg

package cn.kexing.contoller;

import cn.kexing.pojo.Books;
import cn.kexing.pojo.BooksKind;
import cn.kexing.service.BooksKindService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/bookskindcontroller")
public class BooksKindController {
    @Autowired
    @Qualifier(value = "booksKindService")
    private BooksKindService booksKindService;

    //分页查询
    @RequestMapping("/selectallkinds")
    public ModelAndView selectAllKinds(@RequestParam(defaultValue="1")Integer currentPage){
        PageHelper.startPage(currentPage,5);
        //紧跟的一条查询语句会被分页
        List<BooksKind> booksKinds = booksKindService.selectAllKinds();
        PageInfo pageInfo = new PageInfo(booksKinds,5);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("BooksKind");
        return mv;
    }

    //模糊分页查询
    @RequestMapping("/selectforname")
    public ModelAndView selectForName(@RequestParam(defaultValue="1")Integer currentPage,String bookKindName){
        PageHelper.startPage(currentPage,5);
        //紧跟的一条查询语句会被分页
        List<BooksKind> booksKinds = booksKindService.selectForName(bookKindName);
        PageInfo pageInfo = new PageInfo(booksKinds,5);
        ModelAndView mv = new ModelAndView();
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("BooksKind");
        return mv;
    }

    @RequestMapping("/toupdatekindbooks")
    public String toUpdateKindBooks(Integer bookKindID,Model model){
        //根据ID查询
        List<BooksKind> booksKinds = booksKindService.selectKindBookByID(bookKindID);
        System.out.println(booksKinds);
        model.addAttribute("QKinds",booksKinds);
        return "toUpdateKindsBook";
    }

    //到数据库进行修改
    @RequestMapping("/updatekindbooks")
    public String updateKindBooks(BooksKind booksKind){
        int res = booksKindService.updateKindsBook(booksKind);
        if(res == 1){
            return "redirect:/bookskindcontroller/selectallkinds";
        }
        return "redirect:/bookskindcontroller/toupdatekindbooks";
    }

    @RequestMapping("/deletekinds")
    public String deleteKinds(int bookKindID){
        booksKindService.deleteKind(bookKindID);
        return "redirect:/bookskindcontroller/selectallkinds";
    }

    //判断是否能够删除该分类
    @RequestMapping("/isinbook")
    public String isInBook(int bookKindID,Model model){
        Integer res = booksKindService.isInBook(bookKindID);
        System.out.println(res);
        if(res != null){
            model.addAttribute("msg",true);
            model.addAttribute("ID",bookKindID);
        }else {
            model.addAttribute("msg", false);
            model.addAttribute("ID",bookKindID);
        }
        return "BooksKind";
    }

    @RequestMapping("/toaddkind")
    public String toAddKind(){
        return "toAddKind";
    }

    @RequestMapping("/addkind")
    public String addKind(String bookKindName){
        int res = booksKindService.addKind(bookKindName);
        if(res > 0){
            return "redirect:/bookskindcontroller/selectallkinds";
        }else{
            return "redirect:/bookskindcontroller/addkind";
        }
    }

}

六、JSP层(前端代码量大,只放了首页)

<%@ page import="cn.kexing.pojo.Login" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>

    <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <title>Title</title>

    <link href="http://cdn.bootcss.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet">
    <script src="http://cdn.bootcss.com/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>


</head>
<body style="overflow:-Scroll;overflow-y:hidden">

<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <a class="navbar-brand" href="${pageContext.request.contextPath}/book/toindex">可星图书管理系统V3.0</a>

            <ul class="nav navbar-nav navbar-right function">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">系统管理 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="${pageContext.request.contextPath}/root_updatepasswordcontroller/toupdatepassword?username=<%=session.getAttribute("username")%>">修改密码</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">制定规则</a></li>
                    </ul>
                </li>

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">图书管理 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="${pageContext.request.contextPath}/bookskindcontroller/selectallkinds">图书类别管理</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="${pageContext.request.contextPath}/book/selectbookforpage">图书信息管理</a></li>
                    </ul>
                </li>

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">读者管理 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">读者类别管理</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">读者信息管理</a></li>
                    </ul>
                </li>

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">借阅管理 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">借书</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">还书</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">续借</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">催还</a></li>
                    </ul>
                </li>
            </ul>
        </div>

        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <li><a>欢迎&nbsp;<span style="font-size: 19px;font-family: fantasy;color: cornflowerblue"><%=session.getAttribute("username")%></span></a></li>
                <li><a href="${pageContext.request.contextPath}/book/deletesession?session=<%=request.getSession()%>">注销</a></li>
            </ul>
        </div>
    </div>
</nav>

<div class="container showBook">
    <div class="row clearfix">
        <div class="col-md-12">
            <h1 class="page-header">
                <small>图书分类展示</small>
            </h1>
        </div>
    </div>

    <div class="col-md-1">
        <button class="btn btn-default" type="submit" ><a href="/bookskindcontroller/toaddkind">新增分类</a></button>
    </div>
    <div class="col-md-4">
        <button class="btn btn-default" type="submit" ><a href="/bookskindcontroller/selectallkinds">查询全部分类</a></button>
    </div>

    <div class="col-md-4" style="float: right;position: relative;bottom: 8px">
        <form class="navbar-form navbar-left" role="search" action="${pageContext.request.contextPath}/bookskindcontroller/selectforname" method="post">
            <div class="form-group">
                <input type="text" class="form-control" placeholder="请输入图书分类名称" name="bookKindName">
                <button type="submit" class="btn btn-default">搜索</button>
            </div>
        </form>
    </div>
    <div class="row clearfix">
        <div class="col-md-12">
            <table class="table table-hover table-bordered">
                <tr>
                    <th>图书分类ID</th>
                    <th>图书分类名称</th>
                    <th>操作</th>
                </tr>

                <c:forEach var="book" items="${pageInfo.list}">
                    <tr>
                        <td id="kindID">${book.bookKindID}</td>
                        <td>${book.bookKindName}</td>
                        <td>
                            <a href="${pageContext.request.contextPath}/bookskindcontroller/toupdatekindbooks?bookKindID=${book.bookKindID}">修改</a>
                            &nbsp;|&nbsp;
                            <a id="delete" href="${pageContext.request.contextPath}/bookskindcontroller/isinbook?bookKindID=${book.bookKindID}">删除</a>
                        </td>
                    </tr>
                </c:forEach>

            </table>
        </div>
    </div>

    <script>
        if (${msg}){
            alert("请先删除该分类下的所有图书");
            window.location= "${pageContext.request.contextPath}/bookskindcontroller/selectallkinds";
        }else{
            alert("删除成功")
            window.location= "${pageContext.request.contextPath}/bookskindcontroller/deletekinds?bookKindID=${ID}";
        }
    </script>
    <div style="float: left;font-size: 16px;width: 500px;height: 100px;margin-top: 15px">
        <span>共<span style="font-size: 19px;font-family: fantasy;color: cornflowerblue">&nbsp;${pageInfo.total}&nbsp;</span>条记录,</span>
        <span>当前显示第<span style="font-size: 19px;font-family: fantasy;color: cornflowerblue">&nbsp;${pageInfo.pageNum}&nbsp;</span>页,</span>
        <span>共<span style="font-size: 19px;font-family: fantasy;color: cornflowerblue">&nbsp;${pageInfo.pages}&nbsp;</span>页</span>
    </div>

    <nav aria-label="Page navigation" style="float: right;">
        <ul class="pagination">
            <c:if test="${pageInfo.hasPreviousPage}">
                <li>
                    <a href="${pageContext.request.contextPath}/bookskindcontroller/selectallkinds?currentPage=${pageInfo.pageNum-1}" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span></a>
                </li>
            </c:if>

            <c:if test="${pageInfo.hasPreviousPage == false}">
                <li>
                    <a href="#">&laquo;</a>
                </li>
            </c:if>

            <c:forEach items="${pageInfo.navigatepageNums}" var="page_num">
                <c:if test="${page_num == pageInfo.pageNum}">
                    <li>
                        <a href="#">${page_num}</a>
                    </li>
                </c:if>

                <c:if test="${page_num != pageInfo.pageNum}">
                    <li>
                        <a href="${pageContext.request.contextPath}/bookskindcontroller/selectallkinds?currentPage=${page_num}">${page_num}</a>
                    </li>
                </c:if>
            </c:forEach>


            <c:if test="${pageInfo.hasNextPage}">
                <li>
                    <a href="${pageContext.request.contextPath}/bookskindcontroller/selectallkinds?currentPage=${pageInfo.pageNum+1}" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                    </a>
                </li>
            </c:if>

            <c:if test="${pageInfo.hasNextPage == false}">
                <li>
                    <a href="#">&raquo;</a>
                </li>
            </c:if>
        </ul>
    </nav>
</div>

</body>
</html>

这里分类删除的时候通过控制器判断后进行了重定向

七、页面

书籍页面
书籍展示.jpg

分类页面
分类功能截图.jpg

删除.jpg

最后修改:2020 年 09 月 17 日 08 : 31 AM
如果觉得我的文章对你有用,请随意赞赏