一、图书管理系统需求分析

一、背景分析

​ 一直以来人们使用传统的人工方式管理图书资料。这种方式存在着许多缺点,如效率低、保密性差且较为繁琐。图书管理作为计算机应用的一个分支,有着手工管理无法比拟的优点,如检索迅速、查找方便、可靠性高、存储量大、保密性好、寿命长、成本低等。这些优点能够极大地提高图书管理的效率。因此,开发一套能够为用户提供充足的信息和快捷的查询手段的图书管理系统,将是非常必要的,也是十分及时的。

二、功能模块分析

功能模块

上图为图书馆管理系统的功能模块概览,该图书馆管理系统主要分为两大模块:

一、管理员模块

1.1、用户管理

模块功能 说明
管理员登录 用户输入用户名和密码进行登录,并作出相应提示,登录的同时需要对其身份进行验证,判断此用户为管理员还是普通账户,如果为管理员,则跳转到管理员界面,如果为普通用户则跳转到普通用户界面。
管理员添加 在管理员界面,管理员选择管理员添加操作,可进行管理员添加,添加时需要进行重名检测。
普通用户列表查询 选择普通用户列表查询,可查询所有已经注册的普通用户的基本信息列表(密码等隐私信息不可显示)。
用户账户禁用与开启 管理员可对指定的普通用户进行账号禁用和开启操作,用户账号禁用后则不能进行登录。

1.2、书籍管理

模块功能 说明
书籍添加 管理员在书籍管理界面进行书籍的添加,书籍添加时,书籍名可以重复,但是需要书籍编号作为主键。
添书籍基础信息时,要指定此书籍的类别编号,可单独定义一张书籍类别表和书籍表关联,但是此类别表事先定义好数据,不作任何修改。
书籍删除 管理员通过书籍编号可对指定的书籍进行删除,前提是这本书处于无人借阅状态,否则删除失败。
书籍修改 管理员通过书籍编号可对指定书籍的信息进修改。
书籍列表查询 进行书籍列表查询时,需要管理员选择要查询的字段(如书籍名、书籍作者、书籍类型),查询时都是进行模糊查询。
书籍详情查看 根据书籍编号进行书籍详细信息显示。

二、普通用户模块

2.1、用户个人信息管理

模块功能 说明
用户登录 用户输入用户名和密码进行登录,并作出相应提示,登录的同时需要对其身份进行验证,判断此用户为管理员还是普通账户,如果为管理员,则跳转到管理员界面,如果为普通用户则跳转到普通用户界面。
用户注册 普通使用此图书馆平台时,需要进行用户注册,注册时,需要对用户名进行重名检测。
个人基础信息查询 用户登录后,可查询当前用户的基础信息(不包含密码等敏感信息)。
个人基础信息修改 用户登录后,可对当前基础信息进行修改,用户名不可修改。
密码修改 修改密码等敏感信息,需要单独用页面进行,修改时,需要用输入原始密码进行验证。

2.2、书籍管理

模块功能 说明
书籍列表查询 进行书籍列表查询时,需要用户选择要查询的字段(如书籍名、书籍作者、书籍类型),查询时都是进行模糊查询。
书籍详细查询 用户通过输入书籍编号,查询当前书籍的详细信息。
书籍借阅 用户通过输入书籍编号进行相应书籍借阅,每次借阅行为只能借阅一本书籍。借阅时,如果此书籍状态为不可借阅,则借阅失败;如果此书籍库存数量为0,则借阅失败;借阅成功,则当前书籍库存数量为-1。
书籍归还 在书籍归还界面,用书输入要归还的书籍编号进行书籍归还,然后将相应书籍的借阅状态修改成已归还,并将相关书籍的库存数量+1。
个人借阅历史 通过此界面可查看自己借阅过的书籍列表,显示时按照借阅时间排序,并且显示此书籍的借阅状态(已归还、借阅中)

三、表结构

1、用户表(t_user)

字段名 数据类型 说明
user_id int 用户id,主键
user_name varchar 用户名,唯一,非空
password varchar 用户密码,非空
age int 年龄
gender varchar 性别
phone varchar 电话号码
address varchar 地址
user_state int 用户账号状态: 0:正常 , 1:禁用
role int 用户性质:0:管理员,1:普通用户

2、书籍表(t_book)

字段名 数据类型 说明
book_id int 书籍编号,主键
book_name varchar 书籍名,非空
book_author varchar 书籍作者,非空
book_num int 书籍库存数量,非空
book_category int 书籍类别,该字段值和书籍类别表中的类别编号,非空
book_state int 书籍是否可借阅状态: 0:可借阅 , 1:不可借阅

3、书籍类别表(book_category)

字段名 数据类型 说明
category_id int 书籍类别编号,主键
category_name varchar 书籍类别名,非空(如政治、军事、体育、经济等)

4、借阅信息表(t_borrowing_info)

字段名 数据类型 说明
borrowing_info_id int 当前这条借阅数据的id,主键, 用户每借阅一本书籍时,会在当前借阅表中生成一条数据
user_id int 发生借阅行为的用户id,非空
book_id int 被借阅的书籍的id,非空
borrowing_state int 借阅状态: 0:借阅中 1:已归还
borrowing_time datetime 借阅时间
giveback_time datetime 归还书籍时间,初始为空,归还书籍后,显示最后还书时间

二、MVC设计模式

​ MVC 模式(Model–view–controller)是软件工程中的一种软件架构模式,它把软件系统分为三个基本部分:模型(Model)、视图(View)和控制器(Controller)。

​ MVC 模式的目的是实现一种动态的程序设计,简化后续对程序的修改和扩展,并且使程序某一部分的重复利用成为可能。除此之外,MVC 模式通过对复杂度的简化,使程序的结构更加直观。软件系统在分离了自身的基本部分的同时,也赋予了各个基本部分应有的功能。专业人员可以通过自身的专长进行相关的分组:

  • 模型(Model):程序员编写程序应有的功能(实现算法等等)、数据库专家进行数据管理和数据库设计(可以实现具体的功能);
  • 控制器(Controller):负责转发请求,对请求进行处理;
  • 视图(View):界面设计人员进行图形界面设计。

MVC 模式的图形描述如下

MVC图解
MVC模式中三个组件的详细介绍如下

  • 模型(Model):用于封装与应用程序的业务逻辑相关的数据以及对数据的处理方法。“Model”有对数据直接访问的权力,例如对数据库的访问。“Model”不依赖“View”和“Controller”,也就是说, Model 不关心它会被如何显示或是如何被操作。但是 Model 中数据的变化一般会通过一种刷新机制被公布。为了实现这种机制,那些用于监视此 Model 的 View 必须事先在此 Model 上注册,由此,View 可以了解在数据 Model 上发生的改变。(比如:观察者模式(软件设计模式));
  • 视图(View):能够实现数据有目的的显示(理论上,这不是必需的)。在 View 中一般没有程序上的逻辑。为了实现 View 上的刷新功能,View 需要访问它监视的数据模型(Model),因此应该事先在被它监视的数据那里注册;
  • 控制器(Controller):起到不同层面间的组织作用,用于控制应用程序的流程。它处理事件并作出响应。“事件”包括用户的行为和数据 Model 上的改变。

从MVC模式的一般理解来看,视图层与模型层是存在直接联系的,并且模型层的变化会通过视图层反映出来,这确实是MVC模式的标准理解,不过在我目前接触到的实际应用中,更多的情况时,视图层与模型层是通过控制层联系起来的,两者之间并无直接的联系,三者之间的关系更类似下图所示:

MVC图解
结合MVC模式的标准解释来看,上述模式可能是MVC模式的一种变型使用。

三、实际开发

一、项目结构介绍

首先先看一下我的项目结构:
目录结构一目录结构二
项目各包作用

包名 作用
lib 根据mysql版本下载的数据库连接器
具体下载地址为:https://mvnrepository.com/artifact/mysql/mysql-connector-java
util 存放了工具包,此项目中具体存放了Jdbc工具类
cn.egret 存放所有项目文件的总包
dao 存放和数据库交互的Java文件
entity 存放实体类
service 进一步封装了dao包的工具函数,引用dao包的函数
各impl包 存放了各包接口的实现类
controller 存放了控制器类,具体封装了各功能函数(如登录、注册等)
page 存放了各主要页面
smallPage 存放了小页面(面板)
smallPage.admin 存放了管理员界面的小页面
smallPage.common 存放了普通用户界面的小页面
test 存放了dao包各函数的测试类

二、开发流程

一、创建数据库

利用Navicat Premium 15新建数据库library,然后根据需求设计各表,最终效果如下图所示:
数据库
以下是数据库设计代码,由Navicat Premium 15自动生成,可保存后在自己的数据库中运行,便可得到以上各表:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Navicat Premium Data Transfer

Source Server : localhost_3306
Source Server Type : MySQL
Source Server Version : 80017
Source Host : localhost:3306
Source Schema : library

Target Server Type : MySQL
Target Server Version : 80017
File Encoding : 65001

