javaWeb图书管理系统version 1.1

version 1.1

javaWeb

MVC

一种编程思想

C(Controller)

控制、派发、Servlet

M(Model)

业务数据、service、entity、repository

V(View)

视图、jsp

MVC

图书管理系统简单模拟

开发人员:1人
开发环境:windows10/jdk1.8/mysql5.7
开发工具:Idea/Datagrip/
项目介绍:图书管理系统模拟、首先分为读者和图书管理员、读者可登录进行图书借阅、归还、等待图书管理员审批、图书管理员可登录对读者进行审批、记录等操作
项目技术:JSP+Jquery+Java+JavaWeb

数据库

读者表 管理员表 图书表 图书分类表 借阅表 归还表

数据库

后端结构

Controller Entity Filter Repository Service Utils C3P0-config.xml web css images js WEB-INF jsp

项目分层

Controller

Controller

Entity

Entity

Filter

Filter

Repository

Repository

Service

Service

Uitls

Utils

C3P0-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

<named-config name="testc3p0">

<!-- 指定连接数据源的基本属性 -->
<property name="user">root</property>
<property name="password">xiaobo</property>
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/db_library?useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=UTC</property>

<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">10</property>
</named-config>
</c3p0-config>

web

web

Code

BookServlet

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
/**
* 图书分页Controller
* @author xiaobo
* @date 2021/6/23 - 10:37
*/

@WebServlet("/book")
public class BookServlet extends HttpServlet {

private BookService bookService = new BookServiceImpl();

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

String method = req.getParameter("method");
Reader reader = (Reader)req.getSession().getAttribute("object");

if (null == method){
method = "findAll";
}
switch (method){
case "findAll":
String pageStr = req.getParameter("page");
Integer page = Integer.parseInt(pageStr);

req.setAttribute("list",bookService.findAll(page));
req.setAttribute("dataPrePage",5);
req.setAttribute("currentPage",page);
req.setAttribute("pages",bookService.getPages());
req.setAttribute("reader",reader);

req.getRequestDispatcher("index.jsp").forward(req,resp);
break;
case "addBorrow":
String bookidStr = req.getParameter("bookid");
Integer bookid = Integer.parseInt(bookidStr);
bookService.addBorrow(bookid,reader.getId());
resp.sendRedirect("/book?method=findAllBorrow&page=1");
break;
case"findAllBorrow":
pageStr = req.getParameter("page");
page = Integer.parseInt(pageStr);

req.setAttribute("list",bookService.findBorrowById(reader.getId(),page));
req.setAttribute("dataPrePage",5);
req.setAttribute("currentPage",page);
req.setAttribute("pages",bookService.getBorrowPages(reader.getId()));

req.setAttribute("reader",reader);
req.getRequestDispatcher("borrow.jsp").forward(req,resp);
break;
default:
break;
}
}
}

BookServiceImpl

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
/**图书业务处理service实现类
* @author xiaobo
* @date 2021/6/23 - 10:00
*/
public class BookServiceImpl implements BookService {

private BookRepository bookRepository = new BookRepositoryImpl();
private BorrowRepository borrowRepository = new BorrowRepositoryImpl();
private final int LIMIT = 5;

/**
* 图书查询
* @param page 当前页码
* @return 查询到的图书结果集
*/
@Override
public List<Book> findAll(int page) {

//计算起始位置、总页码数-1 乘 分页数量
int index = (page - 1) * LIMIT;

return bookRepository.findAll(index,LIMIT);
}

/**
* 总页码设置
* @return 返回总页码数
*/
@Override
public int getPages() {
int count = bookRepository.count();
int page = 0;
if(count / LIMIT == 0){
page = count/LIMIT;
}else{
page = count/LIMIT + 1;
}
return page;
}

/**
* 借阅业务处理接口
* @param bookId 图书编号
* @param readerId 读者编号
*/
@Override
public void addBorrow(Integer bookId, Integer readerId) {
//获取当前系统时间
Date date = new Date();
//将时间转为字符串格式
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
String dates = simpleDateFormat.format(date);

//将当前系统时间+14天设置为最晚归还日期
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 14);
Date time = calendar.getTime();
String returnTime = simpleDateFormat.format(time);
borrowRepository.insert(bookId,readerId,dates,returnTime,null,0);
}

/**
*
* @param id
* @param page
* @return
*/
@Override
public List<Borrow> findBorrowById(Integer id,Integer page) {
int index = (page - 1) * LIMIT;
return borrowRepository.findAllById(id,index,LIMIT);
}

@Override
public int getBorrowPages(Integer id) {
int count = borrowRepository.count(id);
int page = 0;
if (count % LIMIT == 0){
page = count / LIMIT;
}else{
page = count / LIMIT + 1;
}
return page;
}

