项目名称:图书管理系统。
主要业务:管理学校图书信息,记录并管理学生借阅图书信息。主要实现查询、新增、删除、修改图书借阅信息。
开发环境:windows、idea
技术栈:MySQL、Ajax、JDBC、Servlet、Maven、Jackson
下载settings.xml文件和本地仓库压缩包M2Repository.zip这两个文件。
(我的github里有这两个文件,往下看,就在下面放置源码库里哦)
图片里的lombok文件是什么呢?
lombok是一个可以通过简单的注解的形式来帮助我们简化消除一些必须有但显得很臃肿的 Java 代码的工具,简单来说,比如我们新建了一个类,然后在其中写了几个字段,然后通常情况下我们需要手动去建立getter和setter方法啊,构造函数啊之类的,lombok的作用就是为了省去我们手动创建这些代码的麻烦,它能够在我们编译源码的时候自动帮我们生成这些方法。
其实lombok文件在idea里就可以直接安装,但是由于我的idea一直加载不出来,所以就得我手动安装啦。
具体代码文件见项目源码
写一个form表单,给后台一个路径。
在后端写登录页面,当输入正确的用户名和密码时,点击登录,即可跳转到相应界面。当用户名或密码错误时,点击登录,会提示用户名或密码错误。
drop database if exists book;
create database book character set utf8mb4;
use book;
drop table if exists user;
create table user(
id int primary key auto_increment,
username varchar(20) not null unique comment '用户账号',
password varchar(20) not null comment '密码',
nickname varchar(20) comment '用户昵称',
email varchar(50) comment '邮箱',
create_time timestamp default NOW() comment '创建时间'
) comment '用户表';
drop table if exists student;
create table student(
id int primary key auto_increment,
student_name varchar(20) not null comment '姓名',
student_no varchar(20) comment '学号',
id_card varchar(20) comment '身份证号',
student_email varchar(50) comment '邮箱',
classes_id int comment '班级id',
create_time timestamp default NOW() comment '创建时间',
foreign key (classes_id) references classes(id)
) comment '学生表';
drop table if exists classes;
create table classes(
id int primary key auto_increment,
classes_name varchar(20) not null comment '班级名称',
classes_graduate_year varchar(20) comment '毕业年份,数据字典000001',
classes_major varchar(20) comment '专业,数据字典000002',
classes_desc varchar(50) comment '备注',
create_time timestamp default NOW() comment '创建时间'
) comment '班级表';
drop table if exists book;
create table book(
id int primary key auto_increment,
book_name varchar(50) not null comment '图书名称',
author varchar(20) comment '作者',
price decimal(10,2) comment '价格',
create_time timestamp default NOW() comment '创建时间'
) comment '图书信息';
drop table if exists borrow_record;
create table borrow_record(
id int primary key auto_increment,
book_id int comment '图书id',
student_id int comment '学生id',
start_time timestamp not null comment '借阅日期',
end_time timestamp null default null comment '归还日期',
create_time timestamp default NOW() comment '创建时间',
foreign key (book_id) references book(id),
foreign key (student_id) references student(id)
) comment '图书借阅信息';
drop table if exists dictionary;
create table dictionary(
id int primary key auto_increment,
dictionary_key varchar(20) not null unique comment '键',
dictionary_value varchar(20) not null comment '值',
dictionary_desc varchar(20) comment '备注',
create_time timestamp default NOW() comment '创建时间'
) comment '数据字典';
drop table if exists dictionary_tag;
create table dictionary_tag(
id int primary key auto_increment,
dictionary_tag_key varchar(20) not null comment '键',
dictionary_tag_value varchar(20) not null comment '值',
dictionary_tag_desc varchar(20) comment '备注',
dictionary_id int comment '数据字典id',
create_time timestamp default NOW() comment '创建时间',
foreign key (dictionary_id) references dictionary(id)
) comment '数据字典标签';
数据字典表和数据字典标签表主要用在一些通用的下拉菜单选项。
下拉菜单的选项都是通过发送http请求,从数据库查询出来的。
-- 初始化数据
-- mysql中没有==,是用=号代替==。为了区分=和==,赋值时使用:=
set @username:='abc';
set @password:='123';
set @nickname:='柠萌呀';
set @email:='[email protected]';
set @dictionary_classes_graduate_year='000001';
set @dictionary_classes_major='000002';
set @classes_name:='大学';
set @student_name:='柠萌';
insert into user(username, nickname, password, email) values (@username, @nickname, @password, @email);
insert into user(username, nickname, password, email) values (concat(@username, '1'), concat(@nickname, '1'), @password, @email);
insert into user(username, nickname, password, email) values (concat(@username, '2'), concat(@nickname, '2'), @password, @email);
insert into user(username, nickname, password, email) values (concat(@username, '3'), concat(@nickname, '3'), @password, @email);
insert into user(username, nickname, password, email) values (concat(@username, '4'), concat(@nickname, '4'), @password, @email);
insert into user(username, nickname, password, email) values (concat(@username, '5'), concat(@nickname, '5'), @password, @email);
## 数据字典:学生毕业年份
insert into dictionary(dictionary_key, dictionary_value, dictionary_desc)values (@dictionary_classes_graduate_year, '毕业年份', '学生毕业的年份');
insert into dictionary_tag(dictionary_tag_key, dictionary_tag_value, dictionary_id)values ('001', '2020届', 1);
insert into dictionary_tag(dictionary_tag_key, dictionary_tag_value, dictionary_id)values ('002', '2021届', 1);
insert into dictionary_tag(dictionary_tag_key, dictionary_tag_value, dictionary_id)values ('003', '2022届', 1);
insert into dictionary_tag(dictionary_tag_key, dictionary_tag_value, dictionary_id)values ('004', '2023届', 1);
## 数据字典:学生专业
insert into dictionary(dictionary_key, dictionary_value, dictionary_desc)values (@dictionary_classes_major, '专业', '学生的专业');
insert into dictionary_tag(dictionary_tag_key, dictionary_tag_value, dictionary_id)values ('001', '中文系', 2);
insert into dictionary_tag(dictionary_tag_key, dictionary_tag_value, dictionary_id)values ('002', '英语系', 2);
insert into dictionary_tag(dictionary_tag_key, dictionary_tag_value, dictionary_id)values ('003', '计算机科学与技术', 2);
insert into classes(classes_name, classes_graduate_year, classes_major, classes_desc) values (concat(@classes_name, '三年级'), '000001001', '000002003', '为自己的未来不断努力,不断充实自己');
insert into classes(classes_name, classes_graduate_year, classes_major, classes_desc) values (concat(@classes_name, '二年级'), '000001002', '000002003', '开始明确自己的未来,为自己制定目标');
insert into classes(classes_name, classes_graduate_year, classes_major, classes_desc) values (concat(@classes_name, '一年级'), '000001003', '000002003', '刚步入大学学生,对大学生活充满好奇');
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'A1'), 's00001', '222222222222222222', @email, 1);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'A2'), 's00002', '222222222222222223', @email, 1);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'A3'), 's00003', '222222222222222224', @email, 1);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'A4'), 's00004', '222222222222222225', @email, 1);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'A5'), 's00005', '222222222222222226', @email, 1);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'B1'), 's00006', '222222222222222227', @email, 2);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'B2'), 's00007', '222222222222222228', @email, 2);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'B3'), 's00008', '222222222222222229', @email, 2);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'B4'), 's00009', '222222222222222230', @email, 2);
insert into student(student_name, student_no, id_card, student_email, classes_id) values (concat(@student_name, 'B5'), 's00010', '222222222222222231', @email, 2);
insert into book(book_name, author, price, create_time) values ('高等数学', '小米', 115.2, now());
insert into book(book_name, author, price, create_time) values ('诗和远方', '小明', 61.5, now());
insert into book(book_name, author, price, create_time) values ('美术', '小画', 33, now());
insert into book(book_name, author, price, create_time) values ('四级单词', '小梦', 33, now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (1, 1, '2020-01-01 14:20:00', '2020-02-01 14:20:00', now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (1, 2, '2020-03-01 14:20:00', '2020-03-22 14:20:00', now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (1, 3, '2020-05-01 14:20:00', '2020-05-12 14:20:00', now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (2, 5, '2020-01-01 14:20:00', '2020-02-03 14:20:00', now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (2, 7, '2020-02-04 14:20:00', '2020-03-03 14:20:00', now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (2, 9, '2020-04-01 14:20:00', '2020-06-01 14:20:00', now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (3, 3, '2020-02-01 14:20:00', '2020-02-21 14:20:00', now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (3, 6, '2020-04-01 14:20:00', '2020-05-01 14:20:00', now());
insert into borrow_record(book_id, student_id, start_time, end_time, create_time) values (3, 8, '2020-06-01 14:20:00', '2020-06-06 14:20:00', now());
根据数据库中每个表的元素,来写每个实体类。
下图为所有实体类。
举例:如实体Book类
package lemon.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import java.math.BigDecimal;
import java.util.Date;
/**
* 图书信息
*/
@Getter
@Setter
@ToString
public class Book extends DictionaryTag{
private Integer id;
/**
* 图书名称
*/
private String bookName;
/**
* 作者
*/
private String author;
/**
* 价格
*/
private BigDecimal price;
/**
* 创建时间
*/
private Date createTime;
private Book book;
private Classes classes;
private Student student;
}
package lemon.dao;
import lemon.exception.SystemException;
import lemon.model.Book;
import lemon.util.DBUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
public class BookDAO {
public static List<Book> queryAsDict() {
List<Book> books = new ArrayList<>();
Connection c = null;
PreparedStatement ps = null;
ResultSet rs = null;
try{
c = DBUtil.getConnection();
String sql = "select id, book_name, author,price from book";
ps = c.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()){
Book book = new Book();
book.setDictionaryTagKey(String.valueOf(rs.getInt("id")));
book.setDictionaryTagValue(rs.getString("book_name"));
book.setAuthor(rs.getString("author"));
book.setPrice(rs.getBigDecimal("price"));
books.add(book);
}
}catch (Exception e){
throw new SystemException("00004","查询图书数据字典出错",e);
}finally {
DBUtil.close(c,ps,rs);
}
return books;
}
}
https://github.com/ningmeng-zy/Projects