基于javaweb+mysql的ssm自习室图书馆座位预约占座管理系统前台后台(java+ssm+jsp+layui+mysql)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb+mysql的SSM自习室图书馆座位预约占座管理系统前台后台(java+ssm+jsp+layui+mysql)
管理员
admin 123456
学生
xs001 123456
xs002 123456
xs003 123456
xs004 123456
添加学生时会自动在用户表中注册
定时任务会定时生成座位信息,
阅览室分类中可设置信用等级,
学生被扣分后信用等级低于相应的值后不能预约相应的阅览室座位。
public void teacherList(HttpServletRequest request,HttpServletResponse response){
try {
page = Integer.parseInt(request.getParameter("page")); //获取当前页
rows = Integer.parseInt(request.getParameter("limit")); //每页记录条数
// 构造查询条件
teacher = new Teacher();
teacher.setPage((page-1)*rows);
teacher.setRows(rows);
teacher.setName(request.getParameter("name"));
teacher.setNo(request.getParameter("no"));
User currentUser = (User)request.getSession().getAttribute("currentUser");
if(currentUser.getRoleId()==2){ //教师
teacher.setNo(currentUser.getUserName());
}
// 调用service层的查询方法查出记录和数量
List<Teacher> list = teacherService.findTeacher(teacher);
int total = teacherService.countTeacher(teacher);
JSONObject jsonObj = new JSONObject();//new一个JSON
jsonObj.put("code", 0);
jsonObj.put("msg", "");
jsonObj.put("count",total );//total代表一共有多少数据
jsonObj.put("data", list);//row是代表显示的页的数据
WriterUtil.write(response,jsonObj.toString()); //将上述Json输出,前台ajax接收
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping("reserveTeacher")
public void reserve(HttpServletRequest request,HttpServletResponse response,Teacher teacher) {
JSONObject result = new JSONObject(); // new一个json对象
result.put("success", true);
}
String studentno = currentUser.getUserName();
String nowDateHour = new SimpleDateFormat("yyyy-MM-dd-HH").format(new Date()); //当前小时数
String selectedDate = keyword.substring(0,13);
try {
//判断信用积分
score = scoreService.findOneScore(studentno);
int myScore = score.getTotal(); //该学生分数
int roomid = Integer.parseInt(keyword.substring(19,20));
int needScore = roomService.findScoreByRoomid(roomid);
if(needScore >= myScore){
jsonObject.put("returnCode", "预约失败!您的信用积分不允许在该阅览室选座");
WriterUtil.write(response, jsonObject.toString());
return;
}
String flag = "1";
Choice c = new Choice();
c.setStudentno(studentno);
c.setStatus("0");
List<Choice> list = choiceService.findChoice(c);
if(list==null || list.size()==0){
// 无预约 OK的
} else if(list.size()>3){
// 限预约3次
flag = "3";
}else {
for(Choice choice : list){
if(choice.getSeatkeyword().substring(0,17).equals(keyword.substring(0,17))){
//重复了
flag = "2";
break;
}
}
}
if("3".equals(flag)){
jsonObject.put("returnCode", "预约失败!今天已经预约3次了,退座后可再次预约哦!");
WriterUtil.write(response, jsonObject.toString());
return;
} else if ("2".equals(flag)) {
if(seat.getStatus()==3) { //如果要改变为暂离状态-->设置暂离定时器
LeaveTimer leavetimer=new LeaveTimer(seat.getKeyword(),seat.getStudentno());
Thread leavethread=new Thread(leavetimer);
leavethread.start();
}
jsonObject.put("returnCode", 200);
WriterUtil.write(response, jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
jsonObject.put("returnCode", "对不起!系统错误,操作失败!");
WriterUtil.write(response, jsonObject.toString());
}
}
//取消选座
@RequestMapping("cancelSeat")
public void cancelSeat(HttpServletRequest request,HttpServletResponse response){
User currentUser = (User)request.getSession().getAttribute("currentUser");
JSONObject jsonObject = new JSONObject();
try {
// 删除choice表中的记录
String keyword = request.getParameter("seatkeyword");
Choice choice = new Choice();
choice.setSeatkeyword(keyword.substring(0, 20));
choice.setStudentno(currentUser.getUserName());
choiceService.cancelChoice(choice);
// 将seat表中该条记录学号变成1
seatService.cancelSeat(keyword); //
jsonObject.put("returnCode", 200);
WriterUtil.write(response, jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
jsonObject.put("returnCode", "对不起!系统错误,操作失败!");
WriterUtil.write(response, jsonObject.toString());
}
}
//签到定时器
public class SignTimer implements Runnable{
private String Keyword;
private String Studentno;
public SignTimer(String keyword,String studentno) {
this.Keyword=keyword;
this.Studentno=studentno;
}
@Override
bbsService.deleteBbs(Integer.parseInt(id));
}
result.put("success", true);
result.put("delNums", ids.length);
} catch (Exception e) {
e.printStackTrace();
result.put("errorMsg", "对不起,删除失败");
}
WriterUtil.write(response, result.toString());
}
}
//推荐朋友控制器
@Controller
@RequestMapping("friend")
public class FriendController {
private Choice choice;
@Autowired
private ChoiceService<Choice> choiceService;
@Autowired
break;
default:
break;
}
userService.addUser(user);
result.put("returnCode",300);
} else {
result.put("returnCode", "该用户名被使用");
}
}
} catch (Exception e) {
e.printStackTrace();
result.put("returnCode", "对不起,操作失败");
}
response.setContentType("application/json");
WriterUtil.write(response, result.toString());
}
@RequestMapping("deleteUser")
public void delUser(HttpServletRequest request,HttpServletResponse response){
JSONObject result=new JSONObject();
try {
String[] ids=request.getParameter("ids").split(",");
for (String id : ids) {
user = userService.findOneUser(Integer.parseInt(id));
userService.deleteUser(Integer.parseInt(id));
}
result.put("returnCode", 200);
result.put("delNums", ids.length);
} catch (Exception e) {
e.printStackTrace();
result.put("returnCode", "对不起,删除失败");
}
WriterUtil.write(response, result.toString());
}
@RequestMapping("updataPortrait")
public void updataPortrait(HttpServletRequest request,HttpServletResponse response,@RequestParam MultipartFile file,User user){
JSONObject jsonObj = new JSONObject();//new一个JSON
String userId = request.getParameter("userId");
HttpSession session = request.getSession();
try {
if (StringUtil.isNotEmpty(userId)) {
user.setUserId(Integer.parseInt(userId));
}
} catch (Exception e) {
e.printStackTrace();
}
String now = System.currentTimeMillis()+"";
if (!file.isEmpty()) {
}
@RequestMapping("chooseMenu")
public void chooseMenu(HttpServletRequest request,HttpServletResponse response){
try {
String parentId=request.getParameter("parentId");
String roleId=request.getParameter("roleId");
role = roleService.findOneRole(Integer.parseInt(roleId));
String menuIds = role.getMenuIds();
String operationIds = role.getOperationIds();
JSONArray jsonArray=getCheckedMenusByParentId(parentId, menuIds,operationIds);
WriterUtil.write(response, jsonArray.toString());
System.out.println(jsonArray.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
// 选中已有的角色
public JSONArray getCheckedMenusByParentId(String parentId,String menuIds,String operationIds)throws Exception{
JSONArray jsonArray=this.getCheckedMenuByParentId(parentId,menuIds,operationIds);
for(int i=0;i<jsonArray.size();i++){
JSONObject jsonObject=jsonArray.getJSONObject(i);
if("open".equals(jsonObject.getString("state"))){
continue;
}else{
jsonObject.put("children", getCheckedMenusByParentId(jsonObject.getString("id"),menuIds,operationIds));
}
}
return jsonArray;
}
public JSONArray getCheckedMenuByParentId(String parentId,String menuIds,String operationIds)throws Exception{
JSONArray jsonArray=new JSONArray();
menu = new Menu();
menu.setParentId(Integer.parseInt(parentId));
List<Menu> list = menuService.findMenu(menu);
for(Menu menu : list){
JSONObject jsonObject = new JSONObject();
int menuId = menu.getMenuId();
jsonObject.put("id", menuId);
jsonObject.put("text", menu.getMenuName());
jsonObject.put("iconCls", menu.getIconCls());
jsonObject.put("state", menu.getState());
if (StringUtil.isNotEmpty(menuIds)) {
if (dingzhen.util.StringUtil.existStrArr(menuId+"", menuIds.split(","))) {
jsonObject.put("checked", true);
for (int i=0;i<ids.length;i++) {
studentService.deleteStudent(Integer.parseInt(ids[i]));
scoreService.deleteOneScore(nos[i]);
}
result.put("returnCode", 200);
result.put("delNums", ids.length);
} catch (Exception e) {
e.printStackTrace();
result.put("returnCode", "对不起,删除失败");
}
WriterUtil.write(response, result.toString());
}
@RequestMapping("uploadPhoto")
public void uploadPhoto(HttpServletRequest request,HttpServletResponse response,@RequestParam MultipartFile photo){
String now = System.currentTimeMillis()+"";
if (!photo.isEmpty()) {
String filePath = request.getSession().getServletContext().getRealPath("/")+ "upload/student/" + now + ".jpg";
try {
photo.transferTo(new File(filePath));
student.setPhoto("upload/student/" + now + ".jpg");
} catch (Exception e) {
e.printStackTrace();
}
}
WriterUtil.write(response, "upload/student/" + now + ".jpg");
}
}
jsonObject.put("returnCode", "对不起!系统错误,选座失败!");
WriterUtil.write(response, jsonObject.toString());
}
}
//改变座位状态
@RequestMapping("reserveSeat")
public void reserveSeat(HttpServletRequest request,HttpServletResponse response,Seat seat){
JSONObject jsonObject = new JSONObject();
try {
seatService.occupySeat(seat);
if(seat.getStatus()==3) { //如果要改变为暂离状态-->设置暂离定时器
LeaveTimer leavetimer=new LeaveTimer(seat.getKeyword(),seat.getStudentno());
Thread leavethread=new Thread(leavetimer);
leavethread.start();
}
jsonObject.put("returnCode", 200);
WriterUtil.write(response, jsonObject.toString());
} catch (Exception e) {
e.printStackTrace();
jsonObject.put("returnCode", "对不起!系统错误,操作失败!");
WriterUtil.write(response, jsonObject.toString());
}
}
//取消选座
@RequestMapping("cancelSeat")
public void cancelSeat(HttpServletRequest request,HttpServletResponse response){
User currentUser = (User)request.getSession().getAttribute("currentUser");
JSONObject jsonObject = new JSONObject();
try {
// 删除choice表中的记录
String keyword = request.getParameter("seatkeyword");
Choice choice = new Choice();
choice.setSeatkeyword(keyword.substring(0, 20));
choice.setStudentno(currentUser.getUserName());
choiceService.cancelChoice(choice);
@RequestMapping("deleteMenu")
public void deleteMenu(HttpServletRequest request,HttpServletResponse response){
JSONObject result = new JSONObject();
try {
String menuId = request.getParameter("menuId");
String parentId = request.getParameter("parentId");
if (!isLeaf(menuId)) { //不是叶子节点,说明有子菜单,不能删除
result.put("errorMsg", "该菜单下面有子菜单,不能直接删除");
} else {
menu = new Menu();
menu.setParentId(Integer.parseInt(parentId));
int sonNum = menuService.countMenu(menu);
if (sonNum == 1) {
// 只有一个孩子,删除该孩子,且把父亲状态置为open
menu = new Menu();
menu.setMenuId(Integer.parseInt(parentId));
menu.setState("open");
menuService.updateMenu(menu);
menuService.deleteMenu(Integer.parseInt(menuId));
} else {
//不只一个孩子,直接删除
menuService.deleteMenu(Integer.parseInt(menuId));
}
result.put("success", true);
}
} catch (Exception e) {
e.printStackTrace();
result.put("errorMsg", "对不起,删除失败!");
}
WriterUtil.write(response, result.toString());
}
}
// 通知信息
private static Object getFieldValueByName(String fieldName,Object o){
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase(); //手写字母大写
String getter = "get" + firstLetter + fieldName.substring(1); // get方法
Method method = o.getClass().getMethod(getter,new Class[]{});
Object object = method.invoke(o, new Object[]{});
return object;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
/**
* 反射获得字段值
* @param o
* @return
*/
@SuppressWarnings("unused")
private static String[] getFieldName(Object o){
Field[] fields = o.getClass().getDeclaredFields();
String[] fieldNames = new String[fields.length];
for (int i = 0; i < fieldNames.length; i++) {
fieldNames[i] = fields[i].getName();
}
return fieldNames;
}
/**
* 获取访问者IP
*
* 在一般情况下使用Request.getRemoteAddr()即可,但是经过nginx等反向代理软件后,这个方法会失效。
*
* 本方法先从Header中获取X-Real-IP,如果不存在再从X-Forwarded-For获得第一个IP(用,分割),
* 如果还不存在则调用Request .getRemoteAddr()。
rows = Integer.parseInt(request.getParameter("limit"));
illegal = new Illegal();
illegal.setPage((page-1)*rows);
illegal.setRows(rows);
illegal.setStudentno(request.getParameter("studentno"));
List<Illegal> list = illegalService.findIllegal(illegal);
int total = illegalService.countIllegal(illegal);
JSONObject jsonObj = new JSONObject();//new一个JSON
jsonObj.put("code", 0);
jsonObj.put("msg", "");
jsonObj.put("count",total );//total代表一共有多少数据
jsonObj.put("data", list);//row是代表显示的页的数据
WriterUtil.write(response,jsonObj.toString()); //将上述Json输出,前台ajax接收
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping("reserveIllegal")
public void reserveIllegal(HttpServletRequest request,HttpServletResponse response,Illegal illegal) {
JSONObject result = new JSONObject();
String illegalId = request.getParameter("id");
if (StringUtil.isNotEmpty(illegalId)) { // illegalId不为空 说明是修改
try {
illegal.setId(Integer.parseInt(illegalId));
this.illegal=illegalService.findOneIllegal(Integer.parseInt(illegalId));
String studentno = this.illegal.getStudentno(); //获得学号
score = scoreService.findOneScore(studentno); //获取socre对象
int total = score.getTotal(); //获得原始积分
int oldScore = this.illegal.getScore(); //原始扣除积分
int newScore = illegal.getScore();
int changeScore = newScore-oldScore;
if(changeScore!=0)
{
// 更新分数
score.setTotal(total - changeScore);
scoreService.updateScore(score);
}
illegalService.updateIllegal(illegal);
result.put("returnCode",200);
} catch (Exception e) {
e.printStackTrace();
result.put("returnCode", "对不起,操作失败");
}
}else { // 添加
try {
String studentno = illegal.getStudentno(); //获得学号
score = scoreService.findOneScore(studentno); //获取socre对象
int total = score.getTotal(); //获得原始积分
int thisScore = illegal.getScore(); //本次扣除积分
if(thisScore > total){
String content = request.getParameter("content");
User currentUser = (User)request.getSession().getAttribute("currentUser");
String author = currentUser.getUserName();
String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
// 插入topic表
topic = new Topic();
topic.setAuthor(author);
topic.setBbsid(bbsid);
topic.setContent(content);
topic.setTime(time);
topicService.addTopic(topic);
// 更新bbs表的最后发表人
Bbs bbs = new Bbs();
bbs.setId(bbsid);
bbs.setLastreply(author);
bbs.setLastreplytime(time);
bbsService.updateBbslast(bbs);
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:detail.htm?bbsid="+bbsid;
}
}
/*
// 教师信息
result.put("returnCode", "对不起,操作失败");
}
}
WriterUtil.write(response, result.toString());
}
@RequestMapping("deleteIllegal")
public void delete(HttpServletRequest request,HttpServletResponse response){
JSONObject result=new JSONObject();
try {
String[] ids=request.getParameter("ids").split(",");
for (String id : ids) {
this.illegal=illegalService.findOneIllegal(Integer.parseInt(id));
String studentno = this.illegal.getStudentno(); //获得学号
score = scoreService.findOneScore(studentno); //获取socre对象
int total = score.getTotal(); //获得原始积分
int thisScore = this.illegal.getScore(); //本次扣除积分
// 更新分数
score.setTotal(total + thisScore);
scoreService.updateScore(score);
illegalService.deleteIllegal(Integer.parseInt(id));
}
result.put("returnCode", 200);
result.put("delNums", ids.length);
} catch (Exception e) {
e.printStackTrace();
result.put("returnCode", "对不起,删除失败");
}
WriterUtil.write(response, result.toString());
}
}
// 具体的帖子内容
} catch (Exception e) {
e.printStackTrace();
result.put("returnCode", "对不起,删除失败");
}
WriterUtil.write(response, result.toString());
}
@RequestMapping("comboList")
public void comboList(HttpServletRequest request,HttpServletResponse response){
try {
List<Classes> list = classesService.findClasses(new Classes());
JSONArray array = new JSONArray();
array.addAll(list);
WriterUtil.write(response, array.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping("instituteList")
public void instituteList(HttpServletRequest request,HttpServletResponse response) {
JSONObject result=new JSONObject();
try {
List<Classes> institutelist = classesService.findInstitute();
result.put("institutelist", institutelist);
WriterUtil.write(response, result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
@RequestMapping("specialtyList")
public void specialtyList(HttpServletRequest request,HttpServletResponse response) {
JSONObject result=new JSONObject();
int instituteId=Integer.parseInt(request.getParameter("instituteId"));
try {
List<Classes> specialtylist = classesService.findSpecialty(instituteId);
result.put("specialtylist", specialtylist);
WriterUtil.write(response, result.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* 交流控制器
*/
@Controller
@RequestMapping("bbs")
public class BbsController {
private int page;
private int rows;
@Autowired
private BbsService<Bbs> bbsService;
private Bbs bbs;
@Autowired
private TopicService<Topic> topicService;
private Topic topic;
@RequestMapping("bbsIndex")
public String index(HttpServletRequest request) {
User currentUser = (User)request.getSession().getAttribute("currentUser");
if(currentUser.getRoleId()==1){
return "bbs/bbsIndexForAdmin";
} else {
return "bbs/bbsIndex";
}
}
/**
* 角色管理
*/
@Controller
@RequestMapping("role")
@SuppressWarnings("unchecked")
public class RoleController {
private int page;
private int rows;
private Role role;
private Operation operation;
@Autowired
private UserService<User> userService;
@Autowired
private RoleService<Role> roleService;
private Map map;
private Menu menu;
@Autowired
private MenuService<Menu> menuService;
@Autowired
private OperationService<Operation> operationService;
@Controller
@RequestMapping("book")
public class BookController {
private int page;
private int rows;
@Autowired
private BookService<Book> bookService;
private Book book;
@RequestMapping("bookIndex")
public String index(HttpServletRequest request){
User currentUser = (User)request.getSession().getAttribute("currentUser");
if(currentUser.getRoleId()==1){
return "tuijian/bookIndexForAdmin";
} else {
return "tuijian/bookIndex";
}
}
@RequestMapping("bookList")
public void bookList(HttpServletRequest request,HttpServletResponse response) {
try {
page = Integer.parseInt(request.getParameter("page"));
rows = Integer.parseInt(request.getParameter("limit"));
book = new Book();
book.setPage((page-1)*rows);
book.setRows(rows);
} catch (Exception e) {
log.setUserName("开发者");
}
log.setCreateTime(TimeUtil.formatTime(new Date(),"yyyy-MM-dd HH:mm:ss"));//操作时间
log.setContent(opContent);//操作内容
log.setOperation("添加");//操作
log.setIp(getIpAddr(request));
logService.insertLog(log);
}
/**
* 管理员修改操作日志(后置通知)
* @param joinPoint
* @param rtv
* @throws Throwable
*/
@AfterReturning(value="updateServiceCall()", argNames="rtv", returning="rtv")
public void updateServiceCallCalls(JoinPoint joinPoint, Object rtv) throws Throwable{
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
User currentUser = (User) request.getSession().getAttribute("currentUser");
//判断参数
if(joinPoint.getArgs() == null){//没有参数
return;
}
//获取方法名
String className = joinPoint.getArgs()[0].getClass().getName();
className = className.substring(className.lastIndexOf(".") + 1);
//获取操作内容
String opContent = adminOptionContent(joinPoint.getArgs(), "修改");
Log log = new Log();
log.setModule(className.toLowerCase());
if (currentUser!=null){
log.setUserName("");
}
log.setCreateTime(TimeUtil.formatTime(new Date(),"yyyy-MM-dd HH:mm:ss"));//操作时间
log.setContent(opContent);//操作内容
log.setOperation("修改");//操作
//logService.insertLog(log);//添加日志
}