@Override
public List<Borrow> findBorrowByState(Integer state, Integer page) {
int index = (page - 1) * LIMIT;
return borrowRepository.findAllByState(state,index,LIMIT);
}


@Override
public int getBorrowStatePages(Integer state) {
int count = borrowRepository.countState(state);
int page = 0;
if(count % LIMIT == 0){
page = count / LIMIT;
}else{
page = count / LIMIT + 1;
}
return page;
}
}

BookRepositoryImpl

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
/**
* @author xiaobo
* @date 2021/6/23 - 10:03
*/
public class BookRepositoryImpl implements BookRepository {

//成员变量、扩大作用范围
private static ComboPooledDataSource dataSource = null;
private Connection connection = null;
private PreparedStatement preparedStatement = null;
private ResultSet resultSet = null;
private List<Book> list = null;

/**
* 图书分页查询
* @param index 起始下标
* @param limit 查询个数
* @return 返回结果集
*/
@Override
public List<Book> findAll(int index,int limit) {
try {
//调用自定义工具类访问数据库
connection = JDBCTools.getConnection();

//定义SQL语句、赋值、并执行
String sql = "SELECT * FROM book,bookcase WHERE book.bookcaseid = bookcase.id limit ?,?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1,index);
preparedStatement.setInt(2,limit);
resultSet = preparedStatement.executeQuery();
//定义一个List集合接受查询结果
list = new ArrayList<>();
//遍历查询的结果集
while (resultSet.next()){
list.add(new Book(resultSet.getInt(1),
resultSet.getString(2),
resultSet.getString(3),
resultSet.getString(4),
resultSet.getInt(5),
resultSet.getDouble(6),
new BookCase(resultSet.getInt(9),resultSet.getString(10))
));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
//调用自定义工具类关闭资源
JDBCTools.release(connection,preparedStatement,resultSet);
}

return list;
}

/**
* 查询图书数量
* @return 返回查询到的数量个数
*/
@Override
public int count() {

int count = 0;

try {
//调用自定义工具类访问数据库
connection = JDBCTools.getConnection();
//定义SQL语句、赋值、并执行
String sql = "SELECT count(*) FROM book,bookcase WHERE book.bookcaseid = bookcase.id";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
//判断查询到的结果集
while (resultSet.next()){
count = resultSet.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//调用自定义工具类关闭资源
JDBCTools.release(connection,preparedStatement,resultSet);
}
return count;
}
}

JDBCTools

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
/**
* @author xiaobo
* @date 2021/6/22 - 23:43
*/
public class JDBCTools {

//成员变量、扩大作用范围
private static Connection connection = null;
private static ComboPooledDataSource dataSource;

//静态代码块、类加载时初始化
static{
dataSource = new ComboPooledDataSource("testc3p0");
}

/**
* 获取数据库连接
* @return 返回数据库连接驱动
*/
public static Connection getConnection(){

try {

connection = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return connection;
}

/**
* 关闭资源
* @param connection
* @param statement
* @param resultSet
*/
public static void release(Connection connection, Statement statement, ResultSet resultSet){

try {
if(connection != null){
connection.close();
}
if(statement != null){
statement.close();
}
if(resultSet != null){
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}

Book

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
/**
* 图书实体类
* @author xiaobo
* @date 2021/6/23 - 9:54
*/
public class Book {

private Integer id;
private String name;
private String author;
private String publish;
private Integer pages;
private Double price;
private BookCase bookCase;

public Book(String name, String author, String publish) {
this.name = name;
this.author = author;
this.publish = publish;
}

public Book(Integer id, String name, String author, String publish, Integer pages, Double price, BookCase bookCase) {
this.id = id;
this.name = name;
this.author = author;
this.publish = publish;
this.pages = pages;
this.price = price;
this.bookCase = bookCase;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getPublish() {
return publish;
}

public void setPublish(String publish) {
this.publish = publish;
}

public Integer getPages() {
return pages;
}

public void setPages(Integer pages) {
this.pages = pages;
}

public Double getPrice() {
return price;
}

public void setPrice(Double price) {
this.price = price;
}

public BookCase getBookCase() {
return bookCase;
}

public void setBookCase(BookCase bookCase) {
this.bookCase = bookCase;
}
}

效果展示

功能欠缺

管理员图书添加功能
管理员图书修改功能
管理员图书删除功能
借阅状态更新 已借出的图书不允许再借、未借出的图书允许借阅

小博正在完善功能、后续更新……

正确的开始、微小的长进、然后持续、嘿、我是小博、带你一起看我目之所及的世界……

-------------本文结束 感谢您的阅读-------------

本文标题:javaWeb图书管理系统version 1.1

文章作者:小博

发布时间:2021年06月26日 - 19:33

最后更新:2021年06月26日 - 19:37

原始链接:https://codexiaobo.github.io/posts/4197782020/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。