Java图书管理系统CRUD展示
简介
这篇文章是针对一个c++的编程题,进行的java的改变,然后在那个基础之上加上了一个网页的前端,主要的参考文章是这两篇,看完之后,就会发现,其实这个,大部分项目都是图书管理系统的简单变化而成的。
图书目录管理系统(C++)
springboot+vue+mybatis-plus+axios实现商品的CRUD
项目展示
项目创建部分
数据库编码
/*
Navicat MySQL Data Transfer
Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 80028
Source Host : localhost:3306
Source Schema : projectdatabase
Target Server Type : MySQL
Target Server Version : 80028
File Encoding : 65001
Date: 31/01/2023 22:24:02
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`isbn` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`author` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`publisher` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL,
`pubdate` datetime NOT NULL,
`price` decimal(10, 2) NOT NULL,
`id` int NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`, `isbn`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES ('9780439227148', 'The Call of the Wild', 'Jack London', 'Scholastic Press', '2001-01-01 00:00:00', 39.40, 1);
INSERT INTO `book` VALUES ('9787501592401', 'The Old Man and the Sea', 'Ernest Hemingway', 'Knowledge Press', '2023-01-30 00:00:00', 25.80, 2);
INSERT INTO `book` VALUES ('9787501592401', 'The Old Man and the Sea', 'Ernest Hemingway', 'Knowledge Press', '2023-01-31 13:02:42', 25.80, 3);
INSERT INTO `book` VALUES ('9780439227148', 'The Call of the Wild', 'Jack London', 'Scholastic Press', '2023-01-30 16:00:00', 34.90, 6);
INSERT INTO `book` VALUES ('9781772262902', 'Oliver Twist', 'Charles Dickens', 'Engage Books', '2023-01-03 16:00:00', 45.00, 7);
SET FOREIGN_KEY_CHECKS = 1;
项目编码
在创建项目的那些地方,在springboot+vue+mybatis-plus+axios实现商品的CRUD这篇文章里面,已经写好了,怎么创建一个springboot项目可以参考这篇文章。
这个题的原题是这样的,是一个C++的期末考试题目。
图书目录管理系统(C++)
原题描述
请设计一个简单的图书目录管理系统(初级版)。
图书目录信息包括:
统一书号(不超过13字符)
书名(不超过30字符)
作者(不超过20字符)
出版社(不超过30字符)
出版日期(包括:年、月、日)
价格(精确到分)
注意:为了保证运行窗口能正确地显示,请右击运行窗口,修改“属性”:
在“选项”选项卡中,选中“使用旧版本控制台”
在“字体”选项卡中,选择“8×16”“点阵字体”
在“布局”选项卡中,将屏幕窗口宽度均设为“120”。
具体需求如下:
显示如下的主菜单:
Append Find Remove Modify Show Quit > _
如果用户输入 A 或 a、F 或 f、R 或 r、M 或 m、S 或 s,则可完成相应的操作。如果用户输入其它字符,则显示错误信息。
程序将反复显示主菜单,让用户持续工作。如果用户输入 Q 或 q,则程序结束。
Append Find Remove Modify Show Quit > Q
Thank you! Goodbye!
若用户输入其它字符,则显示错误信息。
Append Find Remove Modify Show Quit > B
Incorrect choice!
Append Find Remove Modify Show Quit > 9
Incorrect choice!
Append Find Remove Modify Show Quit > +
Incorrect choice!
若用户输入 A 或 a,则可以输入新书的信息,将其添加到图书目录中。
Append Find Remove Modify Show Quit > a
ISBN: 9780439227148
Title: The Call of the Wild
Author: Jack London
Publisher: Scholastic Press
Pub date: 2001/1/1
Price: 39.4
Append Find Remove Modify Show Quit > A
ISBN: 9781772262902
Title: Oliver Twist
Author: Charles Dickens
Publisher: Engage Books
Pub date: 2016/9/15
Price: 648
Append Find Remove Modify Show Quit > a
ISBN: 9787515911076
Title: The Call of the Wild
Author: Jack London
Publisher: Aerospace Publishing House
Pub date: 2016/5/1
Price: 29.8
若用户输入的日期信息不正确,则显示错误信息,并要求用户重新输入。
Append Find Remove Modify Show Quit > A
ISBN: 9787501592401
Title: The Old Man and the Sea
Author: Ernest Hemingway
Publisher: Knowledge Press
Pub date: 2016/2/30
Incorrect date! Please reenter: 2016/6/31
Incorrect date! Please reenter: 2016/8/1
Price: 25.8
若用户输入 S 或 s,则按书号升序排序,然后列表显示全部图书。
Append Find Remove Modify Show Quit > S
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9780439227148 The Call of the Wild Jack London Scholastic Press 2001/01/01 39.40
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
若用户输入 F 或 f,则输入书名,然后显示该书名的图书。如果没有对应的图书,则显示错误信息。
Append Find Remove Modify Show Quit > F
Title: The Call of the Wild
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9780439227148 The Call of the Wild Jack London Scholastic Press 2001/01/01 39.40
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
Append Find Remove Modify Show Quit > f
Title: Gulliver's Travels
Not found!
说明:输出查找结果时,不作排序操作。
若用户输入 R 或 r,则输入书号,然后将删除该书号的图书。如果没有对应的图书,则显示错误信息。
Append Find Remove Modify Show Quit > r
ISBN: 9781772262902
Remove(y/n)? n
Append Find Remove Modify Show Quit > S
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9780439227148 The Call of the Wild Jack London Scholastic Press 2001/01/01 39.40
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
Append Find Remove Modify Show Quit > R
ISBN: 9780439227148
Remove(y/n)? Y
Append Find Remove Modify Show Quit > s
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
Append Find Remove Modify Show Quit > r
ISBN: 9787515914145
Not found!
Append Find Remove Modify Show Quit > s
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787515911076 The Call of the Wild Jack London Aerospace Publishing House 2016/05/01 29.80
要求:用户回答是否删除时,必须回答 Y 或 N (大小写均可)。如果是其它字符,则显示错误信息,要求用户重新回答。
Append Find Remove Modify Show Quit > R
ISBN: 9781772262902
Remove(y/n)? k
Incorrect answer!
Remove(y/n)? $
Incorrect answer!
Remove(y/n)? N
若用户输入 M 或 m,则可以修改图书信息。首先按书号查找,然后重新输入该图书的信息。
Append Find Remove Modify Show Quit > m
ISBN: 9787515911076
Modify(y/n)? y
ISBN: 9787544724968
Title: The House on Mango Street
Author: Sandra Heathneros
Publisher: Yilin Press
Pub date: 2012/1/1
Price: 30
Append Find Remove Modify Show Quit > M
ISBN: 9787501592401
Modify(y/n)? n
Append Find Remove Modify Show Quit > m
ISBN: 9787515914145
Not found!
Append Find Remove Modify Show Quit > S
ISBN--------- Title------------------------- Author-------------- Publisher--------------------- Pub-date-- Price---
9781772262902 Oliver Twist Charles Dickens Engage Books 2016/09/15 648.00
9787501592401 The Old Man and the Sea Ernest Hemingway Knowledge Press 2016/08/01 25.80
9787544724968 The House on Mango Street Sandra Heathneros Yilin Press 2012/01/01 30.00
要求:用户回答是否修改时,必须回答 Y 或 N (大小写均可)。如果是其它字符,则显示错误信息,要求用户重新回答。
Append Find Remove Modify Show Quit > M
ISBN: 9787544724968
Modify(y/n)? K
Incorrect answer!
Modify(y/n)? *
Incorrect answer!
Modify(y/n)? n
相关习题:图书目录管理系统(高级版)。
Append Find Remove Modify Show Quit > Thank you! Goodbye!
q
完整代码
项目结构
application.yml
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/projectdatabase?serverTimezone=UTC
username: root
password: 123456
mybatis-plus:
global-config:
db-config:
id-type: auto
# configuration:
# log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringBootCRUD</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootCRUD</name>
<description>SpringBootCRUD</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
前端文件
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<a href="/add"><input type="button" value="新增"></a><br>
<hr>
<table id="brandTable" border="1" cellspacing="0" width="100%">
<tr>
<th>序号</th>
<th>统一书号</th>
<th>书名</th>
<th>作者</th>
<th>出版社</th>
<th>出版日期</th>
<th>价格</th>
</tr>
<!--
使用v-for遍历tr
-->
<tr v-for="(book,i) in books" align="center" >
<td>{{i + 1}}</td>
<td>{{book.isbn}}</td>
<td>{{book.title}}</td>
<td>{{book.author}}</td>
<td>{{book.publisher}}</td>
<td>{{book.date2}}</td>
<td>{{book.price}}</td>
<td><input type="button" id="update" @click="updateId(book.id)" value="修改"> <input type="button" id="delete" @click="deleteId(book.id)" value="删除"></td>
</tr>
</table>
</div>
<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
</body>
<script>
new Vue({
el:"#app", // 这里写那个上面网页的div的id名称
data(){
return{
books:[]
}
},
mounted(){ // 当网页加载的时候加载
let _this = this; // 制作一个临时的this指针
axios({
method:"get",
url:"http://localhost:8080/selectAll"
}).then(function (response){
console.log(response.data)
_this.books = response.data;
})
},
methods: {
updateId(id){
let _this = this;
axios({
method:"get",
url:"http://localhost:8080/update?id"+id,
data:id
}).then(resp=>{
location.href = "http://localhost:8080/update"
})
},
deleteId(id) {
let _this = this;
axios({
method:"post",
url:"http://localhost:8080/delete",
data:id
}).then(resp=>{
if (resp.data){
location.href = "http://localhost:8080/"
} else {
alert("删除失败")
}
})
}
}
})
</script>
</html>
addBook.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加书本</title>
</head>
<body>
<div id="app">
<h3>添加书本</h3>
<form action="" method="post">
统一书号:<input id="isbn" v-model="book.isbn" name="isbn"><br>
书名:<input id="title" v-model="book.title" name="title"><br>
作者:<input id="author" v-model="book.author" name="author"><br>
出版社:<input id="publisher" v-model="book.publisher" name="publisher"><br>
出版日期:<input id="pubdate" v-model="book.pubdate" name="pubdate" type="date"><br>
价格:<input id="price" v-model="book.price" name="price"><br>
<!-- <input type="button" id="btn" @click="submitForm" value="提交">-->
<input type="button" id="btn" @click="submitForm" value="提交">
</form>
</div>
<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
<script>
new Vue({
el:"#app",
data() {
return{
book:{
}
}
},
methods:{
submitForm() {
var _this = this;
axios({
method: "post",
url:"http://localhost:8080/addBook",
data:_this.book
}).then(function (response) {
if (response.data){ // 添加成功之后返回原来的页面
location.href="http://localhost:8080/";
} else {
alert("添加失败")
}
})
}
}
})
</script>
</body>
</html>
updateBook.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>更新书本</title>
</head>
<body>
<div id="app">
<h3>更新书本</h3>
<form action="" method="post">
统一书号:<input id="isbn" v-model="book.isbn" name="isbn"><br>
书名:<input id="title" v-model="book.title" name="title"><br>
作者:<input id="author" v-model="book.author" name="author"><br>
出版社:<input id="publisher" v-model="book.publisher" name="publisher"><br>
出版日期:<input id="pubdate" v-model="book.pubdate" name="pubdate" type="date"><br>
价格:<input id="price" v-model="book.price" name="price"><br>
<!-- <input type="button" id="btn" @click="submitForm" value="提交">-->
<input type="button" id="btn" @click="submitForm" value="提交">
</form>
</div>
<script src="js/axios-0.18.0.js"></script>
<script src="js/vue.js"></script>
<script>
new Vue({
el:"#app",
data() {
return{
book:{
}
}
},
methods:{
submitForm() {
var _this = this;
axios({
method: "post",
url:"http://localhost:8080/addBook",
data:_this.book
}).then(function (response) {
if (response.data){ // 添加成功之后返回原来的页面
location.href="http://localhost:8080/";
} else{
alert("添加失败");
}
})
}
}
})
</script>
</body>
</html>
后端部分
Book
package com.example.springbootcrud.Bean;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
// 数据id
private Integer id;
// 图书编号
private String isbn;
// 图书名
private String title;
// 图书作者
private String author;
// 图书出版社
private String publisher;
// 图书出版日期
private Date pubdate;
// 只有年份的日期字符串
// private HashMap<String, Integer>map = new HashMap<String, Integer>(){
// {
// put("Jan", 1);
// put("Feb", 2);
// put("Mar", 3);
// put("Apr", 4);
// put("May", 5);
// put("Jun", 6);
// put("Jul", 7);
// put("Aug", 8);
// put("Sept", 9);
// put("Oct", 10);
// put("Nov", 11);
// put("Dec", 12);
// }
// };
public String getDate2(){
HashMap<String, Integer>map = new HashMap<String, Integer>(){
{
put("Jan", 1);
put("Feb", 2);
put("Mar", 3);
put("Apr", 4);
put("May", 5);
put("Jun", 6);
put("Jul", 7);
put("Aug", 8);
put("Sept", 9);
put("Oct", 10);
put("Nov", 11);
put("Dec", 12);
}
};
String strs[] = pubdate.toString().split(" ");
// for (int i = 0; i < strs.length; ++ i){
// System.out.println(strs[i]);
// }
String date = strs[5] + "年" + map.get(strs[1]) + "月" + strs[2] + "日";
System.out.println(date);
return date;
}
// 图书出版价格
private Double price;
}
BookController
package com.example.springbootcrud.Controller;
import com.alibaba.fastjson.JSON;
import com.example.springbootcrud.Bean.Book;
import com.example.springbootcrud.Dao.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;
@Controller
public class BookController {
@Autowired
BookDao bookDao;
@RequestMapping("/") // 设置默认页面
public String index(){
return "index.html";
}
@RequestMapping("/add")
public String add(){
return "addBook.html";
}
@RequestMapping("/update")
public String update(){
return "updateBook.html";
}
@ResponseBody
@GetMapping("/selectAll")
public List<Book> selectAll(){
System.out.println(bookDao.selectList(null).toString());
return bookDao.selectList(null);
}
@RequestMapping("/addBook")
@ResponseBody
public String addBook(HttpServletRequest request)throws ServletException, IOException {
BufferedReader reader = request.getReader();
String params = reader.readLine();
Book book = JSON.parseObject(params, Book.class);
System.out.println(book);
bookDao.insert(book);
return "success";
}
@RequestMapping("/updateId")
@ResponseBody
public Boolean updateBook(HttpServletRequest request)throws ServletException, IOException{
BufferedReader reader = request.getReader();
String params = reader.readLine();
Book book = JSON.parseObject(params, Book.class);
System.out.println(book);
if (bookDao.updateById(book) > 0) return true;
return false;
}
@RequestMapping("/delete")
@ResponseBody
public Boolean deleteBook(HttpServletRequest request)throws ServletException, IOException{
BufferedReader reader = request.getReader();
String params = reader.readLine();
System.out.println(params);
if (bookDao.deleteById(Integer.parseInt(params)) > 0) return true;
return false;
}
}
BookDao
package com.example.springbootcrud.Dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.springbootcrud.Bean.Book;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BookDao extends BaseMapper<Book> {
}
测试类
BookDaoTestCase
package com.example.springbootcrud.Dao;
import com.example.springbootcrud.Bean.Book;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
@SpringBootTest
public class BookDaoTestCase {
@Autowired
private BookDao bookDao;
@Test
void testSelectAll(){
System.out.println(bookDao.selectList(null));
}
@Test
void testSave(){
Book book = new Book();
book.setIsbn("9787515911076");
book.setTitle("The Call of the Wild");
book.setAuthor("Jack London");
book.setPublisher("Aerospace Publishing House");
book.setPubdate(new Date());
book.setPrice(360.00);
System.out.println(book);
bookDao.insert(book);
}
@Test
void testUpdate(){
Book book = new Book();
book.setId(3);
book.setIsbn("9787501592401");
book.setTitle("The Old Man and the Sea");
book.setAuthor("Ernest Hemingway");
book.setPublisher("Knowledge Press");
book.setPubdate(new Date());
book.setPrice(25.8);
bookDao.updateById(book);
}
@Test
void testDelete(){
bookDao.deleteById(5);
}
@Test
void testDate(){
}
}