图书借阅项目练习
Posted 池鱼i_
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了图书借阅项目练习相关的知识,希望对你有一定的参考价值。
- 题目描述和需要实现的整体功能
- 题目描述:某高校有一学生的书籍经常被其他同学借阅,由于没有一个很好的信息记录,导致有部分书籍丢失。现在需要给该生设计“书籍借阅管理系统”来实现该生对自己书籍的借阅信息的管理。程序操作者为学生本人,无需登录
- 需要实现的整体功能:
- 查询书籍信息列表
- 查询借书信息列表
- 新增借书信息
- 还书
- 各省各专业所用技术要求:
html、mysql、JavaSE、javascript、jQuery、JSP、SSM
- 题目功能明细及评分标准:
序号 | 功能列表 | 功能描述 | 分数 |
1 | 数据库设计 (20分) | 创建数据库 | 2 |
2 | 创建表结构及其约束 | 15 | |
3 | 添加测试数据 | 3 | |
4 | 框架搭建(10分) | 根据所用技术搭建项目框架 | 10 |
5 | 书籍信息页面 功能(15分) | 按要求正确进行界面的制作 | 5 |
6 | 查询书籍信息列表的后台代码 | 5 | |
7 | 查询书籍信息列表AJAX获取数据展示到页面 | 5 | |
11 | 借阅信息页面 功能(20分) | 按要求正确进行界面的制作 | 5 |
12 | 查询借书信息列表的后台代码 | 5 | |
13 | 查询书籍信息列表的正确展示到页面 | 5 | |
14 | 正确显示还书状态:已还书绿色,未归还红色 | 2 | |
15 | 借书信息列表中的书籍名称的正确显示 | 1 | |
16 | 根据还书状态显示操作:借阅结束文字,还书按钮 | 2 | |
17 | 还书功能(9分) | 点击“还书”按钮:正确修改借书状态为“已还书” | 5 |
19 | 点击“还书”按钮:弹窗提示“还书成功” | 2 | |
20 | 点击“还书”按钮:还书后,跳转借书信息页面 | 2 | |
21 | 新增借阅信息 功能(21分) | 按要求正确进行界面的制作 | 5 |
22 | 借阅信息页面的表单非空验证 | 2 | |
23 | 借阅信息页面的电话号码11位验证 | 2 | |
24 | 可借的书籍名称由AJAX加载完成 | 5 | |
25 | 新增借阅信息的后台代码 | 5 | |
26 | 新增借阅信息后跳转到借阅信息页面 | 2 | |
27 | 总体编程技术(5分) | 代码的书写规范,命名规范2和适当的注释3 | 5 |
- 数据库表结构如下:
数据库名称:bookdb | ||||
表:bookinfo (书籍表) | ||||
字段显示 | 字段名 | 数据类型 | 默认值 | 备注和说明 |
书籍编号 | bookId | int |
| 主键,自增 |
书籍名称 | bookName | varchar(50) |
| 不允许为空 |
书籍作者 | bookWrite | varchar(20) |
| 不允许为空 |
书籍介绍 | bookContent | text |
| 不允许为空 |
表:borrowInfo (借书信息表) | ||||
字段显示 | 字段名 | 数据类型 | 默认值 | 备注和说明 |
借阅编号 | borrowId | int |
| 主键,自增 |
学生姓名 | stuName | varchar(20) |
| 不允许为空 |
班级 | clazz | varchar(50) |
| 不允许为空 |
联系电话 | tel | varchar(11) |
| 不允许为空 |
借书日期 | borrowDate | date |
| 不允许为空 |
还书日期 | returnDate | date |
| 可以为空 |
还书状态 | status | int |
| 值为0或1,分别表示: 0:已还书 1:未归还 |
所借书籍编号 | bookId | int |
| 外键,所借书籍的编号 |
- 题目功能需求详细描述
- 数据库:创建数据库bookdb,创建数据表:bookinfo和borrowInfo,至少向每张表中添加3条测试数据,其中在向borrowInfo表中添加测试数据时,借书信息的状态需要有2个不同的值,参考数据如下:
- 列表界面对应的功能:
- “查询书籍信息列表”的具体要求如下:
- index.html或index.jsp页面访问后,AJAX加载当前已有书籍信息
- 页面设计要求符合图片所示
- 点击“查看借阅信息”超链接可跳转至“借阅信息列表页面”
- “借阅信息列表页面”的具体要求如下:
- 在borrow.html或borrow.jsp页面展示当前书籍的借阅信息
- 归还状态0显示为“绿色”的“已还书”
- 归还状态1显示为“红色”的“未归还”
- 已还书显示操作“借阅结束”,未还书显示“还书”按钮
- 未还书则还书日期不显示
- 显示借书信息对应的书籍名称
- “还书”的具体要求如下:
- 当单击“还书”按钮或超链接后,改变借阅信息状态为0
- 还书成功后,弹窗提示“还书成功”
- 还书成功后,即时刷新列表上面的数据
- 当单击借阅信息列表页面上的“新增借阅”按钮时,跳转到借书页面。
-
- 按照图片设计页面元素
- 书籍列表下拉菜单通过AJAX加载
- 所有表单添加非空验证,手机号必须11位
- 点击“取消”则返回到借书信息页面
- 程序功能实现后,将数据库脚本文件与源代码一同提交。
- 注意事项
- 仔细审题,把题目要求理解准确;
- 请注意按照页面的设计要求来进行页面的设计;
- 请注意代码的书写、命名规范和适当的注释。
1数据库代码
CREATE DATABASE bookdb CHARACTER set utf8;
use bookdb;
CREATE TABLE bookinfo(
bookId int PRIMARY KEY auto_increment,
bookName varchar(50) not null,
bookWrite varchar(20) not null,
bookContent text not null
);
CREATE TABLE borrowInfo(
borrowId int PRIMARY KEY auto_increment,
stuName varchar(20) not null,
clazz VARCHAR(50) not null,
tel varchar(11) not null,
borrowDate date not null,
returnDate date,
status int,
bookId int
);
INSERT into bookinfo VALUES(null,"三国演义","罗贯中","东汉末年到西晋初年...");
INSERT into bookinfo VALUES(null,"西游记","吴承恩","孙悟空出世及大闹天宫后...");
INSERT into bookinfo VALUES(null,"水浒传","施耐庵","梁山好汉反抗欺压...");
INSERT into bookinfo VALUES(null,"红楼梦","曹雪芹","贾史王薛四大家族的兴衰...");
INSERT into borrowInfo VALUES(null,"张三","2020级软件一班",18738171861,"2021-02-01","2021-02-09",0,1);
INSERT into borrowInfo VALUES(null,"李四","2020级软件二班",18738171862,"2021-02-03","2021-02-07",0,2);
INSERT into borrowInfo VALUES(null,"王五","2020级软件一班",18738171863,"2021-02-09",null,1,3);
INSERT into borrowInfo VALUES(null,"张三","2020级软件一班",18738171864,"2021-02-10",null,1,4);
ssm做出的项目
需要导包 外加一个js
配置文件:springMVC mybatis 以及 web.xml
首先是SpringMVC配置文件:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="cn.hp"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="viewResolver">
<property name="prefix" value="/WEB-INF/pages/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!-- 需要修改此处的 xxx为 当前项目的数据库名称 -->
<property name="url" value="jdbc:mysql://localhost:3306/bookdb?characterEncoding=utf8"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.hp.dao"></property>
</bean>
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis_config.xml"></property>
</bean>
</beans>
mybatis: 在mapper.xml中写sql语句
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>
Web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring_config.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>*.js</url-pattern>
<url-pattern>*.css</url-pattern>
</servlet-mapping>
</web-app>
先写出页面:请求转发 :index.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<jsp:forward page="showAll"></jsp:forward>
</body>
</html>
在web-inf下创建pages:showAll.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>书籍信息列表</h1>
书籍添加和管理暂未开通
<a href="borrow">查看借阅信息</a>
<table border="1px" cellspacing="0px" width="60%">
<tr>
<th>书籍编号</th>
<th>书籍名称</th>
<th>书籍作者</th>
<th>书籍简介</th>
</tr>
<c:forEach items="${list}" var="book">
<tr>
<td>${book.bookId}</td>
<td>${book.bookName}</td>
<td>${book.bookWrite}</td>
<td>${book.bookContent}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
然后是borrow.jsp 需要用到js包
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<script src="/js/jquery-3.6.0.js"></script>
<body>
<a href="showAll">查看书籍库存信息</a>
<a href="add.jsp">新增借阅</a>
<table border="1px" cellspacing="0px" width="60%">
<tr align="center">
<th>编号</th>
<th>借阅人</th>
<th>班级</th>
<th>电话</th>
<th>借书日期</th>
<th>还书日期</th>
<th>归还情况</th>
<th>书籍名称</th>
<th>操作</th>
</tr>
<c:forEach items="${list}" var="borrow">
<tr>
<td>${borrow.borrowId}</td>
<td>${borrow.stuName}</td>
<td>${borrow.clazz}</td>
<td>${borrow.tel}</td>
<td>${borrow.borrowDate}</td>
<td>${borrow.returnDate}</td>
<c:if test="${borrow.status == 1}">
<td><span style="color: red">未归还</span></td>
</c:if>
<c:if test="${borrow.status == 0}">
<td><span style="color: green">已归还</span></td>
</c:if>
<td>${borrow.bookInfo.bookName}</td>
<td>
<c:if test="${borrow.status == 0}"> 借阅结束</c:if>
<c:if test="${borrow.status == 1}">
<form action="returnBook">
<input type="hidden" name="borrowId" value="${borrow.borrowId}" />
<input id="sub" type="submit" value="归还"/>
</form>
</c:if>
</td>
</tr>
</c:forEach>
</table>
<script>
$("#sub").click(function () {
alert("还书成功");
})
</script>
</body>
</html>
添加页面 不需要放在WEB-INF下 放在web包下即可
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<form action="add">
借阅人:<input type="text" name="stuName"> <br/>
班级:<input type="text" name="clazz"> <br/>
电话:<input type="text" name="tel"> <br/>
书籍列表:<select name="bookId">
<option value="1">三国演义</option>
<option value="2">西游记</option>
<option value="3">水浒传</option>
<option value="4">红楼梦</option>
</select> <br/>
<input type="submit" value="确定">
<a href="index.jsp"><input type="submit" value="取消"></a>
</form>
</body>
</html>
接着就是实现功能:
Controller类:
package cn.hp.controller;
import cn.hp.model.BorrowInfo;
import cn.hp.service.GjbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
@Controller
public class GjbController {
@Autowired
private GjbService GjbService;
@RequestMapping("/showAll")
public String showAll(HttpServletRequest request){
List list=GjbService.showAll();
request.setAttribute("list",list);
return "showAll";
}
@RequestMapping("/borrow")
public String borrowAll(HttpServletRequest request){
List list=GjbService.borrowAll();
request.setAttribute("list",list);
return "borrow";
}
@RequestMapping("/add")
public String add(BorrowInfo borrowInfo){
boolean addFlag = GjbService.add(borrowInfo);
if(addFlag){
return "redirect:borrow";
}else{
return "forward:add.jsp";
}
}
@RequestMapping("/returnBook")
public String returnBook(String borrowId){
GjbService.returnBook(borrowId);
return "forward:borrow";
}
}
dao类:
mapper接口
package cn.hp.dao;
import cn.hp.model.BorrowInfo;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface GjbMapper {
List selectAll();
List borrowAll();
int add(BorrowInfo borrowInfo);
void updateById(String borrowId);
}
mapper.xml文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.hp.dao.GjbMapper">
<resultMap id="map1" type="cn.hp.model.BorrowInfo">
<id column="borrowId" property="borrowId"></id>
<result column="stuName" property="stuName"></result>
<result column="clazz" property="clazz"></result>
<result column="tel" property="tel"></result>
<result column="borrowDate" property="borrowDate"></result>
<result column="returnDate" property="returnDate"></result>
<result column="status" property="status"></result>
<result column="bookId" property="bookId"></result>
<association property="bookInfo" javaType="cn.hp.model.BookInfo">
<id column="bookId" property="bookId"></id>
<result column="bookName" property="bookName"></result>
<result column="bookWrite" property="bookWrite"></result>
<result column="bookContent" property="bookContent"></result>
</association>
</resultMap>
<select id="selectAll" resultType="cn.hp.model.BookInfo">
select * from bookinfo
</select>
<select id="borrowAll" resultMap="map1">
select * from borrowInfo left join bookinfo on borrowInfo.bookId=bookinfo.bookId;
</select>
<insert id="add" parameterType="cn.hp.model.BorrowInfo">
insert into borrowinfo values (null,#{stuName},#{clazz},#{tel},now(),null,1,#{bookId})
</insert>
<update id="updateById">
update borrowinfo set returnDate = now() , status = 0 where borrowId = #{borrowId }
</update>
</mapper>
定义两个类来写属性:
第一个BookInfo:
package cn.hp.model;
public class BookInfo {
private int bookId;
private String bookName;
private String bookWrite;
private String bookContent;
@Override
public String toString() {
return "BookInfo{" +
"bookId=" + bookId +
", bookName='" + bookName + '\\'' +
", bookWrite='" + bookWrite + '\\'' +
", bookContent='" + bookContent + '\\'' +
'}';
}
public BookInfo(int bookId, String bookName, String bookWrite, String bookContent) {
this.bookId = bookId;
this.bookName = bookName;
this.bookWrite = bookWrite;
this.bookContent = bookContent;
}
public BookInfo() {
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookWrite() {
return bookWrite;
}
public void setBookWrite(String bookWrite) {
this.bookWrite = bookWrite;
}
public String getBookContent() {
return bookContent;
}
public void setBookContent(String bookContent) {
this.bookContent = bookContent;
}
}
第二个borrowInfo 先写上面那个类 再写此类:
package cn.hp.model;
public class BorrowInfo {
private int borrowId;
private String stuName;
private String clazz;
private String tel;
private String borrowDate;
private String returnDate;
private int status;
private int bookId;
private BookInfo bookInfo;
@Override
public String toString() {
return "BorrowInfo{" +
"borrowId=" + borrowId +
", stuName='" + stuName + '\\'' +
", clazz='" + clazz + '\\'' +
", tel='" + tel + '\\'' +
", borrowDate='" + borrowDate + '\\'' +
", returnDate='" + returnDate + '\\'' +
", status=" + status +
", bookId=" + bookId +
", bookInfo=" + bookInfo +
'}';
}
public int getBorrowId() {
return borrowId;
}
public void setBorrowId(int borrowId) {
this.borrowId = borrowId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getBorrowDate() {
return borrowDate;
}
public void setBorrowDate(String borrowDate) {
this.borrowDate = borrowDate;
}
public String getReturnDate() {
return returnDate;
}
public void setReturnDate(String returnDate) {
this.returnDate = returnDate;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public int getBookId() {
return bookId;
}
public void setBookId(int bookId) {
this.bookId = bookId;
}
public BookInfo getBookInfo() {
return bookInfo;
}
public void setBookInfo(BookInfo bookInfo) {
this.bookInfo = bookInfo;
}
}
service层:
package cn.hp.service;
import cn.hp.dao.GjbMapper;
import cn.hp.model.BorrowInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GjbService {
@Autowired
private GjbMapper GjbMapper;
public List showAll() {
return GjbMapper.selectAll();
}
public List borrowAll() {
return GjbMapper.borrowAll();
}
public boolean add(BorrowInfo borrowInfo) {
int i= GjbMapper.add(borrowInfo);
if(i>0){
return true;
}
return false;
}
public void returnBook(String borrowId) {
GjbMapper.updateById(borrowId);
}
}
这就是整个代码,对童鞋们有用的话记得一键三连QWQ
以上是关于图书借阅项目练习的主要内容,如果未能解决你的问题,请参考以下文章
javaweb项目图书借阅管理系统设计与实现(有文档+调试视频教程+项目源码).rar
Java项目:图书借阅管理系统(java+SpringBoot+Mybatis+Html+maven+Mysql)