提示:黑马程序员的云借阅管理系统, 后端ssm框架, 前端jQuery, layui,模板引擎为thymeleaf。 有需要的小伙伴可以三连, 我会私聊你发源码。
前言
提示:视频观看地址
bibi视频项目展示
提示:管理员全部功能, 用户部分功能
一、整个项目功能图
二、代码展示
1. 用户模块
登录分为管理员登录, 普通用户登录, 不同身份登录拥有的权限也不同
代码
@Controller
@RequestMapping("/pub/user")
public class UserController {
@Autowired
private UserService userService;
/**
* to -> 登录功能
* localhost:8081/pub/user/toLogin
* @return
*/
@GetMapping("toLogin")
public String toLogin(){
return "login";
}
/**
* 登录功能
* localhost:8081/pub/user/login
* @param email
* @param pwd
* @param model
* @return
*/
@PostMapping("login")
public String login(String email, String pwd, Model model, HttpServletRequest request){
//从数据库中查找用户
User user = userService.login(email,pwd);
if(user == null){
//没有该账户
model.addAttribute("userLoginMsg", "账户密码错误");
return "login"; //重新登录
}else {
if ("0".equals(user.getUserStatus())){ //判断是否是正常用户
//将用户存储到session中
request.getSession().setAttribute("user",user);
return "redirect:/pri/book/list_book"; //进入管理员界面
}else{ //用户被冻结
//不是正常用户
model.addAttribute("userLoginMsg", "账号已经冻结");
return "login"; //重新登录
}
}
}
/**
* 退出登录
* localhost:8081/pub/user/layout
* @param request
* @return
*/
@GetMapping("layout")
public String layout(HttpServletRequest request){
//清口session会话
request.getSession().invalidate();
return "redirect:/pub/user/toLogin";
}
}
2. 书籍模块
首页页面
借阅图书页面
当前借阅页面
代码
@Controller
@RequestMapping("/pri/book")
public class BookController {
@Autowired
private BookService bookService;
/**
* 分页查询书籍功能
* localhost:8081/pri/book/list_book?pageNum=2
* @param pageNum
* @param pageSize
* @param model
* @return
*/
@GetMapping("list_book")
public String listBook(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
@RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize,
Model model){
//查询数据库记录
List<Book> list = bookService.listBook(pageNum,pageSize);
//查询数据库书籍的总记录数
Integer totalRecordNum = bookService.findBookTotalNum();
//将书籍存储起来, 传给前端页面
model.addAttribute("bookList", list);
//计算总页数
Integer totalPage = 0;
if(totalRecordNum % pageSize == 0){ //刚好满足
totalPage = totalRecordNum / pageSize;
}else{ //比满页多一页
totalPage = totalRecordNum / pageSize + 1;
}
model.addAttribute("totalPage",totalPage); //将数据存储起来
return "welcome";
}
/**
* 跳转到借阅信息当中
* localhost:8081/pri/book/to_book_borrow
* @return
*/
@GetMapping("to_book_borrow")
public String toBookBorrow(Model model,HttpServletRequest request){
//收集从前端传来的代码
Integer bookId = Integer.parseInt(request.getParameter("book_id"));
//通过book的id去查找Book信息
Book book = bookService.findBookById(bookId);
//将信息存储起来
model.addAttribute("book", book);
System.out.println("===========" + book.toString());
return "bookDetail";
}
/**
* 图书借阅功能
* localhost:8081/pri/book/borrow_book
* @param book
* @param request
* @param model
* @return
*/
@PostMapping("borrow_book")
public String borrowBook(Book book,HttpServletRequest request,Model model){
System.out.println("===========" + book.toString());
//从session中获取user信息
User user = (User)request.getSession().getAttribute("user");
String userName = user.getUserName();
//更新数据库书籍状态
Integer flag = bookService.borrowBook(book,userName);
if (flag > 0){ //更新成功 -> 借阅成功
model.addAttribute("borrowBookMsg", "借阅成功,请到图书馆二楼领取");
return "redirect:/pri/book/list_book";
}
model.addAttribute("borrowBookMsg", "借阅失败,请重试该操作");
return "redirect:/pri/book/list_book";
}
}
3. 拦截器配置
为什么要配置拦截器?
拦截器可以避免一些错误, 比如借阅图书的时候需要获取用户的信息,
如果程序里面的session没用user信息, 则不能访问页面。
未登录的效果展示
代码
- 配置
//能够被springIOC容器扫描到
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
private LoginInterceptor loginInterceptor;
@Autowired
public void setLoginInterceptor(LoginInterceptor loginInterceptor){
this.loginInterceptor = loginInterceptor;
}
/**
* 添加拦截器
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(loginInterceptor).addPathPatterns("/**").
excludePathPatterns("/pub/user/login","/","/pub/user/toLogin", "/pub/user/layout");
}
}
- 拦截器
@Component
public class LoginInterceptor implements HandlerInterceptor {
/**
* 获取session会话,然后验证session会话是否有user,有放行, 没有则通过
* @param request
* @param response
* @param handler
* @return
* @throws Exception
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if(user != null){
return true;
}
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
4. 项目结构图
数据库表文章来源:https://www.toymoban.com/news/detail-520899.html
# 借阅记录表
CREATE TABLE `record` (
`record_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`record_bookname` varchar(32) NOT NULL COMMENT '借阅的图书名称',
`record_bookisbn` varchar(32) NOT NULL COMMENT '借阅的图书的ISBN',
`record_borrower` varchar(32) NOT NULL COMMENT '图书借阅人',
`record_borrowtime` varchar(32) NOT NULL COMMENT '图书借阅时间',
`record_remandtime` varchar(32) NOT NULL COMMENT '图书归还时间',
PRIMARY KEY (`record_id`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8mb4;
# 用户表 -> 管理员和用户放在一起了
CREATE TABLE `user` (
`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`user_name` varchar(32) NOT NULL COMMENT '用户名称',
`user_password` varchar(32) NOT NULL COMMENT '用户密码',
`user_email` varchar(32) NOT NULL COMMENT '用户邮箱[用户账号]',
`user_role` varchar(32) NOT NULL COMMENT '用户角色[ADMIN:管理员,USER:用户]',
`user_status` varchar(1) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
# 书籍表
CREATE TABLE `books` (
`book_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`book_name` varchar(32) NOT NULL COMMENT '图书名称',
`book_isbn` varchar(32) NOT NULL COMMENT '图书标准',
`book_press` varchar(32) NOT NULL COMMENT '图书出版社',
`book_author` varchar(32) NOT NULL COMMENT '图书作者',
`book_pagination` int(11) NOT NULL COMMENT '图书页数',
`book_price` decimal(5,2) NOT NULL COMMENT '图书价格',
`book_uploadtime` varchar(32) NOT NULL COMMENT '图书上架时间',
`book_status` varchar(1) NOT NULL COMMENT,
`book_borrower` varchar(32) DEFAULT NULL COMMENT '图书借阅人',
`book_borrowtime` varchar(32) DEFAULT NULL COMMENT '图书借阅时间',
`book_returntime` varchar(32) DEFAULT NULL COMMENT '图书预计归还时间',
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8mb4;
pom.xml文件文章来源地址https://www.toymoban.com/news/detail-520899.html
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
</dependencies>
总结
上述代码只是部分代码, 需要完整的代码可以私聊我, 如果上述描述有问题,
立即联系我, 谢谢!!! 记得三连 支持支持博主
到了这里,关于期末考试还缺项目吗?黑马的图书借阅管理系统很香的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!