下面是演示:因为gif太大
下面是代码{前端开发}最后有福利哦
登陆加注册框loginReg.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登录注册</title>
<link href="css/public.css" rel="stylesheet" type="text/css" />
<style type="text/css">
html{
background-image: url(img/bg.png);
background-position: center center;
}
#layout{
width:800px;
height: 500px;
position: absolute;
top:50%;
left: 50%;
margin:-250px -400px;
}
#img,#loginReg{
float: left;
height: 100%;
position: relative;
}
#img{
width: 60%;
background-image: url(img/bg_left.png);
background-size: 100% auto;
background-position: center center;
opacity: 0.9;
}
#loginReg{
width: 40%;
box-sizing: border-box;
padding-top: 150px;
background: #fff;
}
#loginReg > form{
/*border: 1px solid red;*/
margin:0px auto 0px;
width: 250px;
position: relative;
}
#loginReg > form > h3{
width: 100%;
margin-bottom: 10px;
box-sizing: border-box;
line-height: 40px;
padding:0px 10px;
text-align: center;
font-size: 25px;
color:#3ab5fe;
letter-spacing: 3px;
}
#loginReg > form > input{
outline: none;
width: 100%;
margin-bottom: 10px;
box-sizing: border-box;
line-height: 40px;
padding:0px 10px;
border:1px solid #CCCCCC;
}
#loginReg > form > input[type="submit"]{
border:none;
background: #3daefe;
color:#fff;
}
#loginReg > form > #radio{
width:100%;
height: 36px;
/* background: #8888FF; */
margin-bottom: 10px;
}
#loginReg > form > #radio > .radios{
border:1px solid #CCCCCC;
height: 100%;
width: 80px;
margin-right: 10px;
text-align: center;
line-height: 36px;
float:left;
cursor: pointer;
}
#loginReg > form > #radio > .radios:nth-child(1){
background:#f99;
}
#loginReg >form > a{
text-align: right;width: 100%;
display: block;
margin-top: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<div id="layout">
<div id="img"></div>
<div id="loginReg">
<form id="login" action="*" method="post" >
<h3>登陆</h3>
<input type="text" name="userName" placeholder="账户" />
<input type="password" name="userPwd" placeholder="密码" />
<input type="submit" value="登录" />
<a class="button">没有账号?注册</a>
</form>
<form id="reg" action="*" method="post" style="display: none;">
<h3>注册</h3>
<input type="text" name="userName" placeholder="账户" />
<input type="password" name="userPwd" placeholder="密码" />
<input type="password" name="userPwd1" placeholder="确认密码" />
<div id="radio">
<div class="radios">男</div>
<div class="radios">女</div>
</div>
<input type="number" name="userAge" placeholder="年龄" />
<input type="number" name="userPhone" placeholder="请输入手机号" />
<input type="submit" value="注册" />
<a class="button">返回登陆</a>
</form>
</div>
</div>
</body>
<script type="text/javascript" src="js/vue.js" ></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
var bool = true;
$(".button").on("click",function(){
if(bool){
$("#login").css("display","none");
$("#reg").css("display","block");
$("#loginReg").css("padding-top","30px");
}else{
$("#login").css("display","block");
$("#reg").css("display","none");
$("#loginReg").css("padding-top","150px");
}
bool = !bool;
});
//提交登陆
$("#login").submit(function(){
var userName = $(this).find("input[name=userName]").val();
var userPwd = $(this).find("input[name=userPwd]").val();
if(userName == "" && userName == "undefined" && userPwd == "" && userPwd == "undefined"){
alert("不允许填写为空");
return false;
}
$.ajax({
url:"http://localhost:8080/Book/Login",
type:"post",
data:{
userName:userName,
userPwd:userPwd
},
dataType:"json",
success:function(data){
if(data.userId != '0'){
sessionStorage.setItem("userId",data.userId);
sessionStorage.setItem("userName",userName);
sessionStorage.setItem("userPwd",userPwd);
sessionStorage.setItem("userPosition",data.userPosition);
window.location.href="Home.html";
console.log(data);
}else{
alert("账号或密码错误");
}
}
});
return false;
})
//男女切换
var sex = "男";
$(".radios").on("click",function(){
$(".radios").css("background","none");
$(this).css("background","#f99");
sex = $(this).text();
console.log(sex);
});
//注册
$("#reg").submit(function(){
var userName = $(this).find("input[name=userName]").val();
var userPwd = $(this).find("input[name=userPwd]").val();
var userAge = $(this).find("input[name=userAge]").val();
var userPhone = $(this).find("input[name=userPhone]").val();
$.ajax({
url:"http://localhost:8080/Book/Reg",
type:"post",
data:{
userName:userName,
userPwd:userPwd,
userSex:sex,
userAge:userAge,
userPhone:userPhone
},
dataType:"text",
success:function(data){
if(data == "ok"){
alert("注册成功");
window.location.href="loginReg.html";
console.log(data);
}else{
alert("账号或密码错误");
console.log(data);
}
}
});
return false;
})
})
</script>
</html>
主页Home.html{列表,购物车,增删改查}
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
<link href="css/public.css" rel="stylesheet" type="text/css" />
<style type="text/css">
#title{
width: 100%;
line-height: 50px;
font-size: 26px;
letter-spacing: 2px;
text-align: center;
background:#ccc;
position: absolute;
top:0px;
left:0px;
}
#layout{
width:700px;
margin:100px auto 0px;
}
#layout > table{
width: 100%;
text-align: center;
line-height: 25px;
}
#shoppingTrolley{
width: 250px;
height: 100%;
position: fixed;
top:0px;
right: 0px;
background: #BBBBBB;
right:-250px;
transition: all 300ms ease;
}
#shoppingTrolley > #shoppingTrolleyBtn{
width: 25px;
height: 100%;
border:none;
position: absolute;
left:-25px;
outline: none;
font-size: 21px;
cursor: pointer;
}
#shoppingTrolley > h4{
width: 100%;
text-align: center;
line-height: 40px;
margin-top: 60px;
font-size: 24px;
letter-spacing: 4px;
}
#shoppingTrolley >#shoppingTrolleyContent{
width: 100%;
height: calc(100% - 100px);
overflow:auto;
box-sizing: border-box;
padding-bottom: 100px;
/* border:1px solid red; */
}
#shoppingTrolley >#shoppingTrolleyContent> .shoppingTrolley{
margin-top: 10px;
box-sizing: border-box;
margin:10px 10px 0px;
padding-top:10px;
border-radius: 10px;
background: #eee;
overflow: hidden;
}
#shoppingTrolley >#shoppingTrolleyContent> .shoppingTrolley > .shoppingTrolleyTitle{
font-size: 30px;
margin-left: 10px;
line-height: 40px;
margin-bottom: 10px;
float:left
}
#shoppingTrolley >#shoppingTrolleyContent> .shoppingTrolley > .shoppingTrolleyBody{
font-size: 16px;
margin-left: 80px;
line-height: 40px;
margin-bottom: 10px;
}
#shoppingTrolley >#shoppingTrolleyContent> .shoppingTrolley > button{
width: 100%;
height: 25px;
margin:0px;
padding:0px;
border:0px;
background: #88f;
color:#fff;
outline: none;
cursor: pointer;
}
/* 添加修改删除主框架 */
#BookAdd , #BookDel , #BookUpdate{
width: 100%;
height:100%;
background:rgba(0,0,0,0.7);
position: fixed;
top:0px;
left: 0px;
}
#BookAdd > .BookAdd , #BookUpdate > .BookUpdate{
/* border:1px solid red; */
width: 400px;
padding: 30px;
}
#BookDel > .BookDel{
/* border:1px solid red; */
width: 300px;
padding: 30px;
}
form{
margin:80px auto 0px;
width: 250px;
position: relative;
background: #CCCCCC;
}
form > h3{
width: 100%;
margin-bottom: 10px;
box-sizing: border-box;
line-height: 40px;
padding:0px 10px;
text-align: center;
font-size: 25px;
letter-spacing: 3px;
}
form > input{
outline: none;
width: 100%;
margin-bottom: 10px;
box-sizing: border-box;
line-height: 40px;
padding:0px 10px;
/*height: 40px;*/
border:none;
}
form > a{
text-align: right;width: 100%;
display: block;
margin-top: 10px;
cursor: pointer;
}
.BookAddBtn{
text-align:right;
width: 100%;
display: block;
margin: 10px 0px;
}
#users{
position: absolute;
right:30px;
top:0px;
font-size: 18px;
}
</style>
</head>
<body>
<!-- 标题 -->
<div id="title"><h3>图书管理系统</h3><span id="users"></span></div>
<!-- 列表 -->
<div id="layout">
<a href="#" class="BookAddBtn">添加图书</a>
<table cellspacing="0" border="0px" >
<tr>
<td>书名</td>
<td>作者</td>
<td>价格</td>
<td>是否借出</td>
<td>操作</td>
</tr>
<!-- <tr>
<td class="TabBookName">java</td>
<td class="TabBookAuthor">hwl </td>
<td class="TabBookPrice">453</td>
<td class="TabBookIsLoan">未借出</td>
<td><a href="#" ids="1" class="BookUpdateBtn">修改</a> | <a href="#" ids="1" class="BookDelBtn">删除</a></td>
</tr> -->
<tbody id="bookList">
</tbody>
</table>
</div>
<!-- 购物车 -->
<div id="shoppingTrolley">
<button id="shoppingTrolleyBtn">购物车</button>
<h4>购物车</h4>
<div id="shoppingTrolleyContent">
<!-- <div class="shoppingTrolley">
<div class="shoppingTrolleyTitle">java</div>
<div class="shoppingTrolleyBody">
作者:<span>hwl</span>
</div>
<button>未还</button>
</div> -->
</div>
</div>
<!-- 添加图书列表 -->
<div id="BookAdd" style="display: none;" >
<form class="BookAdd" action="*" method="post">
<h3>添加图书</h3>
<input type="text" name="bookName" placeholder="书名" />
<input type="text" name="bookAuthor" placeholder="作者" />
<input type="number" name="bookPrice" placeholder="价格" />
<input type="submit" value="确认添加" />
<a class="close">关闭</a>
</form>
</div>
<!-- 修改图书列表 -->
<div id="BookUpdate" style="display: none;">
<form class="BookUpdate" action="*" method="post">
<h3>修改图书</h3>
<input id="UpdateBookId" type="text" name="bookId" value="" style="display: none;" />
<input id="UpDateBookName" type="text" name="bookName" placeholder="书名" />
<input id="UpDateBookAuthor" type="text" name="bookAuthor" placeholder="作者" />
<input id="UpDateBookPrice" type="number" name="bookPrice" placeholder="价格" />
<input type="submit" value="确认修改" />
<a class="close">关闭</a>
</form>
</div>
<!-- 删除列表 -->
<div id="BookDel" style="display: none;" >
<form class="BookDel" action="*" method="post">
<h3>删除</h3>
<input id="DelBookName" type="button" value="书名" />
<input id="DelBookId" type="text" name="bookId" value="" style="display: none;" />
<input type="submit" value="确认删除" />
<a class="close">关闭</a>
</form>
</div>
</body>
<script src="js/vue.js"></script>
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
function load(){
// 购物车
var bool = true;
$("#shoppingTrolleyBtn").on("click",function(){
if(bool){
$("#shoppingTrolley").css("right","0px");
}else{
$("#shoppingTrolley").css("right","-250px");
}
bool = !bool;
});
//开启弹出框添加
//无获取
$(".BookAddBtn").on("click",function(){
$("#BookAdd").css("display","block");
});
//开启弹出框修改
//获取id 获取书名
$(".BookUpdateBtn").on("click",function(){
$("#UpDateBookName").val($(this).parent().prevAll(".TabBookName").text());
$("#UpDateBookAuthor").val($(this).parent().prevAll(".TabBookAuthor").text());
$("#UpDateBookPrice").val($(this).parent().prevAll(".TabBookPrice").text());
$("#UpdateBookId").val($(this).attr("ids"));
$("#BookUpdate").css("display","block");
});
//开启弹出框删除
//获取id 获取书名
$(".BookDelBtn").on("click",function(){
$("#DelBookName").val($(this).parent().prevAll(".TabBookName").text());
$("#DelBookId").val($(this).attr("ids"));
// console.log($("#DelBookId").val())
$("#BookDel").css("display","block");
});
//关闭弹出框
$(".close").on("click",function(){
$(this).parent().parent().css("display","none");
console.log("关闭");
});
//借书在渲染过后
$(".BookUserUpdate").on("click",function(){
var userId = sessionStorage.getItem("userId");
var userName = sessionStorage.getItem("userName");
var userName = sessionStorage.getItem("userName");
var userPwd = sessionStorage.getItem("userPwd");
var userPosition = sessionStorage.getItem("userPosition");
var bookId = $(this).attr("ids");
console.log(bookId);
$.ajax({
url:"http://localhost:8080/Book/BookUserUpdate",
type:"get",
data:{
userId:userId,
userName:userName,
userPwd:userPwd,
userPosition:userPosition,
bookId:bookId
},
dataType:"json",
success:function(data){
if(data.returnName == "ok"){
borrowBooks();
refresh();
}else{
alert(data.returnName);
}
}
});
return false;
});
//还书在渲染过后
$(".Give").on("click",function(){
var bookId = $(this).attr("dis");
console.log(bookId);
$.ajax({
url:"http://localhost:8080/Book/UserBookGive",
type:"get",
data:{
bookId:bookId,
},
dataType:"json",
success:function(data){
if(data.returnName == "ok"){
borrowBooks();
refresh();
}else{
alert(data.returnName);
}
}
});
return false;
});
$("#close").on("click",function(){
sessionStorage.setItem("userPosition","");
sessionStorage.setItem("userId","");
sessionStorage.getItem("userName","");
});
//图书列表变色
$("tr:even").css("background","#3daefe");
$("tr:even").css("color","#fff");
$("tr:odd").css("color","#3daefe");
$("#bookList").html();
}
//服务器交互
var userName = sessionStorage.getItem("userName");
var userPosition = sessionStorage.getItem("userPosition");
$("#users").html(userPosition != "" && userPosition != "undefined" && userPosition != null ?userName+":"+userPosition+"<a href='loginReg.html' id='close'>退出</a>":"未登录<a href='loginReg.html'>请登录</a>");
//获取图书列表
function refresh(){
$.ajax({
url:"http://localhost:8080/Book/BookList",
type:"get",
dataType:"json",
success:function(data){
// <tr>
//<td class="TabBookName">java</td>
//<td class="TabBookAuthor">hwl </td>
//<td class="TabBookPrice">453</td>
//<td class="TabBookIsLoan">未借出</td>
//
//</tr>
var timplate = "";
//判断是否是学生可借书
var userPosition = sessionStorage.getItem("userPosition");
console.log(data);
$.each(data,function(i){
timplate += "<tr><td class='TabBookName'>"+data[i].bookName+"</td>";
timplate += "<td class='TabBookAuthor'>"+data[i].bookAuthor+"</td>";
timplate += "<td class='TabBookPrice'>"+data[i].bookPrice+"</td>";
timplate += "<td class='TabBookIsLoan'>"+data[i].bookIsLoan+"</td>";
//判断是否是学生
if(userPosition == "学生"){
//判断书籍是否借出
if(data[i].bookIsLoan == "未借"){
timplate += "<td><a href='#' ids='"+data[i].bookId+"' class='BookUserUpdate'>借书</a></td></tr>";
}else{
timplate += "<td>不可借此书</td></tr>";
}
}else{
timplate += "<td><a href='#' ids='"+data[i].bookId+"' class='BookUpdateBtn'>修改</a> | <a href='#' ids='"+data[i].bookId+"' class='BookDelBtn'>删除</a></td></tr>";
}
});
$("#bookList").html(timplate);
//图书渲染过后的
load();
}
})
}
refresh();
//获取借书列表
function borrowBooks(){
// <div class='shoppingTrolley'> <div class='shoppingTrolleyTitle'>"+data[i].bookName+"</div>
// <div class="shoppingTrolleyBody">
// 作者:<span>hwl</span>
// </div>
// <button>已换</button></div>
var userId = sessionStorage.getItem("userId");
$.ajax({
url:"http://localhost:8080/Book/UserBookCart",
type:"get",
data:{
userId:userId
},
dataType:"json",
success:function(data){
var timplate = "";
console.log(data);
$.each(data,function(i){
timplate += "<div class='shoppingTrolley'> <div class='shoppingTrolleyTitle'>"+data[i].bookName+"</div>";
timplate += "<div class='shoppingTrolleyBody'>作者:<span>"+data[i].bookAuthor+"</span></div>";
timplate += "<button dis='"+data[i].bookId+"' class='Give'>要归还</button></div>";
});
$("#shoppingTrolleyContent").html(timplate);
//图书渲染过后的
load();
}
})
}
borrowBooks();
//添加图书
$(".BookAdd").submit(function(){
var THIS = this;
var bookName = $(this).find("input[name=bookName]").val();
var bookAuthor = $(this).find("input[name=bookAuthor]").val();
var bookPrice = $(this).find("input[name=bookPrice]").val();
console.log(bookName+""+bookAuthor+""+bookPrice);
$.ajax({
url:"http://localhost:8080/Book/BookAdd",
type:"get",
data:{
bookName:bookName,
bookAuthor:bookAuthor,
bookPrice:bookPrice,
},
dataType:"json",
success:function(data){
if(data.returnName == "插入成功"){
$(THIS).parent().css("display","none");
refresh();
}else{
alert(data.returnName);
}
}
});
return false;
});
//修改图书
$(".BookUpdate").submit(function(){
var THIS = this;
var userName = sessionStorage.getItem("userName");
var userPwd = sessionStorage.getItem("userPwd");
var userPosition = sessionStorage.getItem("userPosition");
var bookId = $(this).find("input[name=bookId]").val();
var bookName = $(this).find("input[name=bookName]").val();
var bookAuthor = $(this).find("input[name=bookAuthor]").val();
var bookPrice = $(this).find("input[name=bookPrice]").val();
console.log(bookId+":"+bookName+""+bookAuthor+""+bookPrice);
$.ajax({
url:"http://localhost:8080/Book/BookUpdate",
type:"get",
data:{
userName:userName,
userPwd:userPwd,
userPosition:userPosition,
bookId:bookId,
bookName:bookName,
bookAuthor:bookAuthor,
bookPrice:bookPrice,
},
dataType:"json",
success:function(data){
if(data.returnName == "ok"){
$(THIS).parent().css("display","none");
refresh();
}else{
alert(data.returnName);
}
}
});
return false;
});
//删除图书
$(".BookDel").submit(function(){
var THIS = this;
var userName = sessionStorage.getItem("userName");
var userPwd = sessionStorage.getItem("userPwd");
var userPosition = sessionStorage.getItem("userPosition");
var bookId = $(this).find("input[name=bookId]").val();
console.log(bookId);
$.ajax({
url:"http://localhost:8080/Book/BookDel",
type:"get",
data:{
userName:userName,
userPwd:userPwd,
userPosition:userPosition,
bookId:bookId
},
dataType:"json",
success:function(data){
if(data.returnName == "ok"){
$(THIS).parent().css("display","none");
refresh();
}else{
alert(data.returnName);
}
}
});
return false;
});
});
</script>
</html>
下面是后台开发由于比较多只留有用的
这是后端的目录结构
先从工具类开始名字就不用写了class里面有
package com.hwl.util;
import java.sql.*;
public class DButil {
public Connection conn(){
Connection conn = null;
try {
Driver.class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/book?useUnicode=true&characterEncoding=utf-8","root","");
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public void connClear(Connection conn , PreparedStatement ps , ResultSet rs){
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
/**
* 通用增删改
*/
public boolean dataUpdate(String sql , Object[] obj){
boolean bool = false;
Connection conn = conn();
PreparedStatement ps = null;
int rs = 0;
try {
ps = conn.prepareStatement(sql);
if (obj != null){
for (int i = 0;i<obj.length;i++){
ps.setObject(i+1 ,obj[i]);
}
}
rs = ps.executeUpdate();
if(rs != 0){
bool = true;
}else{
bool = false;
}
} catch (SQLException e) {
e.printStackTrace();
}
return bool;
}
/**
* 通用增删改
*/
public ResultSet dataQuery(String sql , Object[] obj){
Connection conn = conn();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
if (obj != null){
for (int i = 0;i<obj.length;i++){
ps.setObject(i+1 ,obj[i]);
}
}
rs = ps.executeQuery();
} catch (SQLException e) {
e.printStackTrace();
}
return rs;
}
}
下面是bean包里
package com.hwl.bean;
import com.alibaba.fastjson.JSON;
public class Book {
private int bookId = 0;//
private String bookName = "";//书名
private String bookAuthor= "";//出版社
private String bookPrice = "";//价格
private String bookIsLoan = "";//是否借书
public Book(int bookId, String bookName, String bookAuthor, String bookPrice, String bookIsLoan) {
this.bookId = bookId;
this.bookName = bookName;
this.bookAuthor = bookAuthor;
this.bookPrice = bookPrice;
this.bookIsLoan = bookIsLoan;
}
@Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", bookAuthor='" + bookAuthor + '\'' +
", bookPrice='" + bookPrice + '\'' +
", bookIsLoan='" + bookIsLoan + '\'' +
'}';
}
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 String getBookPrice() {
return bookPrice;
}
public void setBookPrice(String bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookIsLoan() {
return bookIsLoan;
}
public void setBookIsLoan(String bookIsLoan) {
this.bookIsLoan = bookIsLoan;
}
}
package com.hwl.bean;
public class User {
private int userId = 0;
private String userName = "";
private String userPwd = "";
private String userSex = "";
private String userAge = "";
private String userPhone = "";
private String userPosition = "";
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 getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public String getUserAge() {
return userAge;
}
public void setUserAge(String userAge) {
this.userAge = userAge;
}
public String getUserPhone() {
return userPhone;
}
public void setUserPhone(String userPhone) {
this.userPhone = userPhone;
}
public String getUserPosition() {
return userPosition;
}
public void setUserPosition(String userPosition) {
this.userPosition = userPosition;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userPwd='" + userPwd + '\'' +
", userSex='" + userSex + '\'' +
", userAge='" + userAge + '\'' +
", userPhone='" + userPhone + '\'' +
", userPosition='" + userPosition + '\'' +
'}';
}
}
这个用于返回指令用的在json里面使用
package com.hwl.bean;
public class Return {
private String returnName = "";
public Return(String returnName) {
this.returnName = returnName;
}
public String getReturnName() {
return returnName;
}
public void setReturnName(String returnName) {
this.returnName = returnName;
}
}
下面是service层
package com.hwl.service;
import com.hwl.bean.Book;
import java.util.List;
public interface BookDao {
/**
* 查找图书
*/
public List<Book> listBook();
/**
* 插入图书/默认未借
*/
public Boolean BookAdd(Book book);
/**
* 更新图书/是否借出
* 判断
* 如果是 更新图书则 不写入是否借出选项别的都操作
* 如果是 借出图书则 写入是否借出选项别的不操作
*/
public Boolean BookUpdate(Book book);
/**
* 删除图书
*/
public Boolean BookDel(Book book);
/**
* bookGive归还图书
*/
public Boolean bookGive(Book book);
}
package com.hwl.service;
import com.hwl.bean.Book;
import com.hwl.bean.User;
import java.util.List;
public interface UserDao {
/**
* 登陆返回布尔值内容
* @param user
* @return
*/
User login(User user);
/**
* 注册添加返回布尔值
*/
Boolean reg(User user);
/**
* 修改密码返回布尔值
*/
Boolean pwd(User user);
/**
* 删除用户
*/
Boolean userDel(User user);
/**
* 查找用户
*/
public List<User> UserList();
/**
* 登陆返回布尔值内容
* 插入用户,插入密码,插入职位
* @param user
* @return
*/
Boolean verification(User user);
/**
* 根据id查找当前借书的列表
*/
public List<Book> listBook(User user);
/**
* 根据id查找当前用户明
*/
public String UserName(String userId);
}
下面是实现类
package com.hwl.service.impl;
import com.hwl.bean.Book;
import com.hwl.service.BookDao;
import com.hwl.service.UserDao;
import com.hwl.util.DButil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BookDaoImpl implements BookDao {
/**
* 查找所有图书
* @return
*/
@Override
public List<Book> listBook() {
UserDao userDao = new UserDaoImpl();
List<Book> listBook = new ArrayList<Book>();
String sql = "select * from book";
DButil dbutil = new DButil();
ResultSet ps = dbutil.dataQuery(sql , null);
try {
while(ps.next()){
Book book = new Book(
ps.getInt(1),
ps.getString(2),
ps.getString(3),
ps.getString(4),
userDao.UserName(ps.getString(5)) != null && !userDao.UserName(ps.getString(5)).equals("")?userDao.UserName(ps.getString(5)):ps.getString(5)
);
listBook.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
dbutil.connClear(null,null,ps);
}
return listBook;
}
@Override
public Boolean BookAdd(Book book) {
Boolean bool = false;
String sql = " insert into book(bookName,bookAuthor,bookPrice,bookIsLoan) values(?,?,?,?)";
DButil dbutil = new DButil();
Object[] obj = new Object[4];
obj[0] = book.getBookName();
obj[1] = book.getBookAuthor();
obj[2] = book.getBookPrice();
obj[3] = "未借";
bool = dbutil.dataUpdate(sql , obj);
return bool;
}
@Override
public Boolean BookUpdate(Book book) {
Boolean bool = false;
String sql = "";
Object[] obj = null;
DButil dbutil = new DButil();
if(book.getBookIsLoan() != null){
//借选项 写入书的id 修改书的借出内容是借书人的id
sql = " update book set bookIsLoan=? where bookId = ?";
obj = new Object[2];
obj[0] = book.getBookIsLoan();
obj[1] = book.getBookId();
}else{
//修改选项 没有getBookIsLoan的内容
sql = " update book set bookName = ? , bookAuthor = ? , bookPrice = ? where bookId = ?";
obj = new Object[4];
obj[0] = book.getBookName();
obj[1] = book.getBookAuthor();
obj[2] = book.getBookPrice();
obj[3] = book.getBookId();
}
bool = dbutil.dataUpdate(sql , obj);
return bool;
}
@Override
public Boolean BookDel(Book book) {
Boolean bool = false;
String sql = " delete from book where bookId = ?";
DButil dbutil = new DButil();
Object[] obj = new Object[1];
obj[0] = book.getBookId();
bool = dbutil.dataUpdate(sql , obj);
return bool;
}
@Override
public Boolean bookGive(Book book) {
Boolean bool = false;
String sql = "update book set bookIsLoan = '未借' where bookId = ? ";
Object[] obj = new Object[1];
obj[0] = book.getBookId();;
DButil dbutil = new DButil();
bool = dbutil.dataUpdate(sql,obj);
return bool;
}
}
package com.hwl.service.impl;
import com.hwl.bean.Book;
import com.hwl.bean.User;
import com.hwl.service.UserDao;
import com.hwl.util.DButil;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class UserDaoImpl implements UserDao {
@Override
public User login(User user) {
String sql = "select * from user where userName = ? and userPwd = ?";
Object[] obj = new Object[2];
obj[0] = user.getUserName();
obj[1] = user.getUserPwd();
DButil dbutil = new DButil();
ResultSet rs = dbutil.dataQuery(sql,obj);
while(true){
try {
if (!rs.next()) break;
} catch (SQLException e) {
e.printStackTrace();
}
try {
user.setUserId(rs.getInt(1));
user.setUserName(rs.getString(2));
user.setUserPwd(rs.getString(3));
user.setUserSex(rs.getString(4));
user.setUserAge(rs.getString(5));
user.setUserPhone(rs.getString(6));
user.setUserPosition(rs.getString(7));
} catch (SQLException e) {
e.printStackTrace();
}
}
dbutil.connClear(null,null,rs);
return user;
}
@Override
public Boolean reg(User user) {
Boolean bool = false;
String sql = " insert into user(userName,userPwd,userSex,userAge,userPhone,userPosition) values(?,?,?,?,?,?)";
Object[] obj = new Object[6];
obj[0] = user.getUserName();
obj[1] = user.getUserPwd();
obj[2] = user.getUserSex();
obj[3] = user.getUserAge();
obj[4] = user.getUserPhone();
obj[5] = "学生";
DButil dbutil = new DButil();
bool = dbutil.dataUpdate(sql,obj);
return bool;
}
@Override
public Boolean pwd(User user) {
Boolean bool = false;
String sql = "update user set userPwd = ? where userName = ? ";
Object[] obj = new Object[2];
obj[0] = user.getUserPwd();
obj[1] = user.getUserName();
DButil dbutil = new DButil();
bool = dbutil.dataUpdate(sql,obj);
return bool;
}
@Override
public Boolean userDel(User user) {
Boolean bool = false;
String sql = " delete from user where userId = ?";
DButil dbutil = new DButil();
Object[] obj = new Object[1];
obj[0] = user.getUserId();
bool = dbutil.dataUpdate(sql , obj);
return bool;
}
@Override
public List<User> UserList() {
List<User> UserList = new ArrayList<User>();
String sql = "select * from user";
DButil dbutil = new DButil();
ResultSet ps = dbutil.dataQuery(sql , null);
try {
while(ps.next()){
User user = new User();
user.setUserId(ps.getInt(1));
user.setUserName(ps.getString(2));
user.setUserPwd(ps.getString(3));
user.setUserSex(ps.getString(4));
user.setUserAge(ps.getString(5));
user.setUserPhone(ps.getString(6));
user.setUserPosition(ps.getString(7));
UserList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
dbutil.connClear(null,null,ps);
}
return UserList;
}
@Override
public Boolean verification(User user) {
Boolean bool = false;
String sql = "select * from user where userName = ? and userPwd = ? and userPosition = ?";
Object[] obj = new Object[3];
obj[0] = user.getUserName();
obj[1] = user.getUserPwd();
obj[2] = user.getUserPosition();
DButil dbutil = new DButil();
ResultSet rs = dbutil.dataQuery(sql,obj);
while(true){
try {
if (!rs.next()) break;
} catch (SQLException e) {
e.printStackTrace();
}
bool = true;
}
dbutil.connClear(null,null,rs);
return bool;
}
@Override
public List<Book> listBook(User user) {
List<Book> listBook = new ArrayList<Book>();
String sql = " select * from book where bookIsLoan=?";
DButil dbutil = new DButil();
Object[] obj = new Object[1];
obj[0] = user.getUserId();
ResultSet ps = dbutil.dataQuery(sql , obj);
try {
while(ps.next()){
Book book = new Book(
ps.getInt(1),
ps.getString(2),
ps.getString(3),
ps.getString(4),
ps.getString(5)
);
listBook.add(book);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
dbutil.connClear(null,null,ps);
}
return listBook;
}
@Override
public String UserName(String userId) {
String sql = "select userName from user where userId = ?";
DButil dbutil = new DButil();
Object[] obj = new Object[1];
obj[0] = userId;
ResultSet ps = dbutil.dataQuery(sql , obj);
User user = new User();
try {
while(ps.next()){
user.setUserName(ps.getString(1));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
dbutil.connClear(null,null,ps);
}
return user.getUserName();
}
}
下面就是核心了和html前端交互的servlet
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.Book;
import com.hwl.bean.Return;
import com.hwl.bean.User;
import com.hwl.service.BookDao;
import com.hwl.service.UserDao;
import com.hwl.service.impl.BookDaoImpl;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/BookAdd")
public class BookAdd extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//删除图书
/**
* ! 添加书籍没有权限问题
* 添加书名
* 添加作者
* 添加价格
*/
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String bookName = request.getParameter("bookName");
String bookAuthor = request.getParameter("bookAuthor");
String bookPrice = request.getParameter("bookPrice");
if(
bookName != null && !bookName.equals("") &&
bookAuthor != null && !bookAuthor.equals("")&&
bookPrice != null && !bookPrice.equals("")
){
BookDao bookDao = new BookDaoImpl();
Book book = new Book(0,bookName,bookAuthor,bookPrice,null);
if (bookDao.BookAdd(book)){
out.print(JSON.toJSONString(new Return("插入成功")));
}else{
out.print(JSON.toJSONString(new Return("插入失败")));
}
}else{
out.print(JSON.toJSONString(new Return("内容未插完整")));
}
// BookDao bookDao = new BookDaoImpl();
//
// out.print(bookDao);
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.Book;
import com.hwl.bean.Return;
import com.hwl.bean.User;
import com.hwl.service.BookDao;
import com.hwl.service.UserDao;
import com.hwl.service.impl.BookDaoImpl;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/BookDel")
public class BookDel extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//删除图书
/**
* 删除书 人员的姓名
* 删除书人员的密码
* 删除书人员的职位
* 删除书的id
*/
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
String userPosition = request.getParameter("userPosition");
Integer bookId =new Integer(request.getParameter("bookId")) ;
System.out.print(userName+":"+userPwd+":"+userPosition+":"+bookId);
if(
userName != null && !userName.equals("") &&
userPwd != null && !userPwd.equals("")&&
userPosition != null && !userPosition.equals("")
){
User user = new User();
user.setUserName(userName);
user.setUserPosition(userPosition);
user.setUserPwd(userPwd);
UserDao userDao = new UserDaoImpl();
if(userDao.verification(user)){
if(userPosition.equals("管理员") || userPosition.equals("超级管理员")){
BookDao bookDao = new BookDaoImpl();
Book book = new Book(new Integer(bookId),null,null,null,null);
if (bookDao.BookDel(book)){
out.print(JSON.toJSONString(new Return("ok")));
}else{
out.print(JSON.toJSONString(new Return("删除失败")));
};
}else{
out.print(JSON.toJSONString(new Return("不允许删除")));
}
}else{
out.print(JSON.toJSONString(new Return("权限不合格:违规操作")));
}
}else{
out.print(JSON.toJSONString(new Return("未登录")));
}
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.User;
import com.hwl.service.BookDao;
import com.hwl.service.UserDao;
import com.hwl.service.impl.BookDaoImpl;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/BookList")
public class BookList extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//图书列表
/**
* 打印所有图书
*/
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
BookDao bookDao = new BookDaoImpl();
out.print(JSON.toJSONString(bookDao.listBook()));
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.Book;
import com.hwl.bean.Return;
import com.hwl.bean.User;
import com.hwl.service.BookDao;
import com.hwl.service.UserDao;
import com.hwl.service.impl.BookDaoImpl;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/BookUpdate")
public class BookUpdate extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**通过id修改图书必须是管理员
* 获取用户姓名
* 获取用户密码
* 获取用户职位
*
*
* 通过图书id修改图书
* 图书名
* 图书作者
* 图书价钱
*/
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String userPwd = request.getParameter("userPwd");
String userName = request.getParameter("userName");
String userPosition = request.getParameter("userPosition");
String bookId = request.getParameter("bookId");
String bookName = request.getParameter("bookName");
String bookAuthor = request.getParameter("bookAuthor");
String bookPrice = request.getParameter("bookPrice");
System.out.println(userPwd+":"+userName+":"+userPosition+":"+bookId+":"+bookName+":"+bookAuthor+":"+bookPrice);
if(
userName != null && !userName.equals("") &&
userPwd != null && !userPwd.equals("") &&
userPosition != null && !userPosition.equals("")&&
bookId != null && !bookId.equals("") &&
bookName != null && !bookName.equals("") &&
bookAuthor != null && !bookAuthor.equals("")&&
bookPrice != null && !bookPrice.equals("")
){
User user = new User();
user.setUserName(userName);
user.setUserPosition(userPosition);
user.setUserPwd(userPwd);
UserDao userDao = new UserDaoImpl();
//判断是否符合要求
if(userDao.verification(user)){
BookDao bookDao = new BookDaoImpl();
Book book = new Book(new Integer(bookId),bookName,bookAuthor,bookPrice,null);
if (bookDao.BookUpdate(book)){
out.print(JSON.toJSONString(new Return("ok")));
}else{
out.print(JSON.toJSONString(new Return("修改失败")));
}
}else{
out.print(JSON.toJSONString(new Return("权限不合格")));
}
}else{
out.print(JSON.toJSONString(new Return("未登录")));
}
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.Book;
import com.hwl.bean.Return;
import com.hwl.bean.User;
import com.hwl.service.BookDao;
import com.hwl.service.UserDao;
import com.hwl.service.impl.BookDaoImpl;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/BookUserUpdate")
public class BookUserUpdate extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//修改图书是否借出借出的id
/**
* 权限
* 借书人的id
* 借书人的账户
* 借书人的密码
* 借书人的职位
* 被借书的id
*/
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String userId = request.getParameter("userId");
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
String userPosition = request.getParameter("userPosition");
String bookId =request.getParameter("bookId");
if(
userId != null && !userId.equals("") &&
userName != null && !userName.equals("") &&
userPwd != null && !userPwd.equals("")&&
userPosition != null && !userPosition.equals("")
){
User user = new User();
user.setUserName(userName);
user.setUserPosition(userPosition);
user.setUserPwd(userPwd);
UserDao userDao = new UserDaoImpl();
if(userDao.verification(user)){
BookDao bookDao = new BookDaoImpl();
Book book = new Book(new Integer(bookId),null,null,null,userId);
if (bookDao.BookUpdate(book)){
out.print(JSON.toJSONString(new Return("ok")));
}else{
out.print(JSON.toJSONString(new Return("请求借书失败")));
};
}else{
out.print(JSON.toJSONString(new Return("权限不合格")));
}
}else{
out.print(JSON.toJSONString(new Return("未登录")));
}
// BookDao bookDao = new BookDaoImpl();
//
// out.print(bookDao);
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.Book;
import com.hwl.bean.Return;
import com.hwl.service.BookDao;
import com.hwl.service.impl.BookDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/Code")
public class Code extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/**验证码
*/
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
// ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 100, 4, 4);
// //ShearCaptcha captcha = new ShearCaptcha(200, 100, 4, 4);
// //图形验证码写出,可以写出到文件,也可以写出到流
// captcha.write("d:/shear.png");
// //验证图形验证码的有效性,返回boolean值
// captcha.verify("1234");
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.User;
import com.hwl.service.UserDao;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/Login")
public class Login extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
System.out.print("登陆"+userName+":"+userPwd);
User user = new User();
if(userName != null && !userName.equals("") &&
userPwd != null && !userPwd.equals("")){
user.setUserName(userName);
user.setUserPwd(userPwd);
UserDao userDao = new UserDaoImpl();
user = userDao.login(user);
if (user.getUserId() != 0){
out.print(JSON.toJSONString(user));
}else{
out.print(JSON.toJSONString(user));
}
}else{
out.print(JSON.toJSONString(user));
}
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.Return;
import com.hwl.bean.User;
import com.hwl.service.UserDao;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/Pwd")
public class Pwd extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
System.out.print("修改密码:"+userName+":"+userPwd);
User user = new User();
Boolean bool = false;
Map<String,Object> mapBool = new HashMap<String,Object>();
if(userName != null && !userName.equals("") &&
userPwd != null && !userPwd.equals("")){
user.setUserName(userName);
user.setUserPwd(userPwd);
UserDao userDao = new UserDaoImpl();
bool = userDao.pwd(user);
if (bool){
out.print(JSON.toJSONString(new Return("ok")));
}else{
out.print(JSON.toJSONString(new Return("登陆失败")));
}
}else{
out.print(JSON.toJSONString(new Return("未输入内容")));
}
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.User;
import com.hwl.service.UserDao;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/Reg")
public class Reg extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
String userSex = request.getParameter("userSex");
String userAge = request.getParameter("userAge");
String userPhone = request.getParameter("userPhone");
System.out.print("注册:"+userName+":"+userPwd+":"+userSex+":"+userAge+":"+userPhone);
User user = new User();
Boolean bool = false;
Map<String,Object> mapBool = new HashMap<String,Object>();
if(userName != null && !userName.equals("") &&
userPwd != null && !userPwd.equals("") &&
userSex != null && !userSex.equals("") &&
userAge != null && !userAge.equals("") &&
userPhone != null && !userPhone.equals("")){
user.setUserName(userName);
user.setUserPwd(userPwd);
user.setUserSex(userSex);
user.setUserAge(userAge);
user.setUserPhone(userPhone);
UserDao userDao = new UserDaoImpl();
bool = userDao.reg(user);
if (bool){
out.print("ok");
}else{
out.print("error");
}
}else{
out.print("error");
}
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.Return;
import com.hwl.bean.User;
import com.hwl.service.BookDao;
import com.hwl.service.UserDao;
import com.hwl.service.impl.BookDaoImpl;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/UserBookCart")
public class UserBookCart extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//图书列表
/**
* 打印当前用户所借的书
* 插入userID
*/
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String userId = request.getParameter("userId");
if (userId!=null && !userId.equals("")){
User user = new User();
user.setUserId(new Integer(userId));
UserDao userDao = new UserDaoImpl();
out.print(JSON.toJSONString(userDao.listBook(user)));
}else{
out.print(JSON.toJSONString(new Return("当前未登陆")));
}
out.flush();
out.close();
}
}
package com.hwl.servletAjax;
import com.alibaba.fastjson.JSON;
import com.hwl.bean.Book;
import com.hwl.bean.Return;
import com.hwl.bean.User;
import com.hwl.service.BookDao;
import com.hwl.service.UserDao;
import com.hwl.service.impl.BookDaoImpl;
import com.hwl.service.impl.UserDaoImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/UserBookGive")
public class UserBookGive extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setHeader("Access-Control-Allow-Origin","*");
PrintWriter out = response.getWriter();
String bookId = request.getParameter("bookId");
System.out.print("归还图书:"+bookId+":");
Boolean bool = false;
Map<String,Object> mapBool = new HashMap<String,Object>();
if(bookId != null && !bookId.equals("")){
Book book = new Book(new Integer(bookId),null,null,null,null);
BookDao bookDao = new BookDaoImpl();
bool = bookDao.bookGive(book);
if (bool){
out.print(JSON.toJSONString(new Return("ok")));
}else{
out.print(JSON.toJSONString(new Return("没有登陆")));
}
}else{
out.print(JSON.toJSONString(new Return("未输入内容")));
}
}
}
我发现一个问题就是每次写完准备测试时就是tomcat启动非常慢影响效率有没有什么办法可以快速测试有解决办法的小伙伴欢迎在下面评论小编不易给个赞吧