前篇引入(新人必看):SSM图书管理系统(原版)
之前给大家分享过SSM图书管理系统项目的源码,热度较高,但我也发现功能并不是很全面,所以这次对系统进行了功能增强,以下是系统改动的几个方面
1、数据库改动
-
users表添加了两个字段:major(专业)、gender(性别)
-
books表添加了一个字段:times(书籍被借次数)
-
添加了log表,也就是记录日志的表
如果已经搭建过之前的项目,这里建议各位重新创建个数据库,名称为librarydb,避免跟之前的数据库冲突
2、普通用户页面,添加了个人中心菜单
个人中心,显示当前用户的基本信息,如用户名、密码、性别等属性,也可编辑修改
3、超级管理员页面,添加了日志管理和图表分析菜单
日志管理,所有角色登录系统后都会记录下来,日志记录了用户名、用户角色、浏览器类型、用户IP、登录时间。
下载项目文件夹后你会发现比以前多了util包,该包下的BrowserUtil和IpUtil就是获取浏览器类型和IP的工具类
在查询方面,可根据用户名精准查询
图表分析,以书籍分类为准,统计每个分类下书籍的数目,使用到了Echats技术,功能效果如图
Echarts官网:https://echarts.apache.org/zh/index.html
关于修改
当然如果你先只添加以上的某个功能,可以找到对应页面的代码,改变你要显示的菜单即可,下面以超级管理员菜单对应的sysadmin/index.jsp文件为例:
<script>
BUI.use('common/main',function(){
var config = [
{id:'1',menu:[
{text:'系统管理',items:[
{id:'3',text:'用户管理',href:'<%=request.getContextPath()%>/sysadmin/showUser'},
{id:'12',text:'书籍管理',href:'<%=request.getContextPath()%>/sysadmin/show'},
{id:'13',text:'类别管理',href:'<%=request.getContextPath()%>/sort/findAll'},
{id:'14',text:'登录日志',href:'<%=request.getContextPath()%>/sysadmin/log'}
]
},
{
text: '图表管理', items: [
{id: '15', text: '图表分析', href: '<%=request.getContextPath()%>/sysadmin/chart'},
]
}
]
},
];
new PageUtil.MainPage({
modulesConfig : config
});
});
</script>
核心代码
日志实体类:
package com.xian.pojo;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
//登录日志实体类
public class Log {
private Integer id;
private String code;
private String role;
private String browser;
private Timestamp datetime;
private String ip;
// 页面数据
private int start;
// 当前页数
private int currentPage;
public Log() {}
public Log(Integer id, String code, String role, String browser, Timestamp datetime, String ip) {
this.id = id;
this.code = code;
this.role = role;
this.browser = browser;
this.datetime = datetime;
this.ip = ip;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getBrowser() {
return browser;
}
public void setBrowser(String browser) {
this.browser = browser;
}
public Date getDatetime() {
return datetime;
}
public void setDatetime(Timestamp datetime) {
this.datetime = datetime;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@Override
public String toString() {
return "Log{" +
"id=" + id +
", code='" + code + '\'' +
", role='" + role + '\'' +
", browser='" + browser + '\'' +
", datetime=" + datetime +
", ip='" + ip + '\'' +
'}';
}
}
控制层PageController,当请求/login时,而且用户成功登录,就会调用LogService的redcord方法,对登录日志保存到数据库
package com.xian.controller;
@Controller
public class PageController {
@Autowired
private BooksService bookService;
@Autowired
private AdminService adminService;
@Autowired
private SysAdminService sysAdminService;
@Autowired
private SortService sortService;
@Autowired
private LogService logService;
@RequestMapping("/index")
public String toIndex() {
return "login";
}
@RequestMapping("/login")
public String login(Users user, int loginType, Model model, HttpServletRequest request, HttpSession session) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Timestamp timestamp = null;
try {
Date date = sdf.parse(sdf.format(new Date()));
timestamp = new Timestamp(date.getTime());
}catch (Exception e){
e.printStackTrace();
}
// 普通用户
if (loginType == 1) {
Users users = bookService.selectUser(user);
if (users != null) {
logService.record(new Log(null,user.getCode(),"普通用户", BrowserUtil.getBrower(request),timestamp, IpUtil.getIpAddr(request)));
session.setAttribute("user", users);
return "redirect:/user/index";
} else {
model.addAttribute("msg", "账号或密码错误");
return "login";
}
} else if (loginType == 2) {
Admin admin = adminService.getAdmin(user);
if (admin != null) {
session.setAttribute("admin", admin);
logService.record(new Log(null,admin.getCode(),"管理员", BrowserUtil.getBrower(request), timestamp, IpUtil.getIpAddr(request)));
return "redirect:/admin/index";
} else {
model.addAttribute("msg", "账号或密码错误");
return "login";
}
} else if (loginType == 3) {
SysAdmin sysAdmin = sysAdminService.getSysAdmin(user);
if (sysAdmin != null) {
session.setAttribute("sysAdmin", sysAdmin);
logService.record(new Log(null,sysAdmin.getCode(),"超级管理员", BrowserUtil.getBrower(request),timestamp, IpUtil.getIpAddr(request)));
return "redirect:/sysadmin/index";
} else {
model.addAttribute("msg", "账号或密码错误");
return "login";
}
}
return "login";
}
}
如何获取源码?
进入《亦码》小程序,查看SSM源码分类下即可获得