基于javaweb+mysql的ssm+maven图书借阅管理系统(管理员、普通用户)(java+jsp+ssm+javabean+mysql+tomcat+javascript)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SSM+Maven图书借阅管理系统(管理员、普通用户)(java+jsp+ssm+javabean+mysql+tomcat+javascript)
首页:
管理员:
admin 123456
用户:
user1 123456
user2 123456
public class LoginInterceptor implements HandlerInterceptor {
/**
* <h2>前置处理</h2>
* */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
//若登录信息为空,即 用户未登录
Object userLoginInfo = request.getSession().getAttribute("USER_LOGIN_INFO");
if(userLoginInfo == null){
// 用户未登录,重定向到登录页面
String uri = request.getRequestURI();//获取请求来源地址
String query = request.getQueryString();//获取查询字符串
session.setAttribute("REQUEST_URI", uri + (query == null ? "" : "?" + query));
response.sendRedirect("/login?msg="+ URLEncoder.encode("你还没有登陆,请先登录。。。", "UTF-8"));
return false;
}
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}
/**
* <h1>管理员拦截器</h1>
* 限制用户,只有管理员可以访问
*/
@Component
public class RootInterceptor implements HandlerInterceptor {
/**
* <h2>前置处理</h2>
* */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
UserLoginInfo userLoginInfo =(UserLoginInfo)request.getSession().getAttribute("USER_LOGIN_INFO");
//若登录信息为空,即 用户未登录,类别不为零则 非管理员
if(userLoginInfo == null||userLoginInfo.getType()!=0){
//无权限则返回主页
response.sendRedirect("/main"+"?msg="+ URLEncoder.encode("只有管理员才可以访问", "UTF-8"));//重定向到上一个页面
return false;
}
/**
* <h1>登录拦截器</h1>
* 限制用户,不登陆无法访问的页面
*/
@Component
public class LoginInterceptor implements HandlerInterceptor {
/**
* <h2>前置处理</h2>
* */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
//若登录信息为空,即 用户未登录
Object userLoginInfo = request.getSession().getAttribute("USER_LOGIN_INFO");
if(userLoginInfo == null){
// 用户未登录,重定向到登录页面
String uri = request.getRequestURI();//获取请求来源地址
String query = request.getQueryString();//获取查询字符串
session.setAttribute("REQUEST_URI", uri + (query == null ? "" : "?" + query));
response.sendRedirect("/login?msg="+ URLEncoder.encode("你还没有登陆,请先登录。。。", "UTF-8"));
return false;
}
return HandlerInterceptor.super.preHandle(request, response, handler);
}
}
/**
* <h1>管理员拦截器</h1>
* 限制用户,只有管理员可以访问
*/
@Component
public class RootInterceptor implements HandlerInterceptor {
/**
* <h2>前置处理</h2>
* */
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
Result newBorrowing(/*@DateTimeFormat(pattern = "YYYY-MM-DD'T'HH:mm")*/ @RequestBody Borrowing borrowing){
// System.out.println(borrowing);//test
boolean isBorrowingSucceed = true;
String message="";
try {
borrowingServices.borrowing(borrowing);
}
catch (Exception e){
isBorrowingSucceed=false;
message=e.getMessage();
}
if(isBorrowingSucceed){
Result succeed = ResultFactory.getSucceed();
succeed.addData("isBorrowingSucceed",isBorrowingSucceed);
return succeed;
}else{
return ResultFactory.getFail(message);
}
}
}
@Controller
@RequestMapping("/searchcontroller")
/**指定控制器响应url的请求**/
public class SearchController {
@RequestMapping("/page")
String page() {
return "/WEB-INF/page/bookManagement/bookManagementMain.jsp";
}
@RequestMapping("/bookAdd")
String bookAdd(RedirectAttributes redirectAttributes, Model model) {
@Controller
@RequestMapping("/searchcontroller")
/**指定控制器响应url的请求**/
public class SearchController {
@RequestMapping("/page")
String page() {
return "/WEB-INF/page/bookManagement/bookManagementMain.jsp";
}
@RequestMapping("/bookAdd")
String bookAdd(RedirectAttributes redirectAttributes, Model model) {
model.addAttribute("msg", redirectAttributes.getAttribute("msg"));
return "/WEB-INF/page/bookManagement/bookAdd.jsp";
}
@RequestMapping("/bookDelete")
String bookDelete() {
return "/WEB-INF/page/bookManagement/bookDelete.jsp";
}
@RequestMapping("/bookUpdate")
String bookChange() {
return "/WEB-INF/page/bookManagement/bookUpdate.jsp";
}
@RequestMapping("/bookQuery")
String bookQuery() {
return "/WEB-INF/page/bookManagement/bookQuery.jsp";
}
@RequestMapping("/UpdateDate/{bookId}")
String bookUpdateSubmit(@PathVariable int bookId,Model model) {
Book book = bookManService.getBookById(bookId);
model.addAttribute("book",book);
return "/WEB-INF/page/bookManagement/bookUpdateSubmit.jsp";
}
@Autowired
BookManService bookManService;
@PostMapping("/addBook")
String addBook(/*@RequestBody*/ Book book, RedirectAttributes redirectAttributes) {
int i = bookManService.addBook(book);
/**
* <h1>注册控制器</h1>
*/
@Controller
@RequestMapping("/register")
public class RegisterController {
@Autowired
UserService userService;
@GetMapping
String mainPage(){
return "/WEB-INF/page/Register.jsp";
}
@ResponseBody
@GetMapping("/isUserNameNull")
Result getName(String userName){
Result result = ResultFactory.getSucceed();
result.addData("isNull",userService.getUserByUserName(userName) == null);
return result;
}
@ResponseBody
@GetMapping("/isEmailNull")
Result isEmailNull(String email){
Result result = ResultFactory.getSucceed();
result.addData("isNull",userService.getUserByEmail(email)==null);
return result;
}
@ResponseBody
@GetMapping("/getAddress/{id}")
Result getAddressList(@PathVariable int id){
Result result = ResultFactory.getSucceed();
result.addData(userService.getAddressById(id));
return result;
}
@PostMapping("/execute")
String register(User user,HttpSession session){
if (userService.newUser(user)) {
session.setAttribute("msg","注册成功,请先登录。");
String message="";
try {
borrowingServices.borrowing(borrowing);
}
catch (Exception e){
isBorrowingSucceed=false;
message=e.getMessage();
}
if(isBorrowingSucceed){
Result succeed = ResultFactory.getSucceed();
succeed.addData("isBorrowingSucceed",isBorrowingSucceed);
return succeed;
}else{
return ResultFactory.getFail(message);
}
}
}
@Controller
@RequestMapping("/searchcontroller")
/**指定控制器响应url的请求**/
public class SearchController {
@RequestMapping("/page")
String page() {
return "/WEB-INF/page/bookManagement/bookManagementMain.jsp";
}
@RequestMapping("/bookAdd")
String bookAdd(RedirectAttributes redirectAttributes, Model model) {
model.addAttribute("msg", redirectAttributes.getAttribute("msg"));
return "/WEB-INF/page/bookManagement/bookAdd.jsp";
}
}
/**
* <h2>登陆执行控制器</h2>
*/
@PostMapping("/execute")
String login(String userName, String userPassword,HttpSession session){
//处理字符串
if(userName!=null) userName=userName.trim();
if(userPassword!=null) userPassword=userPassword.trim();
//校验是否有空字符串
if (!StringDispose.isNotNull(userName)||!StringDispose.isNotNull(userPassword)) {
return loginMistake(session,"用户名或密码为空");
}
//获取密码
String password = userService.getPasswordByUserName(userName);
if(!StringDispose.isNotNull(password)){ //判断用户是否存在
return loginMistake(session,"用户名不存在");
}else if(password.equals(userPassword)){ //判断密码是否正确
//将User信息添加到session中
session.setAttribute("USER_LOGIN_INFO",userService.getUserLoginInfoByUserName(userName));
session.setAttribute("msg","登陆成功");
String requestUri =(String) session.getAttribute("REQUEST_URI");
System.out.println("REQUEST_URI="+requestUri);//tests
//返回之前的页面
if(requestUri!=null){
session.setAttribute("REQUEST_URI",null);
return "redirect:"+requestUri;
}
return "redirect:/main";
}else{//密码错误处理
return loginMistake(session,"密码错误!");
}
}
/**
* <h2>登陆错误处理</h2>
*/
private String loginMistake(HttpSession session,String msg){
session.setAttribute("msg",msg);
return "redirect:/login";
}
redirectAttributes.addFlashAttribute("msg", "删除成功");
return "redirect:/reader/Page";
}
}
@RequestMapping("/Page")
String Page()
{
return "/WEB-INF/page/userManagement/User.jsp";
}
@ResponseBody
@RequestMapping("/queryuser")
public List<User> queryuser(String keywords){
return readerService.queryuser(keywords);
}
}
}
}
@ResponseBody
@RequestMapping ("/queryBook")
public List<Book> queryBook(String keyword) {
return bookManService.queryBook(keyword);
}
@RequestMapping("/test")
public String Test(){return "/WEB-INF/page/bookManagement/test.jsp";}
@RequestMapping("/UpdateBook")
public String UpdateBook(Book book,Model model) {
// 更新图书信息
// try {
bookManService.bookUpdate(book);
// } catch (Exception e) {
// model.addAttribute("msg",e);
// return "redirect:/searchcontroller/UpdateDate/"+book.getBookId();
// }
model.addAttribute("msg","图书修改成功!");
return "redirect:/searchcontroller/bookUpdate";
}
}
/**
* <h1>登录控制器</h1>
*/
/**
* 对于管理员账号登录后进行的操作
*
*/
@Controller
@RequestMapping("/reader")
public class ReaderController {
UserMapper userMapper;
@Autowired
private ReaderServices readerService;
// 查询所有读者信息
@RequestMapping("/list")
@ResponseBody()
public List<User> list() {
return readerService.getAllName();
}
// 跳转到页面
@RequestMapping("/toadd")
public String toAdd() {
return "/WEB-INF/page/userManagement/User.jsp";
}
@RequestMapping("/jumpAdd")
String jumpAdd(RedirectAttributes redirectAttributes, Model model){
model.addAttribute("msg", redirectAttributes.getAttribute("msg"));
return "/WEB-INF/page/userManagement/adduser.jsp";
}
// 新增读者
@ResponseBody
@RequestMapping("/doAdd")
public String doAdd(User user){
if (readerService.adduser(user)){
return "success";
}
Result returnBook(int borrowingId,int isReturn,boolean isTimeout){
String s = null;
try {
s = borrowingServices.returnBook(borrowingId, isReturn, isTimeout);
} catch (Exception e) {
return ResultFactory.getFail(e.getMessage());
}
return ResultFactory.getSucceed(s);
}
/**
* <h2>取消预约方法</h2>
*/
@ResponseBody
@PostMapping("/cancelAnAppointment")
Result cancelAnAppointment(int borrowingId){
try {
borrowingServices.cancelAnAppointment(borrowingId);
} catch (Exception e) {
Result fail = ResultFactory.getFail();
fail.setMessage(e.getMessage());
return fail;
}
return ResultFactory.getSucceed("取消成功");
}
/**
* <h2>预约借书方法控制器</h2>
*/
@ResponseBody
@PostMapping("/bookingLibrary")
Result bookingLibrary(int borrowingId){
return ResultFactory.getSucceed(borrowingServices.bookingLibrary(borrowingId));
}
}
}
// 跳转到页面
@RequestMapping("/toadd")
public String toAdd() {
return "/WEB-INF/page/userManagement/User.jsp";
}
@RequestMapping("/jumpAdd")
String jumpAdd(RedirectAttributes redirectAttributes, Model model){
model.addAttribute("msg", redirectAttributes.getAttribute("msg"));
return "/WEB-INF/page/userManagement/adduser.jsp";
}
// 新增读者
@ResponseBody
@RequestMapping("/doAdd")
public String doAdd(User user){
if (readerService.adduser(user)){
return "success";
}
return "falue";
}
@RequestMapping("/Deleteuser")
String Deleteuser(){
return "/WEB-INF/page/userManagement/Deleteuser.jsp";
}
// 删除读者信息
@RequestMapping("/delete/{UserId}")
public String delete(@PathVariable("UserId") Integer UserId) {
readerService.deleteUser(UserId);
return "redirect:/reader/list";
}
@RequestMapping("/Searchuser")
String Searchuser(){
return "/WEB-INF/page/userManagement/Searchuser.jsp";
}
@ResponseBody
@RequestMapping ("/userSearch")
public List<User> queryBook(String keyword) {
return readerService.queryuser(keyword);
}
// 跳转到修改读者信息页面
@RequestMapping("/toUpdate/{readerId}")
public String toUpdate(@PathVariable("readerId") Integer readerId, Model model) {
User user2 = readerService.getReaderById(readerId);
model.addAttribute("reader", user2);
return "reader/update";
}
@ResponseBody
@GetMapping("/getBorrowingRecent/{bookId}")
Result getBorrowingRecent(@PathVariable int bookId){
int borrowingRecent = bookService.getBorrowingRecent(bookId);
Result result = ResultFactory.getSucceed();
result.addData("borrowingRecent",borrowingRecent);
return result;
}
@Autowired
BorrowingServices borrowingServices;
/**
* 超时记录
* */
@ResponseBody
@GetMapping("/getTimeoutLogging")
Result getTimeoutLogging(int userId){
List<UserBorrowingInfo> userTimeoutLogging = borrowingServices.getUserTimeoutLogging(userId);
Result succeed = ResultFactory.getSucceed();
succeed.addData("timeoutLogging",userTimeoutLogging);
return succeed;
}
/**
* 借阅数量
* */
@ResponseBody
@GetMapping("/getBorrowingTotalTotal")
Result getBorrowingTotalTotal(int userId){
int userBorrowingTotal = borrowingServices.getUserBorrowingTotal(userId);
Result succeed = ResultFactory.getSucceed();
succeed.addData("borrowedsTotal",userBorrowingTotal);
return succeed;
}
/**
* <h2>借阅请求方法</h2>
* 获取当前时间,其他信息由前端页面给予
*/
@ResponseBody
@PostMapping("/newBorrowing")
Result newBorrowing(/*@DateTimeFormat(pattern = "YYYY-MM-DD'T'HH:mm")*/ @RequestBody Borrowing borrowing){
// System.out.println(borrowing);//test
boolean isBorrowingSucceed = true;
String message="";
try {
borrowingServices.borrowing(borrowing);
}
catch (Exception e){
isBorrowingSucceed=false;
message=e.getMessage();
}
@GetMapping("/pageNum/{pageNo}")
public Page<BookTotalInfor> list(@PathVariable int pageNo) {
return toTalInfor.receive(pageNo,5);
}
@RequestMapping("/borrowrank")
public String BorringRank(){
return "/WEB-INF/page/InformationStatistics/BorrowRank.jsp";
}
@ResponseBody
@RequestMapping("/bookrank")
public List<Map<String, Object>> BookRank(Model model){
return inforRank.BorrowRank();
}
@ResponseBody
@RequestMapping("/bookrankbytype")
public List<Map<String,Object>> BookBookByType(){return inforRank.BookRankByType();}
@RequestMapping("/readerborrow")
public String ReaderBorrow(){
return "/WEB-INF/page/InformationStatistics/ReaderRank.jsp";
}
@ResponseBody
@RequestMapping("/readerrank")
public List<Map<String,Object>> ReaderRank(){return inforRank.ReaderRank();}
@RequestMapping("/bookingpage")
public String BookingPage(){return "/WEB-INF/page/InformationStatistics/BookingStatistic.jsp";}
@ResponseBody
@RequestMapping("/bookingstatistic")
public List<Map<String,Object>> BookingStatistic(){return inforRank.BookingStatistic();}
@ResponseBody
@GetMapping("/pricepageNum/{pageNo}")
public Page<Map<String,Object>> PriceInfor(@PathVariable int pageNo){return toTalInfor.ReceiveByPrice(pageNo,5,5);}
Result result = ResultFactory.getSucceed();
result.addData(userService.getAddressById(id));
return result;
}
@PostMapping("/execute")
String register(User user,HttpSession session){
if (userService.newUser(user)) {
session.setAttribute("msg","注册成功,请先登录。");
// redirectAttributes.addFlashAttribute("msg","注册成功,请先登录。");
// model.addAttribute("msg","注册成功,请先登录。");
return "redirect:/login";
}
else{
session.setAttribute("msg","注册失败,请联系管理员");
// redirectAttributes.addFlashAttribute("msg","注册成功,请先登录。");
// redirectAttributes.addFlashAttribute("User",user);
// model.addAttribute("msg","注册失败,请联系管理员");
return "redirect:/register";
}
}
}
/**
* <h1>登录信息拦截器</h1>
* 将Session添加到request中
*/
@Component
public class LoginInfoInterceptor implements HandlerInterceptor {
/**
* <h2>前置处理</h2>
* 如果Session中有用户登录信息,则将其发给请求域
@ResponseBody
@PostMapping("/newBorrowing")
Result newBorrowing(/*@DateTimeFormat(pattern = "YYYY-MM-DD'T'HH:mm")*/ @RequestBody Borrowing borrowing){
// System.out.println(borrowing);//test
boolean isBorrowingSucceed = true;
String message="";
try {
borrowingServices.borrowing(borrowing);
}
catch (Exception e){
isBorrowingSucceed=false;
message=e.getMessage();
}
if(isBorrowingSucceed){
Result succeed = ResultFactory.getSucceed();
succeed.addData("isBorrowingSucceed",isBorrowingSucceed);
return succeed;
}else{
return ResultFactory.getFail(message);
}
}
}
@Controller
@RequestMapping("/searchcontroller")
/**指定控制器响应url的请求**/
public class SearchController {
@RequestMapping("/page")
String page() {
return "/WEB-INF/page/bookManagement/bookManagementMain.jsp";
}
@RequestMapping("/bookAdd")
String bookAdd(RedirectAttributes redirectAttributes, Model model) {
model.addAttribute("msg", redirectAttributes.getAttribute("msg"));
return "/WEB-INF/page/bookManagement/bookAdd.jsp";
}
@RequestMapping("/bookDelete")
@Controller
@RequestMapping("/usercenter")
public class UserCenterController {
@Autowired
InforService inforService;
@Autowired
ToTalInfor toTalInfor;
@Autowired
InforRank inforRank;
@RequestMapping("/userpage")
public String userpage(Model model, HttpServletRequest req, HttpServletResponse resp,HttpSession session) {
UserLoginInfo userLoginInfo = (UserLoginInfo) session.getAttribute("USER_LOGIN_INFO");
UserInfor userInfor = inforService.QueryInfor(userLoginInfo.getUserName());
System.out.println(userInfor.getName());
model.addAttribute("name",userInfor.getName());
model.addAttribute("username",userInfor.getUserName());
model.addAttribute("userid",userInfor.getId());
model.addAttribute("useremail",userInfor.getEmail());
model.addAttribute("address",userInfor.getAddress());
return "/WEB-INF/page/InformationStatistics/UserCenter.jsp";
}
@RequestMapping("/userupdate")
public String UserUpdate(Model model, HttpServletRequest req){
String name=req.getParameter("name");
int id= Integer.parseInt(req.getParameter("id"));
model.addAttribute("name",name);
model.addAttribute("result", result);
return "/WEB-INF/page/borrowingManagement/BookDetailsBorrowing.jsp";
}
@ResponseBody
@GetMapping("/{bookId}")
Result bookPage(@PathVariable int bookId, Model model){
Book book = bookService.getBookById(bookId);
return ResultFactory.getSucceedData(book);
}
/**
* <h2>获取总借阅量</h2>
*/
@ResponseBody
@GetMapping("/getBorrowingTotal/{bookId}")
Result getBorrowingTotal(@PathVariable int bookId){
int borrowingTotal = bookService.getBorrowingTotal(bookId);
Result result = ResultFactory.getSucceed();
result.addData("borrowingTotal",borrowingTotal);
return result;
}
/**
* <h2>近期借阅量</h2>
*/
@ResponseBody
@GetMapping("/getBorrowingRecent/{bookId}")
Result getBorrowingRecent(@PathVariable int bookId){
int borrowingRecent = bookService.getBorrowingRecent(bookId);
Result result = ResultFactory.getSucceed();
result.addData("borrowingRecent",borrowingRecent);
return result;
}
@Autowired
BorrowingServices borrowingServices;
/**
* 超时记录
* */