Date: 29/07/2021 21:18:41
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for book_category
-- ----------------------------
DROP TABLE IF EXISTS `book_category`;
CREATE TABLE `book_category` (
`category_id` int(11) NOT NULL COMMENT '书籍类别编号,主键',
`category_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '书籍类别名,非空(如政治、军事、体育、经济等)',
PRIMARY KEY (`category_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of book_category
-- ----------------------------
INSERT INTO `book_category` VALUES (1, '政治');
INSERT INTO `book_category` VALUES (2, '计算机科学');
INSERT INTO `book_category` VALUES (3, '文学');

-- ----------------------------
-- Table structure for t_book
-- ----------------------------
DROP TABLE IF EXISTS `t_book`;
CREATE TABLE `t_book` (
`book_id` int(11) NOT NULL COMMENT '书籍编号,主键',
`book_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '书籍名,非空',
`book_author` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '书籍作者,非空',
`book_num` int(11) NOT NULL COMMENT '书籍库存数量,非空',
`book_category` int(255) NOT NULL COMMENT '书籍类别,该字段值和书籍类别表中的类别编号,非空',
`book_state` int(255) NOT NULL COMMENT '书籍是否可借阅状态:0:可借阅 , 1:不可借阅',
PRIMARY KEY (`book_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of t_book
-- ----------------------------
INSERT INTO `t_book` VALUES (1, '操作系统', '王五', 3, 2, 0);
INSERT INTO `t_book` VALUES (2, '计算机组成原理', '王五', 1, 2, 0);
INSERT INTO `t_book` VALUES (3, '世界政治', '佚名', 2, 1, 0);
INSERT INTO `t_book` VALUES (4, '红色革命', '伟人', 1, 1, 0);
INSERT INTO `t_book` VALUES (5, '愤青的那些事', '愤青', 3, 3, 0);
INSERT INTO `t_book` VALUES (6, '爱恨别离伤春秋', '有情人', 3, 3, 0);

-- ----------------------------
-- Table structure for t_borrowing_info
-- ----------------------------
DROP TABLE IF EXISTS `t_borrowing_info`;
CREATE TABLE `t_borrowing_info` (
`borrowing_info_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '当前这条借阅数据的id,主键,\r\n用户每借阅一本书籍时,会在当前借阅表中生成一条数据\r\n当前这条借阅数据的id,主键,用户每借阅一本书籍时,会在当前借阅表中生成一条数据',
`user_id` int(11) NOT NULL COMMENT '发生借阅行为的用户id,非空',
`book_id` int(11) NOT NULL COMMENT '被借阅的书籍的id,非空',
`borrowing_state` int(11) NOT NULL COMMENT '借阅状态:0:借阅中 1:已归还',
`borrowing_time` datetime NOT NULL COMMENT '借阅时间',
`giveback_time` datetime NULL DEFAULT NULL COMMENT '归还书籍时间,初始为空,归还书籍后,显示最后还书时间',
PRIMARY KEY (`borrowing_info_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of t_borrowing_info
-- ----------------------------

-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id,主键',
`user_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户名,唯一,非空',
`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL COMMENT '用户密码,非空',
`age` int(11) NULL DEFAULT NULL COMMENT '年龄',
`gender` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '性别',
`phone` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '电话号码',
`address` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '地址',
`user_state` int(11) NOT NULL COMMENT '用户账号状态:\r\n0:正常 , 1:禁用\r\n用户账号状态:\r\n0:正常 , 1:禁用\r\n用户账号状态:0:正常 , 1:禁用',
`role` int(11) NOT NULL COMMENT '用户性质:0:管理员,1:普通用户',
PRIMARY KEY (`user_id`) USING BTREE,
UNIQUE INDEX `user_name`(`user_name`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of t_user
-- ----------------------------
INSERT INTO `t_user` VALUES (1, '张三', '123456', 18, '男', '123123', '厦门', 0, 1);
INSERT INTO `t_user` VALUES (2, '李四', '123456', 19, '女', '456456', '江苏', 0, 1);
INSERT INTO `t_user` VALUES (3, 'admin', '111111', 9999, '未知', '未知', '互联网', 0, 0);

SET FOREIGN_KEY_CHECKS = 1;

二、添加数据库连接器

在项目中新建lib文件夹,然后在网上下载和自己mysql数据库相对应的连接器,放到lib文件夹中,然后右击连接器文件—>Build Path—>Add to Build Path
添加连接器

三、创建文件jdbc.properties

新建文件jdbc.properties,在里面填上以下内容:

1
2
3
4
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/library?useUnicode=true&characterEncoding=UTF-8&userSSL=true&serverTimezone=UTC
username=root #数据库账户
password=123456 #数据库密码

四、创建工具类JdbcUtil.java

在util包中新建工具类JdbcUtil.java,以下为代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package cn.egret.util;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class JdbcUtil {
private static String url;
private static String username;
private static String password;
private static String driver;

static {
// 读取资源文件,获取值
// 创建properties集合类
Properties properties = new Properties();
try {
// 获取src路径下的文件的⽅式--->ClassLoader 类加载器
ClassLoader classLoader = JdbcUtil.class.getClassLoader();

// URL表示统⼀资源标识符
URL res = classLoader.getResource("jdbc.properties");
String path = res.getPath();
System.out.println(path);

// 2. 加载文件
properties.load(new FileReader(path));
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
driver = properties.getProperty("driver");
Class.forName(driver);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 获取连接
*
* @return 连接对象
*/

public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}

/***
* DML操作(增删改) 1.获取连接数据库对象 2.预处理 3.执行更新操作
*
* @param sql
* @param obj
*/
// 调用者只需传入一个sql语句,和一个Object数组。该数组存储的是SQL语句中的占位符
public static boolean executeUpdate(String sql, Object... obj) {

Connection con = null;
try {
con = getConnection(); // 调用getConnection()方法连接数据库
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

PreparedStatement ps = null;
try {
ps = con.prepareStatement(sql);// 预处理

for (int i = 0; i < obj.length; i++) {// 预处理声明占位符
ps.setObject(i + 1, obj[i]);
}
int count = ps.executeUpdate();// 执行更新操作

if(count > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

close(null, ps, con);// 调用close()方法关闭资源
}
return false;
}

/***
* DQL查询 Result获取数据集
*
* @param sql
* @param obj
* @return
*/
public static List<Map<String, Object>> executeQuery(String sql, Object... obj) {
Connection con = null;
try {
con = getConnection();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ResultSet rs = null;
PreparedStatement ps = null;

try {
ps = con.prepareStatement(sql);
for (int i = 0; i < obj.length; i++) {
ps.setObject(i + 1, obj[i]);
}
rs = ps.executeQuery();

// new 一个空的list集合用来存放查询结果
List<Map<String, Object>> list = new ArrayList<>();

// 获取结果集的列数
int count = rs.getMetaData().getColumnCount();

// 对结果集遍历每一条数据是一个Map集合,列是key,值是value
while (rs.next()) {
// 一个空的map集合,用来存放每一行数据
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < count; i++) {
Object ob = rs.getObject(i + 1);// 获取值
String key = rs.getMetaData().getColumnName(i + 1);// 获取k即列名
map.put(key, ob);
}
list.add(map);
}
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

close(rs, ps, con);
}

return null;

}

/**
* 释放资源
*
* @param rs
* @param ps
* @param conn
*/
public static void close(ResultSet rs, PreparedStatement ps, Connection conn) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}

五、创建各实体类

根据数据库表的需要在entity包中新建各实体类,以下为各实体类代码:

Book.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package cn.egret.entity;

public class Book {
private int bookId; // 书本ID
private String bookName; // 书名
private String bookAuthor; // 作者
private int bookNum; // 书本剩余量
private int bookCategory; // 书本类别
private int bookState; // 书本状态 0表示可借阅 1表示不可借阅

public Book() {
// TODO Auto-generated constructor stub
}

public Book(int bookId, String bookName, String bookAuthor, int bookNum, int bookCategory, int bookState) {
super();
this.bookId = bookId;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookNum = bookNum;
this.bookCategory = bookCategory;
this.bookState = bookState;
}

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 getBookAuthor() {
return bookAuthor;
}

public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}

public int getBookNum() {
return bookNum;
}

public void setBookNum(int bookNum) {
this.bookNum = bookNum;
}

public int getBookCategory() {
return bookCategory;
}

public void setBookCategory(int bookCategory) {
this.bookCategory = bookCategory;
}

public int getBookState() {
return bookState;
}

public void setBookState(int bookState) {
this.bookState = bookState;
}

@Override
public String toString() {
return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookAuthor=" + bookAuthor + ", bookNum="
+ bookNum + ", bookCategory=" + bookCategory + ", bookState=" + bookState + "]";
}

}

User.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package cn.egret.entity;

public class User {
private int userId; // 用户ID
private String userName; // 用户名
private String passWord; // 用户密码
private int age; // 年龄
private String gender; // 性别
private String phone; // 手机号
private String address; // 地址
private int userState; // 用户状态, 0表示可用 1表示禁用封号
private int role; // 用户类别, 0表示管理员,1表示普通用户

public User() {
// TODO Auto-generated constructor stub
}

public User(int userId, String userName, String passWard, int age, String gender, String phone, String address,
int userState, int role) {
super();
this.userId = userId;
this.userName = userName;
this.passWord = passWard;
this.age = age;
this.gender = gender;
this.phone = phone;
this.address = address;
this.userState = userState;
this.role = role;
}

public User(String userName, String passWard, int age, String gender, String phone, String address, int userState,
int role) {
super();
this.userName = userName;
this.passWord = passWard;
this.age = age;
this.gender = gender;
this.phone = phone;
this.address = address;
this.userState = userState;
this.role = role;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public String getUserName() {
return userName;
}

public void setUserName(String userName) {
this.userName = userName;
}

public String getPassWord() {
return passWord;
}

public void setPassWord(String passWard) {
this.passWord = passWard;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public String getGender() {
return gender;
}

public void setGender(String gender) {
this.gender = gender;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getUserState() {
return userState;
}

public void setUserState(int userState) {
this.userState = userState;
}

public int getRole() {
return role;
}

public void setRole(int role) {
this.role = role;
}

@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName + ", passWard=" + passWord + ", age=" + age
+ ", gender=" + gender + ", phone=" + phone + ", address=" + address + ", userState=" + userState
+ ", role=" + role + "]";
}

}

BookCategory.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package cn.egret.entity;

public class BookCategory {
private int categoryId; // 书籍类别编号
private String categoryName; // 书籍类别名

public BookCategory() {
// TODO Auto-generated constructor stub
}

public BookCategory(int categoryId, String categoryName) {
super();
this.categoryId = categoryId;
this.categoryName = categoryName;
}

public int getCategoryId() {
return categoryId;
}

public void setCategoryId(int categoryId) {
this.categoryId = categoryId;
}

public String getCategoryName() {
return categoryName;
}

public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

@Override
public String toString() {
return "BookCategory [categoryId=" + categoryId + ", categoryName=" + categoryName + "]";
}


}

BorrowingInfo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package cn.egret.entity;

import java.sql.Timestamp;

public class BorrowingInfo {
private int borrowingInfoId; // 借阅记录ID
private int userId; // 用户ID
private int bookId; // 书籍ID
private int borrowingState; // 当前借阅状态
private Timestamp borrowingTime; // 借阅时间
private Timestamp giveBackTime; // 归还时间

public BorrowingInfo() {
// TODO Auto-generated constructor stub
}

public BorrowingInfo(int borrowingInfoId, int userId, int bookId, int borrowingState, Timestamp borrowingTime,
Timestamp giveBackTime) {
super();
this.borrowingInfoId = borrowingInfoId;
this.userId = userId;
this.bookId = bookId;
this.borrowingState = borrowingState;
this.borrowingTime = borrowingTime;
this.giveBackTime = giveBackTime;
}

public int getBorrowingInfoId() {
return borrowingInfoId;
}

public void setBorrowingInfoId(int borrowingInfoId) {
this.borrowingInfoId = borrowingInfoId;
}

public int getUserId() {
return userId;
}

public void setUserId(int userId) {
this.userId = userId;
}

public int getBookId() {
return bookId;
}

public void setBookId(int bookId) {
this.bookId = bookId;
}

public int getBorrowingState() {
return borrowingState;
}

public void setBorrowingState(int borrowingState) {
this.borrowingState = borrowingState;
}

public Timestamp getBorrowingTime() {
return borrowingTime;
}

public void setBorrowingTime(Timestamp borrowingTime) {
this.borrowingTime = borrowingTime;
}

public Timestamp getGiveBackTime() {
return giveBackTime;
}

public void setGiveBackTime(Timestamp giveBackTime) {
this.giveBackTime = giveBackTime;
}

@Override
public String toString() {
return "BorrowingInfo [borrowingInfoId=" + borrowingInfoId + ", userId=" + userId + ", bookId=" + bookId
+ ", borrowingState=" + borrowingState + ", borrowingTime=" + borrowingTime + ", giveBackTime="
+ giveBackTime + "]";
}

}

六、dao包各类的创建

各接口类

IBookDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package cn.egret.dao;

import java.util.List;

import cn.egret.entity.Book;

public interface IBookDao {

boolean addBook(Book book); // 添加书籍

boolean deleteBook(int bookId, String bookName); // 删除书籍

List<Book> findBookList(); // 书籍列表查询

Book findBook(int bookId, String bookName); // 书籍详细信息查询

boolean changeBookState(int bookId, String bookName, int newState); // 修改书籍状态

boolean borrowBook(int bookId, String bookName); // 借书

boolean giveBackBook(int bookId, String bookName); // 还书

boolean changeBookBaseInfo(int bookId, String bookName, String bookAuthor, int bookNum, int bookCategory, int newState); // 修改书籍基本信息
}

IUserDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package cn.egret.dao;

import java.util.List;

import cn.egret.entity.User;

public interface IUserDao {

boolean addUser(User user);

User findUserByNameAndPassword(String userName, String password);

String findPasswordById(int userId);

boolean updatePasswordById(int userId, String newPassword);

List<User> findUser(int role);

User findUserById(int userId);

int getUserIdByName(String userName);

boolean updateUserStateById(int userId, int userState);

boolean deleteUserByIdAndUsername(int userId, String userName);

boolean updateBaseInfo(int userId, int age, String gender, String phone,String address);

}

IBorrowInfoDao.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.egret.dao;

import java.util.List;

import cn.egret.entity.BorrowingInfo;

public interface IBorrowInfoDao {

boolean borrowBook(int userId, int bookId); // 借书

boolean giveBackBook(int userId, int bookId); // 还书

List<BorrowingInfo> findBorrowInfo(int userId); // 查询个人已借阅历史

List<BorrowingInfo> findAllBorrowInfo(); // 查询所有借阅历史
}

IBookCategoryDao.java
1
2
3
4
5
6
7
package cn.egret.dao;

public interface IBookCategoryDao {

String findBookCategoryById(int categoryId);
}

各实现类:

BookDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package cn.egret.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.egret.dao.IBookDao;
import cn.egret.entity.Book;
import cn.egret.util.JdbcUtil;

public class BookDaoImpl implements IBookDao {

@Override
public boolean addBook(Book book) {
String sql = "insert into t_book (book_id,book_name,book_author,book_num,book_category,book_state) values(?,?,?,?,?,?)";

return JdbcUtil.executeUpdate(sql, book.getBookId(), book.getBookName(), book.getBookAuthor(),
book.getBookNum(), book.getBookCategory(), book.getBookState());
}

@Override
public boolean deleteBook(int bookId, String bookName) {
String sql = "delete from t_book where book_id = ? and book_name = ?";
boolean flag = JdbcUtil.executeUpdate(sql, bookId, bookName);
if (flag) {
System.out.println("成功删除了书籍");
} else {
System.out.println("删除书籍失败");
}
return flag;
}

@Override
public List<Book> findBookList() {
String sql = "select * from t_book";
List<Book> listBook = new ArrayList<>();

List<Map<String, Object>> list = JdbcUtil.executeQuery(sql);

for (Map<String, Object> map : list) {
Book book = new Book((int) map.get("book_id"), (String) map.get("book_name"),
(String) map.get("book_author"), (int) map.get("book_num"), (int) map.get("book_category"),
(int) map.get("book_state"));
listBook.add(book);
}
return listBook;
}

@Override
public Book findBook(int bookId, String bookName) {
String sql = "select * from t_book where book_id = ? and book_name = ?";
Book book = null;

List<Map<String, Object>> list = JdbcUtil.executeQuery(sql, bookId, bookName);

for (Map<String, Object> map : list) {
book = new Book((int) map.get("book_id"), (String) map.get("book_name"), (String) map.get("book_author"),
(int) map.get("book_num"), (int) map.get("book_category"), (int) map.get("book_state"));
}
return book;
}

@Override
public boolean changeBookState(int bookId, String bookName, int newState) {
String sql = "update t_book set book_state = ? where book_id = ? and book_name = ?";
boolean flag = JdbcUtil.executeUpdate(sql, newState, bookId, bookName);

if (flag) {
System.out.println("成功修改了书籍状态");
} else {
System.out.println("修改书籍状态失败");
}
return flag;
}

@Override
public boolean borrowBook(int bookId, String bookName) {
String sql = "update t_book set book_num = ? where book_id = ? and book_name = ? and book_state = ?";
Book book = findBook(bookId, bookName);
boolean flag = false;
if (book.getBookNum() > 0) {
flag = JdbcUtil.executeUpdate(sql, book.getBookNum() - 1, bookId, bookName, book.getBookState());
if (book.getBookNum() == 1) {
changeBookState(bookId, bookName, 1);
}
}

if (flag) {
System.out.println("成功借书");
} else {
System.out.println("借书失败");
}
return flag;
}

@Override
public boolean giveBackBook(int bookId, String bookName) {
String sql = "update t_book set book_num = ? where book_id = ? and book_name = ? and book_state = ?";
Book book = findBook(bookId, bookName);
boolean flag = false;
if (book.getBookNum() >= 0) {
flag = JdbcUtil.executeUpdate(sql, book.getBookNum() + 1, bookId, bookName, book.getBookState());
if (book.getBookNum() == 0) {
changeBookState(bookId, bookName, 0);
}
}

if (flag) {
System.out.println("成功还书11");
} else {
System.out.println("还书失败");
}
return flag;
}

@Override
public boolean changeBookBaseInfo(int bookId, String bookName, String bookAuthor, int bookNum, int bookCategory,
int newState) {
String sql = "update t_book set book_name = ?, book_author = ?, book_num = ?, book_category = ?, book_state = ? where book_id = ?";

Book book = findBook(bookId, bookName);

if (bookName.equals("")) {
bookName = book.getBookName();
}

if (bookAuthor.equals("")) {
bookAuthor = book.getBookAuthor();
}

if (bookNum == -1) {
bookNum = book.getBookNum();
}

if (bookCategory == -1) {
bookCategory = book.getBookCategory();
}

if (newState == -1) {
newState = book.getBookState();
}

boolean flag = JdbcUtil.executeUpdate(sql, bookName, bookAuthor, bookNum, bookCategory, newState);

if (flag) {
System.out.println("成功修改了书籍基本信息");
} else {
System.out.println("修改书籍基本信息失败");
}
return flag;
}

}

UserDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package cn.egret.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.egret.dao.IUserDao;
import cn.egret.entity.Book;
import cn.egret.entity.User;
import cn.egret.util.JdbcUtil;

public class UserDaoImpl implements IUserDao {

/**
* 用户添加
*/
@Override
public boolean addUser(User user) {
String sql = "insert into t_user (user_name,password,age,gender,phone,address,user_state,role) values(?,?,?,?,?,?,?,?)";
boolean flag = JdbcUtil.executeUpdate(sql, user.getUserName(), user.getPassWord(), user.getAge(),
user.getGender(), user.getPhone(), user.getAddress(), user.getUserState(), user.getRole());
user.setUserId(getUserIdByName(user.getUserName()));
return flag;
}

@Override
public User findUserByNameAndPassword(String userName, String password) {
String sql = "select * from t_user where user_name = ? and password = ?";
User user = null;
List<Map<String, Object>> list = JdbcUtil.executeQuery(sql, userName, password);

for (Map<String, Object> map : list) {
user = new User((int) map.get("user_id"), (String) map.get("user_name"), (String) map.get("password"),
(int) map.get("age"), (String) map.get("gender"), (String) map.get("phone"),
(String) map.get("address"), (int) map.get("user_state"), (int) map.get("role"));
}
return user;
}

@Override
public String findPasswordById(int userId) {
String sql = "select * from t_user where user_id = ?";
String password = null;
List<Map<String, Object>> list = JdbcUtil.executeQuery(sql, userId);

for (Map<String, Object> map : list) {
password = (String) map.get("password");
}

return password;
}

@Override
public boolean updatePasswordById(int userId, String newPassword) {
String sql = "update t_user set password = ? where user_id = ?";
boolean flag = JdbcUtil.executeUpdate(sql, newPassword, userId);

if (flag) {
System.out.println("成功修改了密码");
} else {
System.out.println("修改密码失败");
}
return flag;
}

@Override
public User findUserById(int userId) {
String sql = "select * from t_user where user_id = ?";
User user = null;

List<Map<String, Object>> list = JdbcUtil.executeQuery(sql, userId);

for (Map<String, Object> map : list) {
user = new User((int) map.get("user_id"), (String) map.get("user_name"), (String) map.get("password"),
(int) map.get("age"), (String) map.get("gender"), (String) map.get("phone"),
(String) map.get("address"), (int) map.get("user_state"), (int) map.get("role"));
}
return user;
}

@Override
public boolean updateUserStateById(int userId, int userState) {
String sql = "update t_user set user_state = ? where user_id = ?";
boolean flag = JdbcUtil.executeUpdate(sql, userState, userId);

if (flag) {
System.out.println("成功修改了用户状态");
} else {
System.out.println("修改用户状态失败");
}
return flag;
}

@Override
public boolean deleteUserByIdAndUsername(int userId, String userName) {
String sql = "delete from t_user where user_id = ? and user_name = ?";
boolean flag = JdbcUtil.executeUpdate(sql, userId, userName);
if (flag) {
System.out.println("成功删除了用户");
} else {
System.out.println("删除用户失败");
}
return flag;
}

@Override
public List<User> findUser(int role) {
String sql = "select * from t_user where role = ?";
List<User> listUser = new ArrayList<>();

List<Map<String, Object>> list = JdbcUtil.executeQuery(sql, role);

for (Map<String, Object> map : list) {
User user = new User((int) map.get("user_id"), (String) map.get("user_name"), (String) map.get("password"),
(int) map.get("age"), (String) map.get("gender"), (String) map.get("phone"),
(String) map.get("address"), (int) map.get("user_state"), (int) map.get("role"));
listUser.add(user);
}
return listUser;
}

@Override
public int getUserIdByName(String userName) {
String sql = "select * from t_user where user_name = ?";
int id = -1;
List<Map<String, Object>> list = JdbcUtil.executeQuery(sql, userName);

for (Map<String, Object> map : list) {
id = (int) map.get("user_id");
}

return id;
}

@Override
public boolean updateBaseInfo(int userId, int age, String gender, String phone, String address) {
String sql = "update t_user set age = ?, gender = ?, phone = ?, address = ? where user_id = ?";
User user = findUserById(userId);

if (age == -1) {
age = user.getAge();
}

if (gender.equals("")) {
gender = user.getGender();
}

if (phone.equals("")) {
phone = user.getPhone();
}

if (address.equals("")) {
address = user.getAddress();
}

boolean flag = JdbcUtil.executeUpdate(sql, age, gender, phone, address, userId);

if (flag) {
System.out.println("修改信息成功");
} else {
System.out.println("修改信息失败");
}

return flag;
}

}

BorrowInfoDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package cn.egret.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cn.egret.dao.IBorrowInfoDao;
import cn.egret.entity.BorrowingInfo;
import cn.egret.util.JdbcUtil;

public class BorrowInfoDaoImpl implements IBorrowInfoDao {

@Override
public boolean borrowBook(int userId, int bookId) {
String sql = "insert into t_borrowing_info(user_id,book_id,borrowing_state,borrowing_time) values(?,?,?,?)";
java.util.Date d = new java.util.Date();

java.sql.Timestamp date = new java.sql.Timestamp(d.getTime());//会丢失时分秒
boolean flag = JdbcUtil.executeUpdate(sql, userId, bookId, 0, date);

if (flag) {
System.out.println("成功借书");
} else {
System.out.println("借书失败");
}
return flag;
}

@Override
public boolean giveBackBook(int userId, int bookId) {
String sql = "update t_borrowing_info set giveback_time = ?, borrowing_state = ? where user_id = ? and book_id = ?";
java.util.Date d = new java.util.Date();

java.sql.Timestamp date = new java.sql.Timestamp(d.getTime());//会丢失时分秒
boolean flag = JdbcUtil.executeUpdate(sql, date, 1, userId, bookId);
if (flag) {
System.out.println("成功还书");
} else {
System.out.println("还书失败");
}
return flag;
}

@Override
public List<BorrowingInfo> findBorrowInfo(int userId) {
String sql = "select * from t_borrowing_info where user_id = ?";

List<BorrowingInfo> BIList = new ArrayList<>();
List<Map<String, Object>> list = JdbcUtil.executeQuery(sql, userId);

for (Map<String, Object> map : list) {
BorrowingInfo BI = new BorrowingInfo((int) map.get("user_id"), (int) map.get("user_id"),
(int) map.get("book_id"), (int) map.get("borrowing_state"),
(java.sql.Timestamp) map.get("borrowing_time"), (java.sql.Timestamp) map.get("giveback_time"));
BIList.add(BI);
}
return BIList;
}

@Override
public List<BorrowingInfo> findAllBorrowInfo() {
String sql = "select * from t_borrowing_info";

List<BorrowingInfo> BIList = new ArrayList<>();
List<Map<String, Object>> list = JdbcUtil.executeQuery(sql);

for (Map<String, Object> map : list) {
BorrowingInfo BI = new BorrowingInfo((int) map.get("user_id"), (int) map.get("user_id"),
(int) map.get("book_id"), (int) map.get("borrowing_state"),
(java.sql.Timestamp) map.get("borrowing_time"), (java.sql.Timestamp) map.get("giveback_time"));
BIList.add(BI);
}
return BIList;
}

}

BookCategoryDaoImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package cn.egret.dao.impl;

import java.util.List;
import java.util.Map;

import cn.egret.dao.IBookCategoryDao;
import cn.egret.util.JdbcUtil;

public class BookCategoryDaoImpl implements IBookCategoryDao {

@Override
public String findBookCategoryById(int categoryId) {
String sql = "select category_name from book_category where category_id = ?";
String categoryName = null;

List<Map<String, Object>> list = JdbcUtil.executeQuery(sql, categoryId);
for (Map<String, Object> map : list) {
categoryName = (String) map.get("category_name");
}
return categoryName;
}
}

七、service包各类的创建

各接口类

IBookService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package cn.egret.service;

import java.util.List;

import cn.egret.entity.Book;

public interface IBookService {
boolean addBook(Book book); // 添加书籍

boolean deleteBook(int bookId, String bookName); // 删除书籍

List<Book> findBookList(); // 书籍列表查询

Book findBook(int bookId, String bookName); // 书籍详细信息查询

boolean changeBookState(int bookId, String bookName, int newState); // 修改书籍状态

boolean borrowBook(int bookId, String bookName); // 借书

boolean giveBackBook(int bookId, String bookName); // 还书

boolean changeBookBaseInfo(int bookId, String bookName, String bookAuthor, int bookNum, int bookCategory, int newState); // 修改书籍基本信息

}

IUserService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package cn.egret.service;

import java.util.List;

import cn.egret.entity.User;

public interface IUserService {
boolean addUser(User user);

User findUserByNameAndPassword(String userName, String password);

String findPasswordById(int userId);

boolean updatePasswordById(int userId, String newPassword);

List<User> findUser(int role);

User findUserById(int userId);

int getUserIdByName(String userName);

boolean updateUserStateById(int userId, int userState);

boolean deleteUserByIdAndUsername(int userId, String userName);

boolean updateBaseInfo(int userId, int age, String gender, String phone,String address);
}

IBorrowInfoService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package cn.egret.service;

import java.util.List;

import cn.egret.entity.BorrowingInfo;

public interface IBorrowInfoService {
boolean borrowBook(int userId, int bookId); // 借书

boolean giveBackBook(int userId, int bookId); // 还书

List<BorrowingInfo> findBorrowInfo(int userId); // 查询个人已借阅历史

List<BorrowingInfo> findAllBorrowInfo(); // 查询所有已借阅历史
}

IBookCategoryService.java
1
2
3
4
5
6
7
8
package cn.egret.service;

public interface IBookCategoryService {

String findBookCategoryById(int categoryId);

}

各实现类

BookServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cn.egret.service.impl;

import java.util.List;

import cn.egret.dao.IBookDao;
import cn.egret.dao.impl.BookDaoImpl;
import cn.egret.entity.Book;
import cn.egret.service.IBookService;

public class BookServiceImpl implements IBookService {

private IBookDao bookDao = new BookDaoImpl();

@Override
public boolean addBook(Book book) {
return bookDao.addBook(book);
}

@Override
public boolean deleteBook(int bookId, String bookName) {
return bookDao.deleteBook(bookId, bookName);
}

@Override
public List<Book> findBookList() {
return bookDao.findBookList();
}

@Override
public Book findBook(int bookId, String bookName) {
return bookDao.findBook(bookId, bookName);
}

@Override
public boolean changeBookState(int bookId, String bookName, int newState) {
return bookDao.changeBookState(bookId, bookName, newState);
}

@Override
public boolean borrowBook(int bookId, String bookName) {
return bookDao.borrowBook(bookId, bookName);
}

@Override
public boolean giveBackBook(int bookId, String bookName) {
return bookDao.giveBackBook(bookId, bookName);
}

@Override
public boolean changeBookBaseInfo(int bookId, String bookName, String bookAuthor, int bookNum, int bookCategory,
int newState) {
return bookDao.changeBookBaseInfo(bookId, bookName, bookAuthor, bookNum, bookCategory, newState);
}

}

UserServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package cn.egret.service.impl;

import java.util.List;

import cn.egret.dao.IUserDao;
import cn.egret.dao.impl.UserDaoImpl;
import cn.egret.entity.User;
import cn.egret.service.IUserService;

public class UserServiceImpl implements IUserService {

private IUserDao userDao= new UserDaoImpl();

@Override
public boolean addUser(User user) {
return userDao.addUser(user);
}

@Override
public User findUserByNameAndPassword(String userName, String password) {
return userDao.findUserByNameAndPassword(userName, password);
}

@Override
public String findPasswordById(int userId) {
return userDao.findPasswordById(userId);
}

@Override
public boolean updatePasswordById(int userId, String newPassword) {
return userDao.updatePasswordById(userId, newPassword);
}

@Override
public List<User> findUser(int role) {
return userDao.findUser(role);
}

@Override
public User findUserById(int userId) {
return userDao.findUserById(userId);
}

@Override
public int getUserIdByName(String userName) {
return userDao.getUserIdByName(userName);
}

@Override
public boolean updateUserStateById(int userId, int userState) {
return userDao.updateUserStateById(userId, userState);
}

@Override
public boolean deleteUserByIdAndUsername(int userId, String userName) {
return userDao.deleteUserByIdAndUsername(userId, userName);
}

@Override
public boolean updateBaseInfo(int userId, int age, String gender, String phone, String address) {
return userDao.updateBaseInfo(userId, age, gender, phone, address);
}

}

BorrowInfoServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package cn.egret.service.impl;

import java.util.List;

import cn.egret.dao.IBorrowInfoDao;
import cn.egret.dao.impl.BorrowInfoDaoImpl;
import cn.egret.entity.BorrowingInfo;
import cn.egret.service.IBorrowInfoService;

public class BorrowInfoServiceImpl implements IBorrowInfoService {

private IBorrowInfoDao BI = new BorrowInfoDaoImpl();

@Override
public boolean borrowBook(int userId, int bookId) {
return BI.borrowBook(userId, bookId);
}

@Override
public boolean giveBackBook(int userId, int bookId) {
return BI.giveBackBook(userId, bookId);
}

@Override
public List<BorrowingInfo> findBorrowInfo(int userId) {
return BI.findBorrowInfo(userId);
}

@Override
public List<BorrowingInfo> findAllBorrowInfo() {
return BI.findAllBorrowInfo();
}

}

BookCategoryServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package cn.egret.service.impl;

import cn.egret.dao.IBookCategoryDao;
import cn.egret.dao.impl.BookCategoryDaoImpl;
import cn.egret.service.IBookCategoryService;

public class BookCategoryServiceImpl implements IBookCategoryService {

private IBookCategoryDao BCD = new BookCategoryDaoImpl();

@Override
public String findBookCategoryById(int categoryId) {
return BCD.findBookCategoryById(categoryId);
}
}


八、controller包各类创建

UserController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package cn.egret.controller;

import java.util.List;

import cn.egret.entity.User;
import cn.egret.service.IUserService;
import cn.egret.service.impl.UserServiceImpl;

public class UserController {

private static IUserService uService = new UserServiceImpl();

/**
* 登录
* @param userName
* @param password
* @return
*/
public static User login(String userName, String password) {
return uService.findUserByNameAndPassword(userName, password);
}

/**
* 注册
* @param user
* @return
*/
public static boolean register(User user) {
return uService.addUser(user);
}

/**
* 禁用普通账号与开启
* @param userId
* @param userState
* @return
*/
public static boolean changeUserState(int userId, int userState) {
return uService.updateUserStateById(userId, userState);
}

/**
* 查询普通用户列表
* @return
*/
public static List<User> findCommonUser(){
return uService.findUser(1);
}

/**
* 查询用户基本信息
* @param id
* @return
*/
public static String findUserInfo(int id) {
return uService.findUserById(id).toString();
}

/**
* 查询用户
* @param id
* @return
*/
public static User findUser(int id) {
return uService.findUserById(id);
}

/**
* 修改密码
* @param userId
* @param newPassword
* @return
*/
public static boolean changePassword(int userId, String newPassword) {
return uService.updatePasswordById(userId, newPassword);
}

/**
* 修改基本信息
* @param userId
* @param age
* @param gender
* @param phone
* @param address
* @return
*/
public static boolean changeBaseInfo(int userId, int age, String gender, String phone, String address) {
return uService.updateBaseInfo(userId, age, gender, phone, address);
}


}

BookController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package cn.egret.controller;

import java.util.List;

import cn.egret.entity.Book;
import cn.egret.entity.BorrowingInfo;
import cn.egret.service.IBookCategoryService;
import cn.egret.service.IBookService;
import cn.egret.service.IBorrowInfoService;
import cn.egret.service.impl.BookCategoryServiceImpl;
import cn.egret.service.impl.BookServiceImpl;
import cn.egret.service.impl.BorrowInfoServiceImpl;

public class BookController {

private static IBookService bService = new BookServiceImpl();
private static IBorrowInfoService bIService = new BorrowInfoServiceImpl();
private static IBookCategoryService BCS = new BookCategoryServiceImpl();

/**
* 添加书籍
*
* @param book
* @return
*/
public static boolean addBook(Book book) {
return bService.addBook(book);
}

/**
* 删除书籍
*
* @param bookId
* @param bookName
* @return
*/
public static boolean deleteBook(int bookId, String bookName) {
return bService.deleteBook(bookId, bookName);
}

/**
* 书籍基本信息修改
*
* @param bookId
* @param bookName
* @param bookAuthor
* @param bookNum
* @param bookCategory
* @param newState
* @return
*/
public static boolean changeBookBaseInfo(int bookId, String bookName, String bookAuthor, int bookNum,
int bookCategory, int newState) {
return bService.changeBookBaseInfo(bookId, bookName, bookAuthor, bookNum, bookCategory, newState);
}

/**
* 书籍列表查询
*
* @return
*/
public static List<Book> findBookList() {
return bService.findBookList();
}

/**
* 书籍详细信息查询
*
* @param bookId
* @param bookName
* @return
*/
public static Book findBook(int bookId, String bookName) {
return bService.findBook(bookId, bookName);
}

/**
* 书籍借阅
*
* @param userId
* @param bookId
* @param bookName
* @return
*/
public static boolean borrowBook(int userId, int bookId, String bookName) {
return bService.borrowBook(bookId, bookName) && bIService.borrowBook(userId, bookId);
}

/**
* 书籍归还
*
* @param userId
* @param bookId
* @param bookName
* @return
*/
public static boolean giveBackBook(int userId, int bookId, String bookName) {
return bService.giveBackBook(bookId, bookName) && bIService.giveBackBook(userId, bookId);
}

/**
* 查询用户借阅信息
* @param userId
* @return
*/
public static List<BorrowingInfo> findUserBorrowInfoList(int userId){
return bIService.findBorrowInfo(userId);
}

public static List<BorrowingInfo> findAllBorrowInfoList(){
return bIService.findAllBorrowInfo();
}

/**
* 查询书籍类别
* @param id
* @return
*/
public static String findBookCategory(int id) {
return BCS.findBookCategoryById(id);
}

}

九、page包各类创建

各page页面均是利用ecplise的WindowsBuilder插件生成的,可自己从网上下载使用。

MainFrame.java

此类为整个应用程序的入口

1
2
3
4
5
6
7
8
9
10
11
12
13
package cn.egret.controller.page;

public class MainFrame {

public MainFrame() {
new LoginPage();
}

public static void main(String[] args) {
new MainFrame();
}
}

LoginPage.java

登录界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package cn.egret.controller.page;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import cn.egret.controller.UserController;
import cn.egret.entity.User;

import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JPasswordField;
import java.awt.Font;

public class LoginPage extends JFrame {

private JFrame frame;
private JTextField userNameField;
private JPasswordField passwordField;
private User user;
private AdminPage adminPage;
private RegisterPage registerPage;
private CommonUserPage commonUserPage;

/**
* Create the application.
*/
public LoginPage() {
initialize();
frame.setVisible(true);
}

/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setTitle("图书管理系统");
frame.setBounds(100, 100, 641, 453);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);// 窗口在屏幕中间显示
frame.getContentPane().setLayout(null);

JPanel panel = new JPanel();
panel.setBounds(0, 0, 623, 406);
frame.getContentPane().add(panel);
panel.setLayout(null);

JLabel userNameLabel = new JLabel("用 户 名:");
userNameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
userNameLabel.setBounds(152, 98, 146, 39);
panel.add(userNameLabel);

JLabel passWordLabel = new JLabel("密 码:");
passWordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
passWordLabel.setBounds(152, 190, 125, 39);
panel.add(passWordLabel);

userNameField = new JTextField();
userNameField.setBounds(325, 105, 134, 29);
panel.add(userNameField);
userNameField.setColumns(10);

passwordField = new JPasswordField();
passwordField.setBounds(325, 197, 134, 29);
panel.add(passwordField);

JButton loginButton = new JButton("登 录");
loginButton.setFont(new Font("宋体", Font.PLAIN, 20));
loginButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {

// 获取文本框中的值
String userName = userNameField.getText();
String password = new String(passwordField.getPassword());
if (userName == null || userName.trim().equals("") || password.trim().equals("")) {
JOptionPane.showMessageDialog(loginButton, "用户名或密码不能为空", "提示", 1);
} else {
user = UserController.login(userName, password);
System.out.println(userName);
System.out.println(password);
if (user != null) {
if (user.getUserState() == 1) {
JOptionPane.showMessageDialog(loginButton, "当前账号已被禁用,请联系管理员", "提示", 1);
} else {
if (user.getRole() == 0) {// 打开新的页面 - 管理员界面
adminPage = new AdminPage("管理员界面", user);

frame.setVisible(false);
// // 将原本的⻚⾯关闭
dispose();
System.out.println("登录成功");
} else if (user.getRole() == 1) {
// 打开新的界面- 普通用户界面CustomerPage
commonUserPage = new CommonUserPage("普通用户界面", user);
frame.setVisible(false);
// customerPage.setVisible(true);
// 将原本的⻚⾯关闭
dispose();
}
}
} else if (user == null) {

JOptionPane.showMessageDialog(loginButton, "用户名或密码错误", "错误", 0);
}
}

}
});
loginButton.setBounds(122, 295, 140, 51);
panel.add(loginButton);

JButton registerButton = new JButton("注 册");
registerButton.setFont(new Font("宋体", Font.PLAIN, 20));
registerButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
RegisterPage registerPage = new RegisterPage("注册页面");
frame.setVisible(false);
dispose();
}
});
registerButton.setBounds(355, 295, 140, 51);
panel.add(registerButton);

}
}

RegisterPage.java

注册界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package cn.egret.controller.page;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JTextField;

import cn.egret.controller.UserController;
import cn.egret.entity.User;

import javax.swing.JComboBox;
import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JRadioButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class RegisterPage extends JFrame{

private JFrame frame;
private JTextField userNameField;
private JPasswordField passwordField1;
private JPasswordField passwordField2;
private JTextField ageField;
private JTextField phoneField;
private JTextField addressField;

/**
* Create the application.
*/
public RegisterPage(String name) {
initialize(name);
frame.setVisible(true);
}

/**
* Initialize the contents of the frame.
*/
private void initialize(String name) {
frame = new JFrame(name);
frame.setBounds(100, 100, 503, 618);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);// 窗口在屏幕中间显示
frame.getContentPane().setLayout(null);

JPanel panel = new JPanel();
panel.setBounds(0, 0, 484, 571);
frame.getContentPane().add(panel);
panel.setLayout(null);

JLabel userNameLabel = new JLabel("用户名:");
userNameLabel.setBounds(100, 35, 71, 39);
panel.add(userNameLabel);

JLabel passWordLabel = new JLabel("密 码:");
passWordLabel.setBounds(100, 98, 72, 18);
panel.add(passWordLabel);

JLabel passWordLabel1 = new JLabel("重复密码:");
passWordLabel1.setBounds(94, 148, 84, 18);
panel.add(passWordLabel1);

JLabel ageLabel = new JLabel("年 龄:");
ageLabel.setBounds(100, 200, 72, 18);
panel.add(ageLabel);

JLabel genderLabel = new JLabel("性 别:");
genderLabel.setBounds(100, 257, 72, 18);
panel.add(genderLabel);

JLabel phoneLabel = new JLabel("手机号:");
phoneLabel.setBounds(100, 312, 72, 18);
panel.add(phoneLabel);

JLabel addressLabel = new JLabel("地 址:");
addressLabel.setBounds(100, 367, 72, 18);
panel.add(addressLabel);

JLabel roleLabel = new JLabel("用户类别:");
roleLabel.setBounds(88, 423, 90, 18);
panel.add(roleLabel);



userNameField = new JTextField();
userNameField.setBounds(196, 42, 158, 24);
panel.add(userNameField);
userNameField.setColumns(10);

passwordField1 = new JPasswordField();
passwordField1.setBounds(196, 95, 158, 24);
panel.add(passwordField1);

passwordField2 = new JPasswordField();
passwordField2.setBounds(196, 145, 158, 24);
panel.add(passwordField2);

ageField = new JTextField();
ageField.setText("18");
ageField.setColumns(10);
ageField.setBounds(196, 197, 158, 24);
panel.add(ageField);

phoneField = new JTextField();
phoneField.setColumns(10);
phoneField.setBounds(196, 309, 158, 24);
panel.add(phoneField);

addressField = new JTextField();
addressField.setColumns(10);
addressField.setBounds(196, 364, 158, 24);
panel.add(addressField);

JComboBox comboBox = new JComboBox();
comboBox.setModel(new DefaultComboBoxModel(new String[] {"管理员", "普通用户"}));
comboBox.setSelectedIndex(1);
comboBox.setBounds(196, 420, 158, 24);
panel.add(comboBox);

JRadioButton genderButton1 = new JRadioButton("男");
genderButton1.setBounds(196, 253, 58, 27);
genderButton1.setSelected(true);

JRadioButton genderButton2 = new JRadioButton("女");
genderButton2.setBounds(271, 253, 58, 27);

ButtonGroup btnGroup = new ButtonGroup();
btnGroup.add(genderButton1);
btnGroup.add(genderButton2);

panel.add(genderButton1);
panel.add(genderButton2);

JButton button1 = new JButton("确认注册");
button1.setFont(new Font("宋体", Font.PLAIN, 18));
button1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {

String userName = userNameField.getText();

String passWord1 = new String(passwordField1.getPassword());
String passWord2 = new String(passwordField2.getPassword());

int age = Integer.parseInt(ageField.getText());

String gender = null;
if(genderButton1.isSelected()) {
gender = "男";
}else if(genderButton2.isSelected()) {
gender = "女";
}

String phone = phoneField.getText();
String address = addressField.getText();

int role = comboBox.getSelectedIndex();

// System.out.println(userName);
// System.out.println(passWord1);
// System.out.println(passWord2);
// System.out.println(age);
// System.out.println(gender);
// System.out.println(phone);
// System.out.println(address);
// System.out.println(role);

if(userName.equals("") || passWord1.equals("") || passWord2.equals("") || phone.equals("") || address.equals("")) {
System.out.println("输入数据中有空数据,非法,请重新输入!");
JOptionPane.showMessageDialog(button1, "输入数据中有空数据,非法,请重新输入!", "提示", 1);
userNameField.setText("");
passwordField1.setText("");
passwordField2.setText("");
phoneField.setText("");
addressField.setText("");
}else if(!passWord1.equals(passWord2)) {
System.out.println("两次输入的密码不一致,请重新输入!");
JOptionPane.showMessageDialog(button1, "两次输入的密码不一致,请重新输入!", "提示", 1);
userNameField.setText("");
passwordField1.setText("");
passwordField2.setText("");
phoneField.setText("");
addressField.setText("");
}else {
User user = new User(userName,passWord1,age,gender,phone,address,0,role);
boolean flag = UserController.register(user);
System.out.println(flag);
if(flag) {
JOptionPane.showMessageDialog(button1, "恭喜您注册成功!", "提示", 1);
new LoginPage();
frame.setVisible(false);
dispose();
}else {
JOptionPane.showMessageDialog(button1, "注册失败!", "提示", 1);
userNameField.setText("");
passwordField1.setText("");
passwordField2.setText("");
phoneField.setText("");
addressField.setText("");
}

}

}
});
button1.setBounds(88, 492, 121, 39);
panel.add(button1);

