博主主页:Java旅途
简介:分享计算机知识、学习路线、系统源码及教程
文末获取源码
图书管理系统基于Spring+SpringMVC+Mybatis开发,系统主要实现了图书馆借书还书功能,系统分为管理员和读者两种角色。
管理员功能如下:
读者功能如下:
BookInfoController
package com.yx.controller;
import com.github.pagehelper.PageInfo;
import com.yx.po.BookInfo;
import com.yx.po.TypeInfo;
import com.yx.service.BookInfoService;
import com.yx.service.TypeInfoService;
import com.yx.utils.DataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@Controller
public class BookInfoController {
@Autowired
private BookInfoService bookInfoService;
@Autowired
private TypeInfoService typeInfoService;
/**
* 图书管理首页
* @return
*/
@GetMapping("/bookIndex")
public String bookIndex(){
return "book/bookIndex";
}
/**
* 获取book信息,封装成json
* @param bookInfo
* @param pageNum
* @param limit
* @return
*/
@RequestMapping("/bookAll")
@ResponseBody //@ResponseBody将java对象转为json格式的数据,表示该方法的返回结果直接写入 HTTP response body 中,一般在异步ajax获取数据时使用
public DataInfo bookAll(BookInfo bookInfo, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "15") Integer limit){
PageInfo<BookInfo> pageInfo = bookInfoService.queryBookInfoAll(bookInfo,pageNum,limit);
return DataInfo.ok("成功",pageInfo.getTotal(),pageInfo.getList());//总条数getTotal,数据封装成list,以便加载分页显示,由于加了ResponseBody,就会返回一个字符串
}
/**
* 添加页面的跳转
*/
@GetMapping("/bookAdd")
public String bookAdd(){
return "book/bookAdd";
}
/**
* 类型添加提交
*/
@RequestMapping("/addBookSubmit")
@ResponseBody
public DataInfo addBookSubmit(BookInfo info){
bookInfoService.addBookSubmit(info);
return DataInfo.ok();
}
/**
* 类型根据id查询(修改)
*/
@GetMapping("/queryBookInfoById")
public String queryTypeInfoById(Integer id, Model model){
BookInfo bookInfo= bookInfoService.queryBookInfoById(id);
model.addAttribute("info",bookInfo);
return "book/updateBook";
}
/**
* 修改提交功能
*/
@RequestMapping("/updateBookSubmit")
@ResponseBody
public DataInfo updateBookSubmit(@RequestBody BookInfo info){
bookInfoService.updateBookSubmit(info);
return DataInfo.ok();
}
/**
* 类型删除
*/
@RequestMapping("/deleteBook")
@ResponseBody
public DataInfo deleteBook(String ids){
List<String> list= Arrays.asList(ids.split(","));
bookInfoService.deleteBookByIds(list);
return DataInfo.ok();
}
@RequestMapping("/findAllList")
@ResponseBody
public List<TypeInfo> findAll(){
PageInfo<TypeInfo> pageInfo = typeInfoService.queryTypeInfoAll(null,1,100);
List<TypeInfo> lists = pageInfo.getList();
return lists;
}
}
package com.yx.controller;
import com.yx.codeutil.IVerifyCodeGen;
import com.yx.codeutil.SimpleCharVerifyCodeGenImpl;
import com.yx.codeutil.VerifyCode;
import com.yx.po.Admin;
import com.yx.po.ReaderInfo;
import com.yx.service.AdminService;
import com.yx.service.ReaderInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Controller
public class LoginController {
@Autowired
private AdminService adminService;
@Autowired
private ReaderInfoService readerService;
/**
* 登录页面的转发
*/
@GetMapping("/login")
public String login(){
return "login";
}
/**
* 获取验证码方法
* @param request
* @param response
*/
@RequestMapping("/verifyCode")
public void verifyCode(HttpServletRequest request, HttpServletResponse response) {
IVerifyCodeGen iVerifyCodeGen = new SimpleCharVerifyCodeGenImpl();
try {
//设置长宽
VerifyCode verifyCode = iVerifyCodeGen.generate(80, 28);
String code = verifyCode.getCode();
//将VerifyCode绑定session
request.getSession().setAttribute("VerifyCode", code);
//设置响应头
response.setHeader("Pragma", "no-cache");
//设置响应头
response.setHeader("Cache-Control", "no-cache");
//在代理服务器端防止缓冲
response.setDateHeader("Expires", 0);
//设置响应内容类型
response.setContentType("image/jpeg");
response.getOutputStream().write(verifyCode.getImgBytes());
response.getOutputStream().flush();
} catch (IOException e) {
System.out.println("异常处理");
}
}
/**
* 登录验证
*/
@RequestMapping("/loginIn")
public String loginIn(HttpServletRequest request, Model model){
//获取用户名与密码
String username = request.getParameter("username");
String password = request.getParameter("password");
String code=request.getParameter("captcha");
String type=request.getParameter("type");
//判断验证码是否正确(验证码已经放入session)
HttpSession session = request.getSession();
String realCode = (String)session.getAttribute("VerifyCode");
if (!realCode.toLowerCase().equals(code.toLowerCase())){
model.addAttribute("msg","验证码不正确");
return "login";
}else{
//验证码正确则判断用户名和密码
if(type.equals("1")){//管理员信息
//用户名和密码是否正确
Admin admin=adminService.queryUserByNameAndPassword(username,password);
if(admin==null){//该用户不存在
model.addAttribute("msg","用户名或密码错误");
return "login";
}
session.setAttribute("user",admin);
session.setAttribute("type","admin");
}else{//来自读者信息表
ReaderInfo readerInfo=readerService.queryUserInfoByNameAndPassword(username,password);
if(readerInfo==null){
model.addAttribute("msg","用户名或密码错误");
return "login";
}
session.setAttribute("user",readerInfo);
session.setAttribute("type","reader");
}
return "index";
}
}
/**
* 退出功能
*/
@GetMapping("loginOut")
public String loginOut(HttpServletRequest request){
HttpSession session = request.getSession();
session.invalidate();//注销
return "/login";
}
}
大家点赞、收藏、关注、评论啦 、点开下方卡片关注后回复 103