软件环境
Operating System:Windows10
IDEA:2018.2
Java:jdk1.8
Mysql:8.0.13
Tomcat:7.0.85
该图书管理系统实现了用户注册与登录功能,实现了实现了图书列表展示,购物车简单的功能。后台表只有三张,一张是user表,存储的是用户的信息,一张是book表,存储的是图书的信息,另外一张是card表,用来存储某个用户对应的购物车的信息。
下面是整个整个工程的截图
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/24
Time: 10:50
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.service.impl.BookServiceImpl" %>
<%@ page import="com.sjsq.po.User" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>查看图书</title>
<style type="text/css">
h1{
text-align: center;
}
#before{
text-align: center;
}
#head{
background: #eeeeee;height: 80px;
}
#headLink{
font-size: 20px;
}
#headWelLink{
font-size: 20px;
}
</style>
</head>
<body>
<%--头部信息--%>
<%
User user =(User)session.getAttribute("user");
if(user == null){
response.sendRedirect("login.jsp");
}else {
%>
<div id="head">
<table width="100%">
<td id="headWelLink">欢迎您:<%=user.getName()%></td>
<td align="right" id="headLink">
<a href="cart.jsp">我的购物车</a>
<a href="logout.jsp">安全退出</a>
</td>
</table>
</div>
<%
}
%>
<%--图书信息--%>
<%
Book book = new Book();
BookServiceImpl service = new BookServiceImpl();
List<Book> list = service.select(book);
%>
<h1>图书列表</h1>
<div id="before">
<a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
<table align="center" cellpadding="10" cellspacing="10">
<tr bgcolor="green">
<td>编号</td>
<td>书名</td>
<td>价格</td>
<td>作者</td>
<td>封皮</td>
<td>出版社</td>
</tr>
<%
String bg = null;
for (int i = 0;i<list.size();i++){
Book b =list.get(i);
if(i%2 == 0){
bg = "pink";
}else{
bg = "yellow";
}
%>
<tr bgcolor="<%=bg%>">
<td><%=b.getBookid()%></td>
<td><a href="doInfo.jsp?bookid=<%=b.getBookid()%>"><%=b.getBookname()%></a></td>
<td><%=b.getPrice() %></td>
<td><%=b.getAuthor() %></td>
<td><%=b.getPicture() %></td>
<td><%=b.getPublish() %></td>
</tr>
<%
}
%>
</table>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/31
Time: 10:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="com.sjsq.po.User" %>
<%@ page import="com.sjsq.po.Card" %>
<%@ page import="com.sjsq.service.CardService" %>
<%@ page import="com.sjsq.service.impl.CardServiceImpl" %>
<%@ page import="java.util.List" %>
<%@ page import="com.sjsq.dao.BookDao" %>
<%@ page import="com.sjsq.dao.impl.BookDaoImpl" %>
<html>
<head>
<title>购物车信息</title>
<style type="text/css">
h1{
text-align: center;
}
#before{
text-align: center;
}
#head{
background: #eeeeee;height: 80px;
}
#headLink{
font-size: 20px;
}
#headWelLink{
font-size: 20px;
}
</style>
</head>
<body>
<%--头部信息--%>
<%
User user =(User)session.getAttribute("user");
if(user == null){
response.sendRedirect("login.jsp");
}else {
%>
<div id="head">
<table width="100%">
<td id="headWelLink">欢迎您:<%=user.getName()%></td>
<td align="right" id="headLink">
<a href="cart.jsp">我的购物车</a>
<a href="logout.jsp">安全退出</a>
</td>
</table>
</div>
<%
}
%>
<%--图书信息--%>
<%
CardService service = new CardServiceImpl();
List<Card> list = service.getCard(user.getId());
BookDao dao = new BookDaoImpl();
Double totalPrice = 0D;
Double bookPrice = 0D;
%>
<h1>购物车图书</h1>
<div id="before">
<a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
<table align="center" cellpadding="10" cellspacing="10">
<tr bgcolor="green">
<td>姓名</td>
<td>图书序号</td>
<td>书名</td>
<td>数量</td>
<td>价格小计</td>
</tr>
<%
for (int i = 0;i<list.size();i++){
Card card = list.get(i);
String bookName = dao.getBook(card.getBookid()).getBookname();
bookPrice = dao.getBook(card.getBookid()).getPrice()*card.getBooknum();
totalPrice = bookPrice + totalPrice;
%>
<tr bgcolor="#ffdead">
<td><%=card.getUsername() %></td>
<td><%=card.getBookid() %></td>
<td><%=bookName %></td>
<td><%=card.getBooknum() %></td>
<td><%=bookPrice%></td>
</tr>
<%
}
%>
<tr>
<td colspan="4" align="right" bgcolor="#6495ed">价格总计</td>
<td bgcolor="#6495ed"><%=totalPrice %></td>
</tr>
</table>
<div style="text-align:center;font-size:20px;margin-top:20px;">
<a href="book.jsp">继续购买图书</a>
<a href="login.jsp">登陆页面</a>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/24
Time: 10:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.po.User" %>
<%
// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图书详情</title>
<style type="text/css">
h1{
text-align: center;
}
a{
text-align:center;font-size: 24px;text-decoration: none;
}
a:hover{
text-decoration: underline;font-size: 20px;
}
#before{
text-align: center;
}
#head{
background: #eeeeee;height: 80px;
}
#headLink{
font-size: 20px;
}
#headWelLink{
font-size: 20px;
}
</style>
</head>
<body>
<%--头部信息--%>
<%
User user =(User)session.getAttribute("user");
if(user == null){
response.sendRedirect("login.jsp");
}else {
%>
<div id="head">
<table width="100%">
<td id="headWelLink">欢迎您:<%=user.getName()%></td>
<td align="right" id="headLink">
<a href="cart.jsp">我的购物车</a>
<a href="logout.jsp">安全退出</a>
</td>
</table>
</div>
<%
}
%>
<h1>图书详情</h1>
<div id="before">
<a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
<%
Book book = (Book)session.getAttribute("book");
%>
<table align="center" cellpadding="20" cellspacing="20">
<tr style="font-size: 20px">
<td>图书编号</td>
<td>图书名称</td>
<td>图书价格</td>
<td>图书作者</td>
<td>图书封皮</td>
<td>图书出版社</td>
</tr>
<tr>
<td><%=book.getBookid()%></td>
<td><%=book.getBookname()%></td>
<td><%=book.getPrice()%></td>
<td><%=book.getAuthor()%></td>
<td><%=book.getPicture()%></td>
<td><%=book.getPublish()%></td>
</tr>
<tr>
<td colspan="3"></td>
<td></td>
<td colspan="2"></td>
</tr>
</table>
<div style="text-align:center;font-size: 36px;">
<a href="doCart.jsp">添加到购物车</a>
<a href="book.jsp">图书列表</a>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/31
Time: 10:40
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.po.User" %>
<%@ page import="com.sjsq.service.CardService" %>
<%@ page import="com.sjsq.service.impl.CardServiceImpl" %>
<html>
<head>
<title>处理购物车</title>
</head>
<body>
<%-- 处理购物车 --%>
<%
// 获取用户的信息
User user =(User)session.getAttribute("user");
// 获取书籍的信息
Book book = (Book)session.getAttribute("book");
// 定义购物车服务
CardService service = new CardServiceImpl();
// 获取图书数量且加1
Integer booknum = service.getBookNum(book) + 1;
// 执行添加购物车操作
boolean flag = service.addCard(user,book,booknum);
if(flag){
// 添加成功返回到购物车页面
response.sendRedirect("cart.jsp");
}else{
//
response.sendRedirect("doCartFail.jsp");
}
%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/31
Time: 11:45
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>添加失败</title>
<style type="text/css">
h1{
text-align: center;
}
h4{
text-align: center;color: red;
}
body{
background-color: antiquewhite;
}
div{
text-align: center;
}
</style>
</head>
<body>
<h1>添加购物车失败</h1>
<hr>
<h4>请重新添加</h4>
<div>
<a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/24
Time: 10:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="com.sjsq.po.Book" %>
<%@ page import="com.sjsq.service.BookService" %>
<%@ page import="com.sjsq.service.impl.BookServiceImpl" %>
<%
// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>书籍信息</title>
</head>
<body>
<%
Book book = new Book();
String sid = request.getParameter("bookid");
Integer id = Integer.parseInt(sid);
BookService service = new BookServiceImpl();
book.setBookid(id);
Book bookCur = service.getBook(book);
// 控制台打印出类的信息(日志的前身)
System.out.print("doInfo.jsp的信息-->");
System.out.println(bookCur);
session.setAttribute("book", bookCur);
response.sendRedirect("detail.jsp");
%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/21
Time: 23:45
To change this template use File | Settings | File Templates.
--%>
<%@page import="com.sjsq.dao.impl.UserDaoImpl"%>
<%@page import="com.sjsq.dao.UserDao"%>
<%@page import="com.sjsq.po.User"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>处理注册</title>
</head>
<body>
<%
// 设置获取注册时的编码为UTF-8
request.setCharacterEncoding("UTF-8");
User user=new User();
//获取register.jsp页面提交的账号和密码
String name=request.getParameter("name");
String password=request.getParameter("password");
String email=request.getParameter("email");
String phone=request.getParameter("phone");
//获取register.jsp页面提交的账号和密码设置到实体类User中
user.setName(name);
user.setPassword(password);
user.setEmail(email);
user.setPhone(phone);
//引入数据交互层
UserDao dao=new UserDaoImpl();
boolean flag=dao.register(user);
if(flag){
response.sendRedirect("login.jsp");
}else{
response.sendRedirect("register.jsp");
}
%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/22
Time: 23:23
To change this template use File | Settings | File Templates.
--%>
<%@page import="java.util.List"%>
<%@page import="com.sjsq.service.impl.UserServiceImpl"%>
<%@page import="com.sjsq.po.User"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%
// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>处理找回密码</title>
</head>
<body>
<%
User user=new User();
//获取searchPassword.jsp页面提交的账号和密码
String name=request.getParameter("name");
user.setName(name);
UserServiceImpl service=new UserServiceImpl();
List<User> list=service.selectUser(user);
request.setAttribute("list", list);
for(User u:list){
request.setAttribute("user", u);
out.print(u);
}
if(user!=null){
//response.sendRedirect("search.jsp");//不传输数据的转发
request.getRequestDispatcher("search.jsp").forward(request, response);
}
%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/26
Time: 23:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录失败</title>
<style type="text/css">
h1{
text-align: center;
}
h4{
text-align: center;color: red;
}
body{
background-color: antiquewhite;
}
div{
text-align: center;
}
</style>
</head>
<body>
<h1>现存图书列表</h1>
<hr>
<h4>登录失败</h4>
<div>
<a href="login.jsp">返回登录</a>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/26
Time: 22:56
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%@ page import="com.sjsq.po.User"%>
<%@ page import="com.sjsq.dao.UserDao"%>
<%@ page import="com.sjsq.dao.impl.UserDaoImpl"%>
<%@ page import="com.sjsq.service.UserService" %>
<%@ page import="com.sjsq.service.impl.UserServiceImpl" %>
<%@ page import="java.util.List" %>
<%
// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
<style type="text/css">
h1{
text-align: center;
}
h4{
text-align: center;color: red;
}
body{
background-color: antiquewhite;
}
</style>
</head>
<body>
<h1>现存图书列表</h1>
<hr>
<h4>---装饰中---</h4>
<%
// 设置接收的编码为UTF-8
request.setCharacterEncoding("utf-8");
User user = new User();
UserDao dao = new UserDaoImpl();
String name = request.getParameter("name");
String password=request.getParameter("password");
user.setName(name);
user.setPassword(password);
User us=dao.login(user);
// 把数据库里面的User获取出来
UserService service = new UserServiceImpl();
List<User> list = service.selectUser(user);
for(int i=0;i<list.size();i++){
user = list.get(i);
}
System.out.println("显示用户信息:");
System.out.println(user);
session.setAttribute("user",user);
if(us != null){
response.sendRedirect("book.jsp");
}else{
response.sendRedirect("fail.jsp");
}
%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/19
Time: 22:44
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%
// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户登录</title>
<style type="text/css">
h1{
text-align: center;
}
h4{
text-align: center;color: red;
}
body{
background-color: antiquewhite;
}
a{
text-decoration: none;font-size: 20px;color: black;
}
a:hover{
text-decoration: underline;font-size: 24px;color: red;
}
</style>
</head>
<body>
<form action="info.jsp" method="post">
<h1>用户登录</h1>
<h4>---正在美化中---</h4>
<hr/>
<table align="center">
<tr>
<td>账号:</td>
<td><input type="text" name="name" id="name" placeholder="请输入您的账号" autofocus="autofocus"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" id="password" placeholder="请输入您的密码"></td>
<td><a href="searchPassword.jsp">找回密码</a></td>
</tr>
<tr>
<td colspan="1">
</td>
<td>
<input type="submit" value="登录"/>
<input type="reset" value="重置"/>
<a href="register.jsp" target="_blank">注册</a>
</td>
</tr>
</table>
</form>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/25
Time: 21:51
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>退出登录</title>
</head>
<body>
<%
session.invalidate();
response.sendRedirect("login.jsp");
%>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/21
Time: 23:14
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>用户注册</title>
<style type="text/css">
h1{
text-align: center;
}
h4{
text-align: center;color: red;
}
body{
background-color: antiquewhite;
}
div{
text-align: center;
}
</style>
</head>
<body>
<h1>用户注册</h1>
<h4>---装饰中---</h4>
<hr/>
<form action="doregister.jsp" method="post" name="registerForm">
<div>
<tr>
<label>您的账号:</label>
<input type="text" name="name" id="name" placeholder="请输入用户名" autofocus="autofocus">
</tr>
</div>
<div>
<tr>
<label>您的密码:</label></td>
<input type="password" name="password" id="password" placeholder="请输入密码">
</tr>
</div>
<div>
<tr>
</tr>
<label>确认密码:</label>
<input type="password" name="relpassword" id="relpassword" placeholder="请确认密码">
</div>
<div>
<tr>
<label>电话号码:</label>
<input type="text" name="phone" id="phone" placeholder="请输入电话号码">
</tr>
</div>
<div>
<tr>
<label>电子邮件:</label>
<input type="text" name="email" id="email" placeholder="请输入电子邮件">
</tr>
</div>
<div>
<tr>
<button type="submit" onclick="return checkForm()">注册</button>
<button type="reset">重置</button>
<a href="login.jsp" target="_blank">登录</a>
</tr>
</div>
</form>
<script type="text/javascript">
function checkForm() {
var name = registerForm.name.value;
var pwd = registerForm.password.value;
var repwd = registerForm.relpassword.value;
//alert(name + pwd + repwd);
if (name == "" || name == null) {
alert("请输入用户名");
registerForm.name.focus();
return false;
} else if (pwd == "" || pwd == null) {
alert("请输入密码");
registerForm.password.focus();
return false;
} else if (repwd == "" || repwd == null) {
alert("请输入确认密码");
registerForm.relpassword.focus();
return false;
} else if (pwd != repwd) {
alert("两次密码输入不一致,请重新输入!");
registerForm.relpassword.focus();
return false;
}
alert('注册成功!');
return true;
}
</script>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/22
Time: 23:28
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%
// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>弹出信息</title>
<script type="text/javascript">
alert("您的密码是:${user.password}");
</script>
<style type="text/css">
h1{
text-align: center;
}
div{
text-align: center;
}
</style>
</head>
<body>
<h1>您的密码是:${user.password}</h1>
<div>
<a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
</body>
</html>
<%--
Created by IntelliJ IDEA.
User: shuijianshiqing
Date: 2020/5/22
Time: 23:14
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<%
// 获取绝对路径路径 ,开发项目一定要使用绝对路径,不然肯定出错
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":"
+ request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<base href="<%=basePath %>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>找回密码</title>
<style type="text/css">
h1{
text-align: center;
}
div{
text-align: center;
}
body{
background-color:antiquewhite;
}
</style>
</head>
<body>
<h1>找回密码</h1>
<div>
<a href="javascript: window.history.go(-1)">返回上一级</a>
</div>
<form action="dosearchPassword.jsp" method="post">
<table align="center">
<tr>
<td>请输入账号:</td>
<td><input type="text" name="name"/></td>
</tr>
<tr>
<td colspan="1"></td>
<td>
<input type="submit" value="提交">
<input type="reset" value="重置">
</td>
</tr>
</table>
</form>
</body>
</html>
package com.sjsq.po;
import java.io.Serializable;
/**
* @author shuijianshiqing
* @date 2020/5/19 22:40
*/
public class Book implements Serializable {
private static final long serialVersionUID = 1L;
private Integer bookid;
private String bookname;
private Double price;
private String author;
private String picture;
private String publish;
public static long getSerialVersionUID() {
return serialVersionUID;
}
public Integer getBookid() {
return bookid;
}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public String getBookname() {
return bookname;
}
public void setBookname(String bookname) {
this.bookname = bookname;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPicture() {
return picture;
}
public void setPicture(String picture) {
this.picture = picture;
}
public String getPublish() {
return publish;
}
public void setPublish(String publish) {
this.publish = publish;
}
@Override
public String toString() {
return "Book{" +
"bookid=" + bookid +
", bookname='" + bookname + '\'' +
", price=" + price +
", author='" + author + '\'' +
", picture='" + picture + '\'' +
", publish='" + publish + '\'' +
'}';
}
}
package com.sjsq.po;
/**
* @author shuijianshiqing
* @date 2020/5/29 19:34
*/
public class Card {
// 主键
private Integer id;
// 用户Id
private Integer userid;
// 用户名
private String username;
// 书名
private Integer bookid;
// 书本的数量
private int booknum;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public Integer getBookid() {
return bookid;
}
public void setBookid(Integer bookid) {
this.bookid = bookid;
}
public int getBooknum() {
return booknum;
}
public void setBooknum(int booknum) {
this.booknum = booknum;
}
@Override
public String toString() {
return "Card{" +
"id=" + id +
", userid=" + userid +
", username='" + username + '\'' +
", bookid=" + bookid +
", booknum=" + booknum +
'}';
}
}
package com.sjsq.po;
import java.io.Serializable;
/**
* @author shuijianshiqing
* @date 2020/5/19 22:19
* 用户的实体类
*/
public class User implements Serializable {
// 增加序列号,作用是反序列化的时候不会报错,切能进行IO的持久化
public static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String password;
private String email;
private String phone;
public static long getSerialVersionUID() {
return serialVersionUID;
}
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 getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
package com.sjsq.dao;
import com.sjsq.po.Book;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/20 23:13
* 图书信息接口
*/
public interface BookDao {
/**
* 查询图书信息
* @param sql
* @param arr
* @return
*/
public List<Book> select(String sql,Object[] arr);
/**
* 根据图书编号进行查询
* @param id
* @return
*/
public Book getBook(Integer id);
}
package com.sjsq.dao;
import com.sjsq.po.Book;
import com.sjsq.po.Card;
import com.sjsq.po.User;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/29 19:42
*/
public interface CardDao {
/**
* 通过用户Id获取用户的购物车
* @param id
* @return
*/
public List<Card> getCard(Integer id);
/**
* 添加用户信息和图书信息到用户的购物车
* @param user
* @param book
* @param booknum
* @return
*/
public boolean addCard(User user, Book book,Integer booknum);
/**
* 使用Book来获取booknum
* @param book
* @return
*/
public Integer getBookNum(Book book);
}
package com.sjsq.dao;
import com.sjsq.po.User;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/20 22:10
* 创建一个接口用于声明用户登录注册的方法
*/
public interface UserDao {
/**
* 用户登录
* @param user
* @return
*/
public User login(User user);
/**
* 用户注册
* @param user
* @return
*/
public boolean register(User user);
/**
* 查询用户信息
* @param sql
* @param arr
* @return
*/
public List<User> selectUser(String sql,Object arr[]);
}
package com.sjsq.service;
import com.sjsq.po.Book;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/20 23:37
* Book的Service层
*/
public interface BookService {
/**
* 查询图书信息
* @param book
* @return
*/
public List<Book> select(Book book);
/**
* 根据编号来查询
* @param book
* @return
*/
public Book getBook(Book book);
}
package com.sjsq.service;
import com.sjsq.po.Book;
import com.sjsq.po.Card;
import com.sjsq.po.User;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/29 23:00
*/
public interface CardService {
/**
* 获取用户的购物车
* @param id
* @return
*/
public List<Card> getCard(Integer id);
/**
* 添加用户信息和图书信息到用户的购物车
* @param user
* @param book
* @param booknum
* @return
*/
public boolean addCard(User user, Book book, Integer booknum);
/**
* 根据Book获取booknum
* @param book
* @return
*/
public Integer getBookNum(Book book);
}
package com.sjsq.service;
import com.sjsq.po.User;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/20 22:54
* Service层
*/
public interface UserService {
/**
* 用户查询的信息
* @param user
* @return
*/
public List<User> selectUser(User user);
}
package com.sjsq.service.impl;
import com.sjsq.dao.BookDao;
import com.sjsq.dao.impl.BookDaoImpl;
import com.sjsq.po.Book;
import com.sjsq.service.BookService;
import java.util.ArrayList;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/20 23:42
* Book的Service实现层
*/
public class BookServiceImpl implements BookService {
private BookDao dao = new BookDaoImpl();
/**
* 查询所有图书信息
* @param book
* @return
*/
@Override
public List<Book> select(Book book) {
StringBuffer sql = new StringBuffer("select * from book where 1 = 1 ");
List<Object> list = new ArrayList<Object>();
if(book != null){
// 根据bookid来查找对应的书籍
if(book.getBookid() != null && book.getBookid() != 0){
sql.append(" and bookid = ?");
list.add(book.getBookid());
}
}
return dao.select(sql.toString(),list.toArray());
}
/**
* 根据图书编号查询信息
* @param book
* @return
*/
@Override
public Book getBook(Book book) {
if(book.getBookid() != null && book.getBookid() != 0){
return dao.getBook(book.getBookid());
}
return null;
}
}
package com.sjsq.service.impl;
import com.sjsq.dao.CardDao;
import com.sjsq.dao.impl.CardDaoImpl;
import com.sjsq.po.Book;
import com.sjsq.po.Card;
import com.sjsq.po.User;
import com.sjsq.service.CardService;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/29 23:01
*/
public class CardServiceImpl implements CardService {
private CardDao dao = new CardDaoImpl();
@Override
public List<Card> getCard(Integer id) {
if(id != null){
return dao.getCard(id);
}
return null;
}
/**
* 添加信息到购物车
* @param user
* @param book
* @param booknum
* @return
*/
@Override
public boolean addCard(User user, Book book, Integer booknum) {
return dao.addCard(user,book,booknum);
}
/**
* 根据Book查询booknum
* @param book
* @return
*/
@Override
public Integer getBookNum(Book book) {
if(dao.getBookNum(book) == null){
return 0;
}
return dao.getBookNum(book);
}
}
package com.sjsq.service.impl;
import com.sjsq.dao.UserDao;
import com.sjsq.dao.impl.UserDaoImpl;
import com.sjsq.po.User;
import com.sjsq.service.UserService;
import java.util.ArrayList;
import java.util.List;
/**
* @author shuijianshiqing
* @date 2020/5/20 22:56
*/
public class UserServiceImpl implements UserService {
// 定义dao对象
private UserDao dao = new UserDaoImpl();
/**
* 查询用户信息
* @param user
* @return
*/
@Override
public List<User> selectUser(User user) {
StringBuffer sql = new StringBuffer("select * from user where 1 = 1 ");
List<Object> list = new ArrayList<Object>();
if(user != null){
// 按照姓名查询
if(user.getName() != null && !user.getName().equals("")){
sql.append(" and name = ?");
list.add(user.getName());
}
// 按照email查询
if(user.getEmail() != null && !user.getEmail().equals("")){
sql.append(" and email = ?");
list.add(user.getEmail());
}
}
// 交给dao层处理逻辑
return dao.selectUser(sql.toString(),list.toArray());
}
}
package com.sjsq.utils;
import java.sql.*;
/**
* @author shuijianshiqing
* @date 2020/5/19 23:08
* 数据交互层dao层
*/
public class BaseDao {
private static String driver = "com.mysql.cj.jdbc.Driver";
private static String url = "jdbc:mysql://localhost/book_management?serverTimezone=UTC";
private static String user = "root";
private static String password = "admin";
/**
* 连接数据库的方法
* @return
* @throws ClassNotFoundException
* @throws SQLException
*/
public static Connection getConnection() throws ClassNotFoundException, SQLException {
// 加载数据库驱动
Class.forName(driver);
Connection con = DriverManager.getConnection(url,user,password);
return con;
}
/**
* 关闭数据库的方法
* @param con
* @param ps
* @param rs
*/
public static void close(Connection con, PreparedStatement ps, ResultSet rs){
// 关闭资源,避免出现异常
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(con != null){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 设置增删改的方法
* @param sql
* @param arr
* @return
*/
public static boolean addUpdateDelete(String sql,Object[] arr){
Connection con = null;
PreparedStatement ps = null;
try {
// 第一步:连接数据库
con = BaseDao.getConnection();
// 第二步:预编译
ps = con.prepareStatement(sql);
// 第三步:设置值
if(arr != null && arr.length !=0){
for (int i = 0; i < arr.length; i++) {
ps.setObject(i+1,arr[i]);
}
}
int count = ps.executeUpdate();
if(count > 0){
return true;
}else{
return false;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
public static void main(String[] args){
try {
BaseDao.getConnection();
System.out.println("---测试数据库链接成功---");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
jquery-3.4.1.min.js
mysql-connector-java-8.0.15.jar
/*
Navicat Premium Data Transfer
Source Server : MySQL
Source Server Type : MySQL
Source Server Version : 80013
Source Host : localhost:3306
Source Schema : book_management
Target Server Type : MySQL
Target Server Version : 80013
File Encoding : 65001
Date: 01/06/2020 21:45:17
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for book
-- ----------------------------
DROP TABLE IF EXISTS `book`;
CREATE TABLE `book` (
`bookid` int(11) NOT NULL AUTO_INCREMENT,
`bookname` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`price` decimal(10, 2) NULL DEFAULT NULL,
`author` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`picture` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`publish` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`bookid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of book
-- ----------------------------
INSERT INTO `book` VALUES (1, '水浒传', 23.00, '施耐庵', NULL, '人民文学出版社');
INSERT INTO `book` VALUES (2, '西游记', 25.00, '吴承恩', NULL, '人民文学出版社');
INSERT INTO `book` VALUES (3, '红楼梦', 35.00, '曹雪芹', NULL, '人民文学出版社');
INSERT INTO `book` VALUES (4, '三国演义', 28.00, '罗贯中', NULL, '人民文学出版社');
-- ----------------------------
-- Table structure for card
-- ----------------------------
DROP TABLE IF EXISTS `card`;
CREATE TABLE `card` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`bookid` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`booknum` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 13 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of card
-- ----------------------------
INSERT INTO `card` VALUES (11, '1', '2001', '1', 2);
INSERT INTO `card` VALUES (12, '1', '2001', '3', 1);
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`password` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`email` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`phone` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 54 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '2001', '111111', '[email protected]', '31321');
INSERT INTO `user` VALUES (53, '张三', '1', '', '');
SET FOREIGN_KEY_CHECKS = 1;
手写日志太麻烦,所以后来出现了Log4j,Jsp中Java代码和Html混在一起,太不方便了,所以后来有了Servlet和各种框架,所以这也就是框架的意义。简单的事情重复做,做到一定的熟练度,把它抽象出来,形成公用的方法,就有了框架。还要数据连接,JDBC里面有很多重复性代码,后续就有了Mybatis等等。
备注1:关于如何搭建项目之前文章里面已经写过,请移步:
IDEA创建Web项目详细说明
备注2:若您想添加管理员增删改查功能,请移步:
图书管理系统-添加管理员增删改查功能
备注3:感谢别先生的博客,是参照他的博客实现的。
别先生的博客地址
备注4:要是侵犯到您的权益,请通知我删除此文章。
若您想运行此项目或者想添加额外的功能(有偿服务),可以联系我:
微信
QQ
鸡汤:牛奶会有的,面包会有的,技术都是千锤百炼出来的。