JButton button2 = new JButton("取 消");
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
}
});
button2.setFont(new Font("宋体", Font.PLAIN, 18));
button2.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new LoginPage();
frame.setVisible(false);
dispose();
}
});
button2.setBounds(251, 492, 121, 39);
panel.add(button2);
}
}

AdminPage.java

管理员界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package cn.egret.controller.page;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultTreeModel;

import cn.egret.controller.UserController;
import cn.egret.controller.page.smallPage.admin.AddAdmin;
import cn.egret.controller.page.smallPage.admin.AddAndDeleteBook;
import cn.egret.controller.page.smallPage.admin.ChangeUserState;
import cn.egret.controller.page.smallPage.admin.SearchBookList;
import cn.egret.controller.page.smallPage.admin.SearchUserList;
import cn.egret.controller.page.smallPage.admin.UserInfoSearch;
import cn.egret.controller.page.smallPage.admin.WelcomePage;
import cn.egret.controller.page.smallPage.common.UserInfoSearchPage;
import cn.egret.entity.User;

import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.CardLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AdminPage extends JFrame {

private JFrame frame;
private CardLayout cl;
JPanel panel_2;
private User user;

WelcomePage welcomePage;
SearchUserList searchUserList;
SearchBookList searchBookList;
AddAdmin addAdmin;
AddAndDeleteBook addAndDeleteBook;
UserInfoSearch userInfoSearch;
ChangeUserState changeUserState;

/**
* Create the application.
*/
public AdminPage(String name, User user) {
super(name);
this.user = user;
initialize(name);
frame.setVisible(true);
}

/**
* Initialize the contents of the frame.
*/
private void initialize(String name) {
frame = new JFrame(name);
frame.setBounds(100, 100, 1076, 661);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);// 窗口在屏幕中间显示
frame.getContentPane().setLayout(null);

JPanel panel = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
panel.setBounds(0, 0, 1058, 38);
frame.getContentPane().add(panel);

JLabel upLabel1 = new JLabel("欢迎" + user.getUserName() + "登录");
panel.add(upLabel1);

JLabel upLabel2 = new JLabel(" | ");
panel.add(upLabel2);

JLabel upLabel3 = new JLabel("退出登录");
upLabel3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new LoginPage();
frame.setVisible(false);
dispose();
}
});
panel.add(upLabel3);

JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 35, 184, 579);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);

JTree tree = new JTree();
tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(" ") {
{
DefaultMutableTreeNode node_1;
node_1 = new DefaultMutableTreeNode("用户管理");
node_1.add(new DefaultMutableTreeNode("管理员添加"));
node_1.add(new DefaultMutableTreeNode("用户列表查询"));
node_1.add(new DefaultMutableTreeNode("禁用与启用用户"));
add(node_1);
node_1 = new DefaultMutableTreeNode("书籍管理");
node_1.add(new DefaultMutableTreeNode("书籍列表查询"));
node_1.add(new DefaultMutableTreeNode("书籍添加与删除"));
add(node_1);
add(new DefaultMutableTreeNode("个人信息"));
}
}));
tree.addTreeSelectionListener(new TreeSelectionListener() {

@Override
public void valueChanged(TreeSelectionEvent e) {
String name = ((DefaultMutableTreeNode) tree.getLastSelectedPathComponent()).toString();
user = UserController.login(user.getUserName(), user.getPassWord());
if (name.equals("管理员添加")) {
cl.show(panel_2, "addAdmin");
} else if (name.equals("用户列表查询")) {
cl.show(panel_2, "searchUserList");
} else if (name.equals("禁用与启用用户")) {
cl.show(panel_2, "changeUserState");
} else if (name.equals("书籍列表查询")) {
cl.show(panel_2, "searchBookList");
} else if (name.equals("书籍添加与删除")) {
cl.show(panel_2, "addAndDeleteBook");
} else if (name.equals("个人信息")) {
panel_2.remove(userInfoSearch);
userInfoSearch = new UserInfoSearch(user);
panel_2.add(userInfoSearch, "userInfoSearch");
cl.show(panel_2, "userInfoSearch");
}

}
});
tree.setBounds(14, 13, 156, 553);
panel_1.add(tree);

panel_2 = new JPanel();
panel_2.setBounds(183, 35, 875, 579);
frame.getContentPane().add(panel_2);
panel_2.setLayout(new CardLayout(0, 0));

welcomePage = new WelcomePage(user.getUserName()); // 欢迎页面
searchUserList = new SearchUserList(); // 查询用户列表
searchBookList = new SearchBookList(); // 查询书籍列表
addAdmin = new AddAdmin(); // 添加管理员
addAndDeleteBook = new AddAndDeleteBook(); // 增加删除书籍
userInfoSearch = new UserInfoSearch(user); // 用户详细信息
changeUserState = new ChangeUserState(user); // 禁用与启用用户

panel_2.add(welcomePage, "welcomePage");
panel_2.add(searchUserList, "searchUserList");
panel_2.add(searchBookList, "searchBookList");
panel_2.add(addAdmin, "addAdmin");
panel_2.add(addAndDeleteBook, "addAndDeleteBook");
panel_2.add(userInfoSearch, "userInfoSearch");
panel_2.add(changeUserState, "changeUserState");

cl = (CardLayout) (panel_2.getLayout());
cl.show(panel_2, "welcomePage");

}
}

CommonUserPage.java

普通用户界面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package cn.egret.controller.page;

import java.awt.CardLayout;
import java.awt.FlowLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;

import cn.egret.controller.UserController;
import cn.egret.controller.page.smallPage.common.BorrowAndGiveBackPage;
import cn.egret.controller.page.smallPage.common.SearchBookListPage;
import cn.egret.controller.page.smallPage.common.SearchUserBorrowInfoPage;
import cn.egret.controller.page.smallPage.common.UpdataPasswordPage;
import cn.egret.controller.page.smallPage.common.UpdateBaseInfoPage;
import cn.egret.controller.page.smallPage.common.UserInfoSearchPage;
import cn.egret.controller.page.smallPage.common.WelcomePage;
import cn.egret.entity.User;

public class CommonUserPage extends JFrame {

private JFrame frame;
private CardLayout cl;
JPanel panel_2;
private User user;

WelcomePage welcomePage; // 欢迎页面
SearchBookListPage searchBookList; // 查询书籍列表
UserInfoSearchPage userInfoSearch; // 个人信息查询
BorrowAndGiveBackPage borrowAndGiveBackPage; // 借还书页面
SearchUserBorrowInfoPage searchUserBorrowInfoPage; // 查询用户借阅信息
UpdataPasswordPage updatePasswordPage; // 修改密码
UpdateBaseInfoPage updateBaseInfoPage; // 修改基本信息

/**
* Create the application.
*/
public CommonUserPage(String name, User user) {
super(name);
this.user = user;
initialize(name);
frame.setVisible(true);
}

/**
* Initialize the contents of the frame.
*/
private void initialize(String name) {
frame = new JFrame(name);
frame.setBounds(100, 100, 1076, 661);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLocationRelativeTo(null);// 窗口在屏幕中间显示
frame.getContentPane().setLayout(null);

JPanel panel = new JPanel();
FlowLayout flowLayout = (FlowLayout) panel.getLayout();
flowLayout.setAlignment(FlowLayout.RIGHT);
panel.setBounds(0, 0, 1058, 38);
frame.getContentPane().add(panel);

JLabel upLabel1 = new JLabel("欢迎" + user.getUserName() + "登录");
panel.add(upLabel1);

JLabel upLabel2 = new JLabel(" | ");
panel.add(upLabel2);

JLabel upLabel3 = new JLabel("退出登录");
upLabel3.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
new LoginPage();
frame.setVisible(false);
dispose();
}
});

JLabel updatePWLabel = new JLabel("密码修改");
updatePWLabel.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
user = UserController.login(user.getUserName(), user.getPassWord());
panel_2.remove(updatePasswordPage);
updatePasswordPage = new UpdataPasswordPage(user);
panel_2.add(updatePasswordPage, "updatePasswordPage");
cl.show(panel_2, "updatePasswordPage");
}
});
panel.add(updatePWLabel);

JLabel upLabel2_1 = new JLabel(" | ");
panel.add(upLabel2_1);

JLabel updateBILable = new JLabel("个人基础信息修改");
updateBILable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
user = UserController.login(user.getUserName(), user.getPassWord());
panel_2.remove(updateBaseInfoPage);
updateBaseInfoPage = new UpdateBaseInfoPage(user);
panel_2.add(updateBaseInfoPage, "updateBaseInfoPage");
cl.show(panel_2, "updateBaseInfoPage");
}
});
panel.add(updateBILable);

JLabel upLabel2_2 = new JLabel(" | ");
panel.add(upLabel2_2);
panel.add(upLabel3);

JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 35, 184, 579);
frame.getContentPane().add(panel_1);
panel_1.setLayout(null);

JTree tree = new JTree();
tree.setModel(new DefaultTreeModel(new DefaultMutableTreeNode(" ") {
{
DefaultMutableTreeNode node_1;
node_1 = new DefaultMutableTreeNode("书籍管理");
node_1.add(new DefaultMutableTreeNode("书籍列表查询"));
add(node_1);
node_1 = new DefaultMutableTreeNode("借阅管理");
node_1.add(new DefaultMutableTreeNode("借阅与归还书籍"));
node_1.add(new DefaultMutableTreeNode("个人借阅历史查看"));
add(node_1);
add(new DefaultMutableTreeNode("个人信息"));
}
}));
tree.addTreeSelectionListener(new TreeSelectionListener() {

@Override
public void valueChanged(TreeSelectionEvent e) {
String name = ((DefaultMutableTreeNode) tree.getLastSelectedPathComponent()).toString();
user = UserController.login(user.getUserName(), user.getPassWord());
if (name.equals("书籍列表查询")) {
cl.show(panel_2, "searchBookList");
} else if (name.equals("借阅与归还书籍")) {
panel_2.remove(borrowAndGiveBackPage);
borrowAndGiveBackPage = new BorrowAndGiveBackPage(user);
panel_2.add(borrowAndGiveBackPage, "borrowAndGiveBackPage");
cl.show(panel_2, "borrowAndGiveBackPage");
} else if (name.equals("个人借阅历史查看")) {
cl.show(panel_2, "searchUserBorrowInfoPage");
} else if (name.equals("个人信息")) {
panel_2.remove(userInfoSearch);
userInfoSearch = new UserInfoSearchPage(user);
panel_2.add(userInfoSearch, "userInfoSearch");
cl.show(panel_2, "userInfoSearch");
}

}
});
tree.setBounds(14, 13, 156, 553);
panel_1.add(tree);

panel_2 = new JPanel();
panel_2.setBounds(183, 35, 875, 579);
frame.getContentPane().add(panel_2);
panel_2.setLayout(new CardLayout(0, 0));

welcomePage = new WelcomePage(user.getUserName()); // 欢迎页面
// SearchUserList searchUserList = new SearchUserList();
searchBookList = new SearchBookListPage(); // 查询书籍列表
// AddAdmin addAdmin = new AddAdmin();
// AddAndDeleteBook addAndDeleteBook = new AddAndDeleteBook();
userInfoSearch = new UserInfoSearchPage(user); // 个人信息查询
borrowAndGiveBackPage = new BorrowAndGiveBackPage(user); // 借还书页面
searchUserBorrowInfoPage = new SearchUserBorrowInfoPage(); // 查询用户借阅信息
updatePasswordPage = new UpdataPasswordPage(user); // 修改密码
updateBaseInfoPage = new UpdateBaseInfoPage(user); // 修改基本信息

panel_2.add(welcomePage, "welcomePage");
// panel_2.add(searchUserList, "searchUserList");
panel_2.add(searchBookList, "searchBookList");
// panel_2.add(addAdmin, "addAdmin");
// panel_2.add(addAndDeleteBook, "addAndDeleteBook");
panel_2.add(userInfoSearch, "userInfoSearch");
panel_2.add(borrowAndGiveBackPage, "borrowAndGiveBackPage");
panel_2.add(searchUserBorrowInfoPage, "searchUserBorrowInfoPage");
panel_2.add(updatePasswordPage, "updatePasswordPage");
panel_2.add(updateBaseInfoPage, "UpdateBaseInfoPage");

cl = (CardLayout) (panel_2.getLayout());
cl.show(panel_2, "welcomePage");
}

}

smallPage.admin包内各面板的创建

WelcomePage.java

欢迎面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.egret.controller.page.smallPage.admin;

import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class WelcomePage extends JPanel {

/**
* Create the application.
*/
public WelcomePage(String name) {
setLayout(null);
setSize(1000, 400);

JLabel label = new JLabel("尊敬的管理员 " +name +" 您好,欢迎来到小波图书管理系统");
label.setFont(new Font("宋体", Font.PLAIN, 31));
label.setBounds(35, 147, 888, 125);
add(label);
}

}

AddAdmin.java

添加管理员面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package cn.egret.controller.page.smallPage.admin;

import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import cn.egret.controller.UserController;
import cn.egret.entity.User;

import javax.swing.JButton;
import javax.swing.JPasswordField;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AddAdmin extends JPanel {
private JTextField userNameField;
private JPasswordField passwordField;

public AddAdmin() {
setLayout(null);
setSize(1000, 400);

JLabel userNameLabel = new JLabel("用户名:");
userNameLabel.setBounds(143, 57, 72, 18);
add(userNameLabel);

JLabel passwordLabel = new JLabel("密 码:");
passwordLabel.setBounds(143, 114, 72, 18);
add(passwordLabel);

userNameField = new JTextField();
userNameField.setBounds(259, 54, 132, 24);
add(userNameField);
userNameField.setColumns(10);

passwordField = new JPasswordField();
passwordField.setBounds(259, 111, 132, 24);
add(passwordField);

JButton button = new JButton("添 加");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String userName = userNameField.getText();
String password = new String(passwordField.getPassword());

if (userName.equals("") || userName == null || password.equals("") || password == null) {
System.out.println("数据未填写完整,请重新输入!");
JOptionPane.showMessageDialog(button, "数据未填写完整,请重新输入!", "提示", 1);
} else {
User us = new User(userName, password, -1, "未知", "未知", "未知", 0, 0);
boolean flag = UserController.register(us);
if (flag) {
JOptionPane.showMessageDialog(button, "添加管理员成功", "提示", 1);

} else {
JOptionPane.showMessageDialog(button, "添加管理员失败", "提示", 1);
}
}
userNameField.setText("");
passwordField.setText("");
}
});
button.setBounds(221, 215, 113, 27);
add(button);
}
}

AddAndDeleteBook.java

添加和删除书籍面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package cn.egret.controller.page.smallPage.admin;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import cn.egret.controller.BookController;
import cn.egret.entity.Book;

import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class AddAndDeleteBook extends JPanel {
private JTextField bookIdField1;
private JTextField bookNameField1;
private JTextField authorField;
private JTextField bookNumField;
private JTextField bookIdField2;
private JTextField bookNameField2;

/**
* Create the application.
*/
public AddAndDeleteBook() {
setLayout(null);

JPanel panel = new JPanel();
panel.setBounds(0, 0, 966, 626);
add(panel);
panel.setLayout(null);

JLabel deleteLabel = new JLabel("添 书");
deleteLabel.setBounds(138, 25, 72, 18);
panel.add(deleteLabel);

JLabel addBookLabel = new JLabel("删 书");
addBookLabel.setBounds(446, 116, 72, 18);
panel.add(addBookLabel);

JLabel bookIdLabel = new JLabel("书籍编号:");
bookIdLabel.setBounds(77, 73, 102, 18);
panel.add(bookIdLabel);

JLabel bookIdLabel1 = new JLabel("书籍编号:");
bookIdLabel1.setBounds(394, 198, 96, 18);
panel.add(bookIdLabel1);

JLabel bookNameLabel = new JLabel("书 名:");
bookNameLabel.setBounds(88, 132, 72, 18);
panel.add(bookNameLabel);

JLabel bookNameLabel1 = new JLabel("书 名:");
bookNameLabel1.setBounds(403, 285, 72, 18);
panel.add(bookNameLabel1);

JLabel bookAuthorLabel = new JLabel("作 者:");
bookAuthorLabel.setBounds(88, 198, 72, 18);
panel.add(bookAuthorLabel);

JLabel bookCategoryLabel = new JLabel("类 别:");
bookCategoryLabel.setBounds(88, 335, 72, 18);
panel.add(bookCategoryLabel);

JLabel bookNumLabel = new JLabel("数 量:");
bookNumLabel.setBounds(88, 268, 72, 18);
panel.add(bookNumLabel);

bookIdField1 = new JTextField();
bookIdField1.setBounds(193, 70, 86, 24);
panel.add(bookIdField1);
bookIdField1.setColumns(10);

bookIdField2 = new JTextField();
bookIdField2.setColumns(10);
bookIdField2.setBounds(504, 195, 86, 24);
panel.add(bookIdField2);

bookNameField1 = new JTextField();
bookNameField1.setBounds(193, 129, 86, 24);
panel.add(bookNameField1);
bookNameField1.setColumns(10);

bookNameField2 = new JTextField();
bookNameField2.setColumns(10);
bookNameField2.setBounds(504, 282, 86, 24);
panel.add(bookNameField2);

authorField = new JTextField();
authorField.setBounds(193, 195, 86, 24);
panel.add(authorField);
authorField.setColumns(10);

bookNumField = new JTextField();
bookNumField.setBounds(193, 265, 86, 24);
panel.add(bookNumField);
bookNumField.setColumns(10);

JComboBox bookCategoryComboBox = new JComboBox();
bookCategoryComboBox.setModel(new DefaultComboBoxModel(new String[] { "政治", "计算机科学", "文学" }));
bookCategoryComboBox.setBounds(193, 332, 86, 24);
panel.add(bookCategoryComboBox);

JButton addButton = new JButton("增加书籍");
addButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bookId = bookIdField1.getText();
String bookName = bookNameField1.getText();
String author = authorField.getText();
String bookNum = bookNumField.getText();

if (bookId.equals("") || bookId == null || bookName.equals("") || bookName == null || author.equals("")
|| author == null || bookNum.equals("") || bookNum == null) {
System.out.println("未完全输入数据,添加失败!");
JOptionPane.showMessageDialog(addButton, "未完全输入数据,添加失败!", "提示", 1);
} else {
int bookIdNum = Integer.parseInt(bookId);
int bookNumn = Integer.parseInt(bookNum);
int bookCategory = bookCategoryComboBox.getSelectedIndex() + 1;
Book bo = new Book(bookIdNum,bookName,author,bookNumn,bookCategory,0);
boolean flag = BookController.addBook(bo);
if(flag) {
System.out.println("添加书籍成功");
JOptionPane.showMessageDialog(addButton, "添加书籍成功", "提示", 1);

}
else {
System.out.println("添加书籍失败");
JOptionPane.showMessageDialog(addButton, "添加书籍失败", "提示", 1);
}
}
bookIdField1.setText("");
bookNameField1.setText("");
authorField.setText("");
bookNumField.setText("");
}
});
addButton.setBounds(109, 439, 113, 27);
panel.add(addButton);

JButton deleteButton = new JButton("删除书籍");
deleteButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bookId = bookIdField2.getText();
String bookName = bookNameField2.getText();
if(bookId.equals("") || bookId == null || bookName.equals("") || bookName == null) {
System.out.println("未完全输入数据,添加失败!");
JOptionPane.showMessageDialog(deleteButton, "未完全输入数据,添加失败!", "提示", 1);
}else {
int bookIdNum = Integer.parseInt(bookId);
boolean flag = BookController.deleteBook(bookIdNum, bookName);
if(flag) {
System.out.println("删除书籍成功");
JOptionPane.showMessageDialog(deleteButton, "删除书籍成功", "提示", 1);

}
else {
System.out.println("删除失败");
JOptionPane.showMessageDialog(deleteButton, "删除失败", "提示", 1);
}
}
bookIdField2.setText("");
bookNameField2.setText("");
}
});
deleteButton.setBounds(634, 238, 113, 27);
panel.add(deleteButton);
}

}

ChangeUserState.java

更改用户状态面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package cn.egret.controller.page.smallPage.admin;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTextField;

import cn.egret.controller.UserController;
import cn.egret.entity.User;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class ChangeUserState extends JPanel {
private JTextField userIdField;
private JTextField userNameField;

public ChangeUserState(User user) {
setLayout(null);

JLabel userIdLabel = new JLabel("用户ID:");
userIdLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userIdLabel.setBounds(141, 100, 123, 37);
add(userIdLabel);

JLabel userNameLabel = new JLabel("用户名:");
userNameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userNameLabel.setBounds(141, 207, 102, 37);
add(userNameLabel);

JButton stopButton = new JButton("禁 用");
stopButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String userIdS = userIdField.getText();
String userName = userNameField.getText();

if (userIdS.equals("") || userIdS == null || userName.equals("") || userName == null) {
System.out.println("未完全填写数据,请重新输入!");
JOptionPane.showMessageDialog(stopButton, "未完全填写数据,请重新输入!!", "提示", 1);
} else {
int userId = Integer.parseInt(userIdS);
boolean flag = UserController.changeUserState(userId, 1);
if (flag) {
System.out.println("禁用账号成功!");
JOptionPane.showMessageDialog(stopButton, "禁用账号成功!", "提示", 1);
} else {
System.out.println("禁用账号失败!");
JOptionPane.showMessageDialog(stopButton, "禁用账号失败!", "提示", 1);
}
}
userIdField.setText("");
userNameField.setText("");
}
});
stopButton.setFont(new Font("宋体", Font.PLAIN, 20));
stopButton.setBounds(106, 321, 122, 44);
add(stopButton);

JButton startButton = new JButton("启 用");
startButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String userIdS = userIdField.getText();
String userName = userNameField.getText();

if (userIdS.equals("") || userIdS == null || userName.equals("") || userName == null) {
System.out.println("未完全填写数据,请重新输入!");
JOptionPane.showMessageDialog(startButton, "未完全填写数据,请重新输入!", "提示", 1);
} else {
int userId = Integer.parseInt(userIdS);
boolean flag = UserController.changeUserState(userId, 0);
if (flag) {
System.out.println("启用账号成功!");
JOptionPane.showMessageDialog(startButton, "启用账号成功!", "提示", 1);
} else {
System.out.println("启用账号失败!");
JOptionPane.showMessageDialog(startButton, "启用账号失败!", "提示", 1);
}
}
userIdField.setText("");
userNameField.setText("");
}
});
startButton.setFont(new Font("宋体", Font.PLAIN, 20));
startButton.setBounds(322, 321, 123, 44);
add(startButton);

userIdField = new JTextField();
userIdField.setFont(new Font("宋体", Font.PLAIN, 25));
userIdField.setBounds(278, 102, 136, 31);
add(userIdField);
userIdField.setColumns(10);

userNameField = new JTextField();
userNameField.setFont(new Font("宋体", Font.PLAIN, 25));
userNameField.setBounds(278, 207, 136, 37);
add(userNameField);
userNameField.setColumns(10);

}
}

SearchBookList.java

查询书籍列表面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package cn.egret.controller.page.smallPage.admin;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import cn.egret.controller.BookController;
import cn.egret.entity.Book;

public class SearchBookList extends JPanel{

private JTextField bookIdField;
private Object[] columnNames = { "书籍编号", "书名", "作者", "剩余数量", "类别", "是否可借阅"};
private Object[][] rowData;
JScrollPane scrollpane;
JTable table;
private JTextField bookNameField;

/**
* Create the application.
*/
public SearchBookList() {

setLayout(null);
setSize(881, 586);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 876, 62);
add(panel);
panel.setLayout(null);

bookIdField = new JTextField();
bookIdField.setBounds(320, 25, 86, 24);
panel.add(bookIdField);
bookIdField.setColumns(10);

JLabel bookIdLabel = new JLabel("书籍编号:");
bookIdLabel.setBounds(236, 28, 92, 18);
panel.add(bookIdLabel);

JButton SearchbButton = new JButton("搜 索");
SearchbButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bookIdString = bookIdField.getText();
String bookNameString = bookNameField.getText();
if(bookIdString.equals("") || bookIdString == null || bookNameString.equals("") || bookNameString == null) {
System.out.println("查询内容未填写完整!");
JOptionPane.showMessageDialog(SearchbButton, "查询内容未填写完整!", "提示", 1);
}else {
int bookId = Integer.parseInt(bookIdString);
String bookName = bookNameString;

Book bo = BookController.findBook(bookId, bookName);
if(bo != null) {
rowData = new Object[1][6];
rowData[0][0] = bo.getBookId();
rowData[0][1] = bo.getBookName();
rowData[0][2] = bo.getBookAuthor();
rowData[0][3] = bo.getBookNum();

rowData[0][4] = BookController.findBookCategory(bo.getBookCategory());
if(bo.getBookState() == 0) {
rowData[0][5] = "可借阅";
}else {
rowData[0][5] = "不可借阅";
}

table.setModel(new DefaultTableModel(rowData, columnNames));
}else {
System.out.println("没有此书!");
JOptionPane.showMessageDialog(SearchbButton, "没有此书!", "提示", 1);
}
}
}
});

SearchbButton.setBounds(663, 24, 113, 27);
panel.add(SearchbButton);

JButton AllBookbutton = new JButton("显示所有书籍");
AllBookbutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
List<Book> list = BookController.findBookList();
rowData = new Object[list.size()][6];

int i = 0;
for (Book bo : list) {
rowData[i][0] = bo.getBookId();
rowData[i][1] = bo.getBookName();
rowData[i][2] = bo.getBookAuthor();
rowData[i][3] = bo.getBookNum();

rowData[i][4] = BookController.findBookCategory(bo.getBookCategory());
if(bo.getBookState() == 0) {
rowData[i][5] = "可借阅";
}else {
rowData[i][5] = "不可借阅";
}
i++;
}
table.setModel(new DefaultTableModel(rowData, columnNames));
}
});
AllBookbutton.setBounds(56, 24, 128, 27);
panel.add(AllBookbutton);

JLabel bookNameLabel = new JLabel("书 名:");
bookNameLabel.setBounds(445, 28, 72, 18);
panel.add(bookNameLabel);

bookNameField = new JTextField();
bookNameField.setBounds(523, 25, 86, 24);
panel.add(bookNameField);
bookNameField.setColumns(10);

JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 62, 876, 522);
panel_1.setLayout(new BorderLayout(0, 0));
add(panel_1);

table = new JTable(rowData,columnNames);
table.setModel(new DefaultTableModel(rowData, columnNames));
table.setBounds(25, 13, 781, 349);

DefaultTableCellRenderer r = new DefaultTableCellRenderer(); // 表格内容居中
r.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, r);

JTableHeader jTableHeader = table.getTableHeader();
panel_1.add(jTableHeader,BorderLayout.NORTH);
panel_1.add(table,BorderLayout.CENTER);

}

}

SearchUserList.java

查询用户列表面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package cn.egret.controller.page.smallPage.admin;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import cn.egret.controller.UserController;
import cn.egret.entity.User;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class SearchUserList extends JPanel {
private JTextField userIdField;
private Object[] columnNames = { "用户ID", "用户名", "密码", "年龄", "性别", "手机号", "地址", "账号状态" };
private Object[][] rowData;
JScrollPane scrollpane;
JTable table;

/**
* Create the application.
*/
public SearchUserList() {
setLayout(null);
setSize(881, 586);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 876, 62);
add(panel);
panel.setLayout(null);

userIdField = new JTextField();
userIdField.setBounds(515, 25, 86, 24);
panel.add(userIdField);
userIdField.setColumns(10);

JLabel userIdLabel = new JLabel("用户 ID:");
userIdLabel.setBounds(444, 28, 72, 18);
panel.add(userIdLabel);

JButton SearchbButton = new JButton("搜 索");
SearchbButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String userIdString = userIdField.getText();
if(userIdString.equals("") || userIdString == null) {
System.out.println("未填写用户ID");
JOptionPane.showMessageDialog(SearchbButton, "未填写用户ID", "提示", 1);
}else {
int userId = Integer.parseInt(userIdString);
User us = UserController.findUser(userId);
if(us != null) {
rowData = new Object[1][8];
rowData[0][0] = us.getUserId();
rowData[0][1] = us.getUserName();
rowData[0][2] = us.getPassWord();
rowData[0][3] = us.getAge();
rowData[0][4] = us.getGender();
rowData[0][5] = us.getPhone();
rowData[0][6] = us.getAddress();

if (us.getUserState() == 0) {
rowData[0][7] = "正常";
} else {
rowData[0][7] = "禁用";
}
table.setModel(new DefaultTableModel(rowData, columnNames));
}else {
System.out.println("没有此用户!");
JOptionPane.showMessageDialog(SearchbButton, "没有此用户!", "提示", 1);
}
}
}
});

SearchbButton.setBounds(663, 24, 113, 27);
panel.add(SearchbButton);

JButton AllUserbutton = new JButton("显示所有用户");
AllUserbutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
List<User> list = UserController.findCommonUser();
rowData = new Object[list.size()][8];

int i = 0;
for (User us : list) {
rowData[i][0] = us.getUserId();
rowData[i][1] = us.getUserName();
rowData[i][2] = us.getPassWord();
rowData[i][3] = us.getAge();
rowData[i][4] = us.getGender();
rowData[i][5] = us.getPhone();
rowData[i][6] = us.getAddress();

if (us.getUserState() == 0) {
rowData[i][7] = "正常";
} else {
rowData[i][7] = "禁用";
}
i++;
}
table.setModel(new DefaultTableModel(rowData, columnNames));
}
});
AllUserbutton.setBounds(56, 24, 128, 27);
panel.add(AllUserbutton);

JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 62, 876, 522);
panel_1.setLayout(new BorderLayout(0, 0));
add(panel_1);

table = new JTable(rowData,columnNames);
table.setModel(new DefaultTableModel(rowData, columnNames));
table.setBounds(25, 13, 781, 349);

DefaultTableCellRenderer r = new DefaultTableCellRenderer(); // 表格内容居中
r.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, r);

JTableHeader jTableHeader = table.getTableHeader();
panel_1.add(jTableHeader,BorderLayout.NORTH);
panel_1.add(table,BorderLayout.CENTER);
}
}

UserInfoSearch.java

用户详细信息查询面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.egret.controller.page.smallPage.admin;

import javax.swing.JPanel;

import cn.egret.entity.User;
import javax.swing.JLabel;
import java.awt.Font;

public class UserInfoSearch extends JPanel {

User user;
public UserInfoSearch(User user) {
this.user = user;
setLayout(null);

String userId = "用户ID:" + user.getUserId();
String userName = "用户名:" + user.getUserName();
String age = "年龄:" + user.getAge();
String gender = "性别:" + user.getGender();
String phone = "电话:" + user.getPhone();
String address = "地址:" + user.getAddress();
String userState = "用户状态:" + (user.getUserState() == 0 ? "正常" : "禁用");
String userRole = "用户类别:" + (user.getRole() == 0 ? "管理员" : "普通用户");

JLabel userIdLabel = new JLabel(userId);
userIdLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userIdLabel.setBounds(142, 65, 200, 37);
add(userIdLabel);

JLabel userNameLabel = new JLabel(userName);
userNameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userNameLabel.setBounds(440, 65, 299, 37);
add(userNameLabel);

JLabel ageLabel = new JLabel(age);
ageLabel.setFont(new Font("宋体", Font.PLAIN, 25));
ageLabel.setBounds(142, 175, 200, 42);
add(ageLabel);

JLabel phoneLabel = new JLabel(phone);
phoneLabel.setFont(new Font("宋体", Font.PLAIN, 25));
phoneLabel.setBounds(142, 284, 200, 37);
add(phoneLabel);

JLabel addressLabel = new JLabel(address);
addressLabel.setFont(new Font("宋体", Font.PLAIN, 25));
addressLabel.setBounds(440, 279, 213, 46);
add(addressLabel);

JLabel userStateLabel = new JLabel(userState);
userStateLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userStateLabel.setBounds(142, 393, 200, 37);
add(userStateLabel);

JLabel userRoleLabel = new JLabel(userRole);
userRoleLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userRoleLabel.setBounds(440, 393, 200, 37);
add(userRoleLabel);

JLabel genderLabel = new JLabel(gender);
genderLabel.setFont(new Font("宋体", Font.PLAIN, 25));
genderLabel.setBounds(440, 178, 213, 37);
add(genderLabel);
}
}

smallPage.common包内各面板的创建

WelcomePage.java

欢迎面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package cn.egret.controller.page.smallPage.common;

import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;

public class WelcomePage extends JPanel {

/**
* Create the application.
*/
public WelcomePage(String name) {
setLayout(null);
setSize(1000, 400);

JLabel label = new JLabel(name + " 您好,欢迎来到小波图书管理系统");
label.setFont(new Font("宋体", Font.PLAIN, 31));
label.setBounds(183, 144, 557, 125);
add(label);
}

}

BorrowAndGiveBackPage.java

借还书面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package cn.egret.controller.page.smallPage.common;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import cn.egret.controller.BookController;
import cn.egret.entity.User;

import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class BorrowAndGiveBackPage extends JPanel {
private JTextField bookIdField;
private JTextField bookNameField;

public BorrowAndGiveBackPage(User user) {
setLayout(null);

JLabel bookIdLabel = new JLabel("书籍编号:");
bookIdLabel.setFont(new Font("宋体", Font.PLAIN, 20));
bookIdLabel.setBounds(86, 167, 130, 36);
add(bookIdLabel);

JLabel bookNameLabel = new JLabel("书 名:");
bookNameLabel.setFont(new Font("宋体", Font.PLAIN, 20));
bookNameLabel.setBounds(101, 268, 125, 50);
add(bookNameLabel);

bookIdField = new JTextField();
bookIdField.setBounds(240, 175, 143, 24);
add(bookIdField);
bookIdField.setColumns(10);

bookNameField = new JTextField();
bookNameField.setBounds(240, 283, 143, 24);
add(bookNameField);
bookNameField.setColumns(10);

JButton borrowButton = new JButton("借 书");
borrowButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bookName = bookNameField.getText();
String bookId = bookIdField.getText();

if(bookName.equals("") || bookName == null || bookId.equals("") || bookId == null) {
System.out.println("未完全输入数据,请重新输入!");
JOptionPane.showMessageDialog(borrowButton, "未完全输入数据,请重新输入!", "提示", 1);
}else {
int bookIdNum = Integer.parseInt(bookId);
boolean flag = BookController.borrowBook(user.getUserId(), bookIdNum, bookName);
if(flag) {
System.out.println("借书成功!");
JOptionPane.showMessageDialog(borrowButton, "借书成功!", "提示", 1);
}else {
System.out.println("借书失败!");
JOptionPane.showMessageDialog(borrowButton, "借书失败!", "提示", 1);
}
}
bookNameField.setText("");
bookIdField.setText("");
}
});
borrowButton.setFont(new Font("宋体", Font.PLAIN, 21));
borrowButton.setBounds(69, 380, 125, 36);
add(borrowButton);

JButton giveBackButton = new JButton("还 书");
giveBackButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bookName = bookNameField.getText();
String bookId = bookIdField.getText();

if(bookName.equals("") || bookName == null || bookId.equals("") || bookId == null) {
System.out.println("未完全输入数据,请重新输入!");
JOptionPane.showMessageDialog(giveBackButton, "未完全输入数据,请重新输入!", "提示", 1);
}else {
int bookIdNum = Integer.parseInt(bookId);
boolean flag = BookController.giveBackBook(user.getUserId(), bookIdNum, bookName);
if(flag) {
System.out.println("还书成功!");
JOptionPane.showMessageDialog(giveBackButton, "还书成功!", "提示", 1);
}else {
System.out.println("还书失败!");
JOptionPane.showMessageDialog(giveBackButton, "还书失败!", "提示", 1);
}
}
bookNameField.setText("");
bookIdField.setText("");
}
});
giveBackButton.setFont(new Font("宋体", Font.PLAIN, 21));
giveBackButton.setBounds(285, 378, 125, 38);
add(giveBackButton);

}
}

SearchBookListPage.java

查询书籍列表面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package cn.egret.controller.page.smallPage.common;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import cn.egret.controller.BookController;
import cn.egret.controller.UserController;
import cn.egret.entity.Book;
import cn.egret.entity.User;

public class SearchBookListPage extends JPanel{

private JTextField bookIdField;
private Object[] columnNames = { "书籍编号", "书名", "作者", "剩余数量", "类别", "是否可借阅"};
private Object[][] rowData;
JScrollPane scrollpane;
JTable table;
private JTextField bookNameField;

/**
* Create the application.
*/
public SearchBookListPage() {

setLayout(null);
setSize(881, 586);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 876, 62);
add(panel);
panel.setLayout(null);

bookIdField = new JTextField();
bookIdField.setBounds(320, 25, 86, 24);
panel.add(bookIdField);
bookIdField.setColumns(10);

JLabel bookIdLabel = new JLabel("书籍编号:");
bookIdLabel.setBounds(236, 28, 92, 18);
panel.add(bookIdLabel);

JButton SearchbButton = new JButton("搜 索");
SearchbButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bookIdString = bookIdField.getText();
String bookNameString = bookNameField.getText();
if(bookIdString.equals("") || bookIdString == null || bookNameString.equals("") || bookNameString == null) {
System.out.println("查询内容未填写完整!");
JOptionPane.showMessageDialog(SearchbButton, "查询内容未填写完整!", "提示", 1);
}else {
int bookId = Integer.parseInt(bookIdString);
String bookName = bookNameString;

Book bo = BookController.findBook(bookId, bookName);
if(bo != null) {
rowData = new Object[1][6];
rowData[0][0] = bo.getBookId();
rowData[0][1] = bo.getBookName();
rowData[0][2] = bo.getBookAuthor();
rowData[0][3] = bo.getBookNum();

rowData[0][4] = BookController.findBookCategory(bo.getBookCategory());
if(bo.getBookState() == 0) {
rowData[0][5] = "可借阅";
}else {
rowData[0][5] = "不可借阅";
}

table.setModel(new DefaultTableModel(rowData, columnNames));
}else {
System.out.println("没有此书!");
JOptionPane.showMessageDialog(SearchbButton, "没有此书!", "提示", 1);
}
}
}
});

SearchbButton.setBounds(663, 24, 113, 27);
panel.add(SearchbButton);

JButton AllBookbutton = new JButton("显示所有书籍");
AllBookbutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
List<Book> list = BookController.findBookList();
rowData = new Object[list.size()][6];

int i = 0;
for (Book bo : list) {
rowData[i][0] = bo.getBookId();
rowData[i][1] = bo.getBookName();
rowData[i][2] = bo.getBookAuthor();
rowData[i][3] = bo.getBookNum();

rowData[i][4] = BookController.findBookCategory(bo.getBookCategory());
if(bo.getBookState() == 0) {
rowData[i][5] = "可借阅";
}else {
rowData[i][5] = "不可借阅";
}
i++;
}
table.setModel(new DefaultTableModel(rowData, columnNames));
}
});
AllBookbutton.setBounds(56, 24, 128, 27);
panel.add(AllBookbutton);

JLabel bookNameLabel = new JLabel("书 名:");
bookNameLabel.setBounds(445, 28, 72, 18);
panel.add(bookNameLabel);

bookNameField = new JTextField();
bookNameField.setBounds(523, 25, 86, 24);
panel.add(bookNameField);
bookNameField.setColumns(10);

JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 62, 876, 522);
panel_1.setLayout(new BorderLayout(0, 0));
add(panel_1);

table = new JTable(rowData,columnNames);
table.setModel(new DefaultTableModel(rowData, columnNames));
table.setBounds(25, 13, 781, 349);

DefaultTableCellRenderer r = new DefaultTableCellRenderer(); // 表格内容居中
r.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, r);

JTableHeader jTableHeader = table.getTableHeader();
panel_1.add(jTableHeader,BorderLayout.NORTH);
panel_1.add(table,BorderLayout.CENTER);

}

}

SearchUserBorrowInfoPage.java

查询用户借阅信息列表面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package cn.egret.controller.page.smallPage.common;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

import cn.egret.controller.BookController;
import cn.egret.entity.Book;
import cn.egret.entity.BorrowingInfo;

public class SearchUserBorrowInfoPage extends JPanel {
private Object[] columnNames = { "借阅信息编号", "用户ID", "书籍编号", "借阅状态", "借阅时间", "归还时间" };
private Object[][] rowData;
JTable table;
private JTextField userIdField;

public SearchUserBorrowInfoPage() {
setLayout(null);
setSize(881, 586);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 876, 62);
add(panel);
panel.setLayout(null);

JButton SearchbButton = new JButton("搜 索");
SearchbButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String userId = userIdField.getText();
if (userId.equals("") || userId == null) {
System.out.println("查询内容未填写完整!");
JOptionPane.showMessageDialog(SearchbButton, "查询内容未填写完整!", "提示", 1);
} else {
int userIdNum = Integer.parseInt(userId);

List<BorrowingInfo> userBI = BookController.findUserBorrowInfoList(userIdNum);
rowData = new Object[userBI.size()][6];

int i = 0;
for(BorrowingInfo bI: userBI) {
rowData[i][0] = bI.getBorrowingInfoId();
rowData[i][1] = bI.getUserId();
rowData[i][2] = bI.getBookId();

rowData[i][3] = (bI.getBorrowingState() == 0 ? "借阅中" : "已归还");
rowData[i][4] = bI.getBorrowingTime();
rowData[i][5] = bI.getGiveBackTime();
i++;
}
table.setModel(new DefaultTableModel(rowData, columnNames));
}
}
});

SearchbButton.setBounds(588, 24, 113, 27);
panel.add(SearchbButton);

JButton AllInfobutton = new JButton("显示所有借阅信息");
AllInfobutton.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
List<BorrowingInfo> userBI = BookController.findAllBorrowInfoList();
rowData = new Object[userBI.size()][6];

int i = 0;
for(BorrowingInfo bI: userBI) {
rowData[i][0] = bI.getBorrowingInfoId();
rowData[i][1] = bI.getUserId();
rowData[i][2] = bI.getBookId();
rowData[i][3] = (bI.getBorrowingState() == 0 ? "借阅中" : "已归还");
rowData[i][4] = bI.getBorrowingTime();
rowData[i][5] = bI.getGiveBackTime();
i++;
}
table.setModel(new DefaultTableModel(rowData, columnNames));
}
});
AllInfobutton.setBounds(66, 24, 170, 27);
panel.add(AllInfobutton);

JLabel userIdLabel = new JLabel("用户ID:");
userIdLabel.setBounds(333, 28, 72, 18);
panel.add(userIdLabel);

userIdField = new JTextField();
userIdField.setBounds(419, 25, 86, 24);
panel.add(userIdField);
userIdField.setColumns(10);

JPanel panel_1 = new JPanel();
panel_1.setBounds(0, 62, 876, 522);
panel_1.setLayout(new BorderLayout(0, 0));
add(panel_1);

table = new JTable(rowData, columnNames);
table.setModel(new DefaultTableModel(rowData, columnNames));
table.setBounds(25, 13, 781, 349);

DefaultTableCellRenderer r = new DefaultTableCellRenderer(); // 表格内容居中
r.setHorizontalAlignment(JLabel.CENTER);
table.setDefaultRenderer(Object.class, r);

JTableHeader jTableHeader = table.getTableHeader();
panel_1.add(jTableHeader, BorderLayout.NORTH);
panel_1.add(table, BorderLayout.CENTER);
}
}

UpdataPasswordPage.java

更改密码面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package cn.egret.controller.page.smallPage.common;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;

import cn.egret.controller.UserController;
import cn.egret.entity.User;

import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class UpdataPasswordPage extends JPanel {
private JPasswordField passwordField;
private JPasswordField passwordField_1;
private JPasswordField passwordField_2;

public UpdataPasswordPage(User user) {
setLayout(null);

JLabel oldPasswordLabel = new JLabel("旧密码:");
oldPasswordLabel.setFont(new Font("宋体", Font.PLAIN, 20));
oldPasswordLabel.setBounds(205, 157, 113, 18);
add(oldPasswordLabel);

JLabel newPassword1 = new JLabel("新密码:");
newPassword1.setFont(new Font("宋体", Font.PLAIN, 20));
newPassword1.setBounds(205, 241, 113, 18);
add(newPassword1);

JLabel newPassword2 = new JLabel("重复新密码:");
newPassword2.setFont(new Font("宋体", Font.PLAIN, 20));
newPassword2.setBounds(184, 334, 147, 18);
add(newPassword2);

passwordField = new JPasswordField();
passwordField.setBounds(357, 156, 155, 24);
add(passwordField);

passwordField_1 = new JPasswordField();
passwordField_1.setBounds(357, 240, 155, 24);
add(passwordField_1);

passwordField_2 = new JPasswordField();
passwordField_2.setBounds(357, 333, 155, 24);
add(passwordField_2);

JButton Button = new JButton("提 交");
Button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String oldPassword = new String(passwordField.getPassword());
String newPassword1 = new String(passwordField_1.getPassword());
String newPassword2 = new String(passwordField_2.getPassword());

if(!oldPassword.equals(user.getPassWord())) {
System.out.println("旧密码错误!");
JOptionPane.showMessageDialog(Button, "旧密码错误!", "提示", 1);
}else {
if(!newPassword1.equals(newPassword2)) {
System.out.println("两次输入的密码不一样!");
JOptionPane.showMessageDialog(Button, "两次输入的密码不一样!", "提示", 1);
}else {
boolean flag = UserController.changePassword(user.getUserId(), newPassword1);
if(flag) {
System.out.println("修改密码成功!");
JOptionPane.showMessageDialog(Button, "修改密码成功!", "提示", 1);
}else {
System.out.println("修改密码失败!");
JOptionPane.showMessageDialog(Button, "修改密码失败!", "提示", 1);
}
}
}

passwordField.setText("");
passwordField_1.setText("");
passwordField_2.setText("");
}
});
Button.setFont(new Font("宋体", Font.PLAIN, 18));
Button.setBounds(278, 417, 130, 38);
add(Button);
}
}

UpdateBaseInfoPage.java

更改用户基本信息面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package cn.egret.controller.page.smallPage.common;

import javax.swing.JPanel;

import cn.egret.controller.UserController;
import cn.egret.entity.User;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.Font;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class UpdateBaseInfoPage extends JPanel {
private JTextField ageField;
private JTextField genderField;
private JTextField phoneField;
private JTextField addressField;

public UpdateBaseInfoPage(User user) {
setLayout(null);

JLabel ageLabel = new JLabel("年 龄:");
ageLabel.setFont(new Font("宋体", Font.PLAIN, 25));
ageLabel.setBounds(113, 104, 131, 39);
add(ageLabel);

JLabel genderLabel = new JLabel("性 别:");
genderLabel.setFont(new Font("宋体", Font.PLAIN, 25));
genderLabel.setBounds(113, 188, 131, 39);
add(genderLabel);

JLabel phoneLabel = new JLabel("电 话:");
phoneLabel.setFont(new Font("宋体", Font.PLAIN, 25));
phoneLabel.setBounds(113, 276, 131, 39);
add(phoneLabel);

JLabel addressLabel = new JLabel("地 址:");
addressLabel.setFont(new Font("宋体", Font.PLAIN, 25));
addressLabel.setBounds(113, 357, 131, 39);
add(addressLabel);

ageField = new JTextField();
ageField.setFont(new Font("宋体", Font.PLAIN, 25));
ageField.setBounds(258, 104, 153, 39);
add(ageField);
ageField.setColumns(10);

genderField = new JTextField();
genderField.setFont(new Font("宋体", Font.PLAIN, 25));
genderField.setBounds(258, 192, 153, 39);
add(genderField);
genderField.setColumns(10);

phoneField = new JTextField();
phoneField.setFont(new Font("宋体", Font.PLAIN, 25));
phoneField.setBounds(258, 277, 153, 39);
add(phoneField);
phoneField.setColumns(10);

addressField = new JTextField();
addressField.setFont(new Font("宋体", Font.PLAIN, 25));
addressField.setBounds(258, 361, 153, 39);
add(addressField);
addressField.setColumns(10);

JButton button = new JButton("确认修改");
button.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String ageString = ageField.getText();
String gender = genderField.getText();
String phone = phoneField.getText();
String address = addressField.getText();

int age;
if(ageString == null || ageString.equals("")) {
age = -1;
}else {
age = Integer.parseInt(ageString);
}

boolean flag = UserController.changeBaseInfo(user.getUserId(), age, gender, phone, address);

if(flag) {
System.out.println("更新成功!");
JOptionPane.showMessageDialog(button, "更新成功!", "提示", 1);
}else {
System.out.println("更新失败!");
JOptionPane.showMessageDialog(button, "更新失败!", "提示", 1);
}
ageField.setText("");
genderField.setText("");
phoneField.setText("");
addressField.setText("");
}
});
button.setFont(new Font("宋体", Font.PLAIN, 20));
button.setBounds(163, 441, 131, 45);
add(button);
}
}

UserInfoSearchPage.java

用户详细信息查询面板

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.egret.controller.page.smallPage.common;

import javax.swing.JPanel;

import cn.egret.entity.User;
import javax.swing.JLabel;
import java.awt.Font;

public class UserInfoSearchPage extends JPanel {

User user;
public UserInfoSearchPage(User user) {
this.user = user;
setLayout(null);

String userId = "用户ID:" + user.getUserId();
String userName = "用户名:" + user.getUserName();
String age = "年龄:" + user.getAge();
String gender = "性别:" + user.getGender();
String phone = "电话:" + user.getPhone();
String address = "地址:" + user.getAddress();
String userState = "用户状态:" + (user.getUserState() == 0 ? "正常" : "禁用");
String userRole = "用户类别:" + (user.getRole() == 0 ? "管理员" : "普通用户");

JLabel userIdLabel = new JLabel(userId);
userIdLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userIdLabel.setBounds(142, 65, 200, 37);
add(userIdLabel);

JLabel userNameLabel = new JLabel(userName);
userNameLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userNameLabel.setBounds(440, 65, 299, 37);
add(userNameLabel);

JLabel ageLabel = new JLabel(age);
ageLabel.setFont(new Font("宋体", Font.PLAIN, 25));
ageLabel.setBounds(142, 175, 200, 42);
add(ageLabel);

JLabel phoneLabel = new JLabel(phone);
phoneLabel.setFont(new Font("宋体", Font.PLAIN, 25));
phoneLabel.setBounds(142, 284, 200, 37);
add(phoneLabel);

JLabel addressLabel = new JLabel(address);
addressLabel.setFont(new Font("宋体", Font.PLAIN, 25));
addressLabel.setBounds(440, 279, 213, 46);
add(addressLabel);

JLabel userStateLabel = new JLabel(userState);
userStateLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userStateLabel.setBounds(142, 393, 200, 37);
add(userStateLabel);

JLabel userRoleLabel = new JLabel(userRole);
userRoleLabel.setFont(new Font("宋体", Font.PLAIN, 25));
userRoleLabel.setBounds(440, 393, 241, 37);
add(userRoleLabel);

JLabel genderLabel = new JLabel(gender);
genderLabel.setFont(new Font("宋体", Font.PLAIN, 25));
genderLabel.setBounds(440, 178, 213, 37);
add(genderLabel);
}
}

十、Test包内测试类的创建

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package cn.egret.test;

import java.util.ArrayList;
import java.util.List;

import cn.egret.controller.page.smallPage.admin.WelcomePage;
import cn.egret.dao.impl.BookCategoryDaoImpl;
import cn.egret.dao.impl.BookDaoImpl;
import cn.egret.dao.impl.BorrowInfoDaoImpl;
import cn.egret.dao.impl.UserDaoImpl;
import cn.egret.entity.Book;
import cn.egret.entity.BorrowingInfo;
import cn.egret.entity.User;

public class Test {
public static void main(String[] args) {
User user = new User("小王","121212",18,"man","110","sky",0,1);
UserDaoImpl u = new UserDaoImpl();
// u.addUser(user);
// System.out.println(user.getUserId());

// User user2 = u.findUserByNameAndPassword("张三", "123456");
// System.out.println(user2);

// System.out.println(u.findPasswordById(3));

// System.out.println(u.updatePasswordById(1, "555555"));

// User user3 = u.findUserById(2);
// System.out.println(user3);

// System.out.println(u.updateUserStateById(2, 1));

// System.out.println(u.deleteUserByIdAndUsername(1, "张三"));
//
// List<User> l = u.findUser(1);
// for(User use: l) {
// System.out.println(use);
// }

// System.out.println(u.getUserIdByName("张三"));

Book book = new Book(2001,"一书一世界","张晓波",3,2,0);
BookDaoImpl b = new BookDaoImpl();
// b.addBook(book);

// b.deleteBook(2001, "一书一世界");

// List<Book> listBook = b.findBookList();
// for(Book bo: listBook) {
// System.out.println(bo);
// }
//

// Book book2 = b.findBook(2, "操作系统");
// System.out.println(book2);

// b.changeBookState(1, "操作系统", 0);

// b.borrowBook(1, "操作系统");

// b.giveBackBook(1, "操作系统");

BorrowInfoDaoImpl bor = new BorrowInfoDaoImpl();
// bor.borrowBook(2, 1);

// bor.giveBackBook(2, 1);

// List<BorrowingInfo> BIList = bor.findBorrowInfo(2);
//
// for(BorrowingInfo bbb: BIList) {
// System.out.println(bbb);
// }
//

BookCategoryDaoImpl bci = new BookCategoryDaoImpl();
// System.out.println(bci.findBookCategoryById(4));

// new WelcomePage();
}
}

三、最终效果

一、登录界面

登录界面

二、注册界面

注册界面

三、普通用户界面

1、主界面

主界面

2、书籍列表查询

书籍列表查询

3、借还书籍

借还书籍

4、个人借阅历史查询

个人借阅历史查询

5、密码修改

密码修改

6、个人基础信息修改

个人基础信息修改

7、个人信息查询

个人信息查询

四、管理员界面

1、主界面

主界面

2、管理员添加

管理员添加

3、用户列表查询

用户列表查询

4、禁用与启用用户

禁用与启用用户

5、书籍列表查询

书籍列表查询

6、书籍添加与删除

书籍添加与删除

7、个人信息查询

个人信息查询

终此!