基于javaweb+mysql的springboot+mybatis在线商城水果蔬菜商城果蔬商城(前台、后台)(java+springboot+ssm+freemarker+redis+mysql)
运行环境
Java≥8、MySQL≥5.7
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
该项目分为前台用户和后台管理员两个角色,
用户角色的功能:登录(JWT的token验证)、注册、浏览商品、修改个人信息(上传图片)、修改密码、评论商品、添加商品到购物车(Redis缓存)、提交订单、查看订单、收藏商品等等功能。
管理员角色的功能:管理用户信息、管理用户评论信息、管理商品信息、管理订单信息等等功能。
前台:
后台:
技术框架
SpringBoot SpringMVC MyBatis FreeMarker JWT Redis
基于javaweb+mysql的SpringBoot+MyBatis在线商城水果蔬菜商城果蔬商城(前台、后台)(java+springboot+ssm+freemarker+redis+mysql)
}
}
}
log.info("当前URL={}", requestURI);
log.info("当前路径获取到的token={}", token);
try{
//验证token
DecodedJWT decodedJWT = JWTUtil.verifyToken(token);
String id = decodedJWT.getClaim("id").asString();
request.setAttribute("id", decodedJWT.getClaim("id").asString());
request.setAttribute("username", decodedJWT.getClaim("username").asString());
request.setAttribute("email", decodedJWT.getClaim("email").asString());
request.setAttribute("phone", decodedJWT.getClaim("phone").asString());
request.setAttribute("headPic", decodedJWT.getClaim("headPic").asString());
request.setAttribute("cartTotal", cartService.total(Long.valueOf(id)).getData()); //获取购物车商品种类
}catch(Exception e){
e.printStackTrace();
//token失效或非法
//判断该路径要不要验证
for(String str : RuntimeConstant.userNotNeedConfirmUrl){
if(requestURI.equals(str)){
temp = true; //该路径不需要验证
}
}
if(temp == false) {
//该路径需要验证,并进行是否是ajax请求判断
//首先判断是否是ajax请求
if (StringUtil.isAjax(request)) {
//表示是ajax请求
try {
response.setCharacterEncoding("UTF-8");
//JSON.parseObject,是将Json字符串转化为相应的对象;JSON.toJSONString则是将对象转化为Json字符串。
response.getWriter().write(JSON.toJSONString(CodeMsg.USER_SESSION_EXPIRED));
} catch (IOException e2) {
e2.printStackTrace();
}
return false;
}
//不是ajax请求,直接跳转页面
try {
log.info("没有登录或token非法,跳转登录界面!当前URL={}",requestURI);
response.sendRedirect("/home/user/login");
} catch (IOException e3) {
e3.printStackTrace();
}
return false;
}
}
* @param prePassword
* @param newPassword
* @param reNewPassword
* @param request
* @return
*/
@RequestMapping(value="/update_passwd",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> updatePasswd(String prePassword, String newPassword, String reNewPassword, HttpServletRequest request){
return userService.updatePasswd(prePassword, newPassword, reNewPassword, request);
}
}
package com.demo.controller.admin;
/**
*/
/**
* 后台管理系统商品控制器
* 用户个人信息修改操作处理
* @param user
* @return
*/
@RequestMapping(value="/update_info",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<String> updateInfo(User user){
return userService.updateInfo(user);
}
/**
* 用户修改密码操作处理
* @param prePassword
* @param newPassword
* @param reNewPassword
* @param request
* @return
*/
@RequestMapping(value="/update_passwd",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> updatePasswd(String prePassword, String newPassword, String reNewPassword, HttpServletRequest request){
return userService.updatePasswd(prePassword, newPassword, reNewPassword, request);
}
}
package com.demo.controller.admin;
for(OrderItem orderItem : order.getOrderItemList()){
productIdSet.add(orderItem.getProductId());
}
//获取这些选中商品的详细信息
List<Product> productList = productMapper.selectByProductIdSet(productIdSet);
//分别比较订单中商品购买数量是否大于库存
for(OrderItem orderItem : order.getOrderItemList()){
for(Product product : productList){
if(orderItem.getProductId().equals(product.getId())){
if(orderItem.getQuantity() > product.getStock()){
codeMsg.setMsg("商品<"+product.getProductName()+">库存不足了,请减少购买数量!");
return ResponseVo.errorByMsg(codeMsg);
}
}
}
}
//减少商品库存和增加商品销量
for(OrderItem orderItem : order.getOrderItemList()){
for(Product product : productList){
if(orderItem.getProductId().equals(product.getId())){
product.setStock(product.getStock() - orderItem.getQuantity());
product.setSellNum(product.getSellNum() + orderItem.getQuantity());
}
}
}
//修改数据库中商品库存和商品销量信息
for(Product product : productList){
if(productMapper.updateByPrimaryKeySelective(product) <= 0){
throw new RuntimeException("商品库存信息更新失败!");
}
}
//修改订单信息
order.setAddressId(address.getId());
order.setRemark(remark);
order.setState(OrderStateEnum.PAYED.getCode());
//修改数据库中的订单信息
if(orderMapper.updateByPrimaryKeySelective(order) <= 0){
throw new RuntimeException("订单提交失败!");
}
//清除购物车中选中的商品(redis有事务,但不能回滚,放最后处理)
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, Long.valueOf(uid));
for(OrderItem orderItem : order.getOrderItemList()){
String value = opsForHash.get(redisKey, String.valueOf(orderItem.getProductId()));
//删除商品
opsForHash.delete(redisKey, String.valueOf(orderItem.getProductId()));
}
//发送邮件提醒用户订单提交成功
//MailUtil.sendMail(MailTypeEnum.ORDER_SUBMIT.getCode(), email, String.valueOf(order.getOrderNo()));
return ResponseVo.successByMsg(true, "订单提交成功,交易完成!");
}
@Override
@Autowired
private AdminMapper adminMapper;
@Autowired
private AuthorityMapper authorityMapper;
@Autowired
private IOrderService orderService;
/**
* 订单列表页面
* @param model
* @param id
* @param request
* @param content
* @param pageNum
* @param pageSize
* @return
*/
@RequestMapping(value="/index",method= RequestMethod.GET)
public String index(Model model, Integer id, HttpServletRequest request, String content,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
) {
//获取列表展示有关信息
if(StringUtil.isEmpty(content)) {
//如果查询信息为空
model.addAttribute("PageInfo", orderService.selectByPage(pageNum, pageSize).getData());
}else {
model.addAttribute("PageInfo", orderService.selectByPageAndContent(Long.valueOf(content), pageNum, pageSize).getData());
model.addAttribute("content",content);
}
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
return "admin/order/index";
}
/**
* 后台管理系统系统控制器
*
*/
@RequestMapping("/admin/system")
@Controller
public class SystemController {
@Autowired
private AdminMapper adminMapper;
@Autowired
private AuthorityMapper authorityMapper;
/**
* 系统登录页面
* @param model
* @return
*/
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(Model model) {
return "admin/system/login";
}
/**
* 个人信息页面
* @param model
* @return
*/
@RequestMapping(value="/person_info",method=RequestMethod.GET)
@Autowired
private AdminMapper adminMapper;
@Autowired
private AnnouncementMapper announcementMapper;
@Autowired
private MailMapper mailMapper;
@Autowired
private AttachmentMapper attachmentMapper;
@Autowired
private AuthorityMapper authorityMapper;
@Autowired
private IOrderService orderService;
/**
* 系统首页页面
* @param model
* @return
*/
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Model model,HttpServletRequest request) {
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("FirstMenus",menuService.getFirstMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("SecondMenus",menuService.getSecondMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("ThirdMenus",menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
return "admin/index/index";
}
/**
* 系统欢迎页面
* @param model
* @return
*/
@RequestMapping(value="/welcome",method=RequestMethod.GET)
public String welcome(Model model){
model.addAttribute("TodayOrder", orderService.getOrderByDay().size());
model.addAttribute("announcementTotal", announcementMapper.getTotal());
model.addAttribute("mailTotal", mailMapper.getTotal());
model.addAttribute("WeekOrder", orderService.getOrderByWeek().size());
model.addAttribute("attachmentTotal", attachmentMapper.getTotal());
model.addAttribute("MonthOrder", orderService.getOrderByMonth().size());
/**
* 管理员删除处理
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> delete(Integer id){
return adminService.delete(id);
}
/**
* 管理员更改状态处理
* @param id
* @return
*/
@RequestMapping(value="/change_state",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> chageState(Integer id){
return adminService.chageState(id);
}
}
package com.demo.controller.home;
* @param model
* @param id
* @param request
* @param content
* @param pageNum
* @param pageSize
* @return
*/
@RequestMapping(value="/index",method= RequestMethod.GET)
public String index(Model model, Integer id, HttpServletRequest request, String content,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
) {
//获取列表展示有关信息
if(StringUtil.isEmpty(content)) {
//如果查询信息为空
model.addAttribute("PageInfo", productCategoryService.getProductCategoryByPage(pageNum, pageSize).getData());
}else {
model.addAttribute("PageInfo", productCategoryService.getProductCategoryByPageAndContent(pageNum, pageSize, content).getData());
model.addAttribute("content",content);
}
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
return "admin/product_category/index";
}
/**
* 商品种类添加页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model) {
return "admin/product_category/add";
}
public ResponseVo<Boolean> removeAttachment(Integer Id){
return mailService.removeAttachment(Id);
}
/**
* 发送邮件
* @param mail
* @return
*/
@RequestMapping(value="/send_mail",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> sendMail(Mail mail,String receivers,HttpServletRequest request){
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
return mailService.sendMail(mail, receivers, loginedAdmin.getId());
}
/**
* 邮件删除处理
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> delete(Integer id,HttpServletRequest request){
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
return mailService.delete(id, loginedAdmin.getId());
}
}
package com.demo.controller.admin;
//model.addAttribute("OrderList", orderMapper.selectByUserIdAndIsDeleted(Long.valueOf(uid), 0));
model.addAttribute("Send", orderService.selectByOrderStateAndUserIdAndIsDeleted(OrderStateEnum.SEND.getCode(), Long.valueOf(uid), OrderDeleteEnum.NO.getCode()).size());
model.addAttribute("Sign", orderService.selectByOrderStateAndUserIdAndIsDeleted(OrderStateEnum.SIGN.getCode(), Long.valueOf(uid), OrderDeleteEnum.NO.getCode()).size());
return "/home/user/order";
}
/**
* 用户登录操作处理
* @param username
* @param password
* @return
*/
@RequestMapping(value="/login",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> login(String username, String password){
return userService.login(username, password);
}
/**
* 用户注册操作处理
* @param user
* @param repassword
* @param cpacha
* @param request
* @return
*/
@RequestMapping(value="/register",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> register(User user, String repassword, String cpacha, HttpServletRequest request) {
return userService.register(user, repassword, cpacha, request);
}
/**
* 用户个人信息修改操作处理
* @param user
* @return
*/
@RequestMapping(value="/update_info",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<String> updateInfo(User user){
return userService.updateInfo(user);
}
/**
* 用户修改密码操作处理
return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_EMPTY);
}
//获取当前登录用户的id
String id = (String) request.getAttribute("id");
//判断用户输入的旧密码是否正确
User user = userMapper.selectByPrimaryKey(Long.valueOf(id));
if(!prePassword.equals(user.getPassword())){
return ResponseVo.errorByMsg(CodeMsg.USER_PREPASSWORD_ERROR);
}
//判断用户输入的新密码是否符合规范
user.setPassword(newPassword);
CodeMsg validate = ValidateEntityUtil.validate(user);
if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
return ResponseVo.errorByMsg(validate);
}
//判断用户输入的新密码和确认新密码是否一致
if(!newPassword.equals(reNewPassword)){
return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_ERROR);
}
//修改数据库中的用户密码信息
if(userMapper.updateByPrimaryKeySelective(user) <= 0){
return ResponseVo.errorByMsg(CodeMsg.USER_PASSWORD_EDIT_ERROR);
}
return ResponseVo.successByMsg(true, "修改密码成功!");
}
@Override
public ResponseVo<PageInfo> getUserByPage(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.selectAll();
PageInfo pageInfo = new PageInfo<>(userList);
pageInfo.setList(userList);
return ResponseVo.success(pageInfo);
}
@Override
public ResponseVo<PageInfo> getUserByPageAndContent(Integer pageNum, Integer pageSize, String content) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.selectBySearchContent(content);
PageInfo pageInfo = new PageInfo<>(userList);
pageInfo.setList(userList);
return ResponseVo.success(pageInfo);
}
@Override
public User selectByPrimaryKey(Long id) {
return userMapper.selectByPrimaryKey(id);
return ResponseVo.errorByMsg(CodeMsg.USER_RENEWPASSWORD_ERROR);
}
//修改数据库中的用户密码信息
if(userMapper.updateByPrimaryKeySelective(user) <= 0){
return ResponseVo.errorByMsg(CodeMsg.USER_PASSWORD_EDIT_ERROR);
}
return ResponseVo.successByMsg(true, "修改密码成功!");
}
@Override
public ResponseVo<PageInfo> getUserByPage(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.selectAll();
PageInfo pageInfo = new PageInfo<>(userList);
pageInfo.setList(userList);
return ResponseVo.success(pageInfo);
}
@Override
public ResponseVo<PageInfo> getUserByPageAndContent(Integer pageNum, Integer pageSize, String content) {
PageHelper.startPage(pageNum, pageSize);
List<User> userList = userMapper.selectBySearchContent(content);
PageInfo pageInfo = new PageInfo<>(userList);
pageInfo.setList(userList);
return ResponseVo.success(pageInfo);
}
@Override
public User selectByPrimaryKey(Long id) {
return userMapper.selectByPrimaryKey(id);
}
@Override
public ResponseVo<Boolean> updateUserPasswd(String passwd, Long userId) {
if(StringUtil.isEmpty(passwd) || userId == null){
return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
}
User user = userMapper.selectByPrimaryKey(userId);
user.setPassword(passwd);
//统一表单验证
CodeMsg validate = ValidateEntityUtil.validate(user);
if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
return ResponseVo.errorByMsg(validate);
}else {
model.addAttribute("PageInfo", announcementService.getAnnouncementByPageAndContent(pageNum, pageSize, content).getData());
model.addAttribute("content",content);
}
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
return "admin/announcement/index";
}
/**
* 公告添加页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model) {
return "admin/announcement/add";
}
/**
* 公告添加表单处理
* @param announcement
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> add(Announcement announcement, HttpServletRequest request){
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
return announcementService.add(announcement, loginedAdmin.getId());
}
/**
* 公告删除处理
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> delete(Integer id){
return announcementService.delete(id);
@RequestMapping("/admin/mail")
@Controller
public class MailController {
@Autowired
private MenuMapper menuMapper;
@Autowired
private IMenuService menuService;
@Autowired
private AdminMapper adminMapper;
@Autowired
private AttachmentMapper attachmentMapper;
@Autowired
private MailMapper mailMapper;
@Autowired
private IMailService mailService;
@Autowired
private AuthorityMapper authorityMapper;
/**
* 发送邮件页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/write",method=RequestMethod.GET)
public String index(Model model,Integer id,HttpServletRequest request) {
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
}
String[] split = ids.split(",");
Set<Long> productIdSet = new HashSet<>();
for(String id : split){
productIdSet.add(Long.valueOf(id));
}
//获取选中商品的详细信息
List<Product> productList = productMapper.selectByProductIdSet(productIdSet);
//声明一个OrderItem类型的List和OrderItem对象
List<OrderItem> orderItemList = new ArrayList<>();
OrderItem orderItem;
//从redis中获取这些选中商品的数据
HashOperations<String, String, String> opsForHash = redisTemplate.opsForHash();
//获取当前登录用户的id
String uid = (String) request.getAttribute("id");
String redisKey = String.format(CART_REDIS_KEY_TEMPLATE, Long.valueOf(uid));
//获取指定redisKey的所有商品
Map<String, String> entries = opsForHash.entries(redisKey);
//订单总价
BigDecimal orderTotalPrice = BigDecimal.ZERO;
//遍历购物车所有商品,把选中的添加到orderItemList中
for(Map.Entry<String,String> entry : entries.entrySet()){
Cart cart = gson.fromJson(entry.getValue(), Cart.class);
for(Product product : productList){
if(product.getId().equals(cart.getProductId())){
orderItem = new OrderItem(product.getId(), cart.getQuantity(),
product.getProductName(),
product.getProductPic(),
product.getPrice(),
product.getPrice().multiply(BigDecimal.valueOf(cart.getQuantity())));
orderItemList.add(orderItem);
orderTotalPrice = orderTotalPrice.add(product.getPrice().multiply(BigDecimal.valueOf(cart.getQuantity())));
}
}
}
//声明一个订单对象
Order order = new Order(new SnowFlake(2,3).nextId(),
Long.valueOf(uid),
OrderStateEnum.NO_PAY.getCode(),
orderTotalPrice);
//把订单信息添加到数据库中
if(orderMapper.insertSelective(order) <= 0){
return ResponseVo.errorByMsg(CodeMsg.ORDER_ADD_ERROR);
}
//给订单详情对应的订单id赋值
for(OrderItem or : orderItemList){
or.setOrderId(order.getId());
}
//把订单详情信息添加到数据库中
if(orderItemMapper.batchInsert(orderItemList) <= 0){
* @return
*/
@RequestMapping(value="/send_mail",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> sendMail(Mail mail,String receivers,HttpServletRequest request){
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
return mailService.sendMail(mail, receivers, loginedAdmin.getId());
}
/**
* 邮件删除处理
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> delete(Integer id,HttpServletRequest request){
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
return mailService.delete(id, loginedAdmin.getId());
}
}
package com.demo.controller.admin;
return roleService.saveAuthority(ids, roleId);
}
/**
* 角色添加表单处理
* @param role
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> add(Role role){
return roleService.add(role);
}
/**
* 角色编辑表单处理
* @param role
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> edit(Role role){
return roleService.edit(role);
}
/**
* 角色删除处理
* @param id
* @return
*/
@RequestMapping(value="/delete",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> delete(Integer id){
return roleService.delete(id);
}
}
package com.demo.controller.admin;
/**
* 后台管理系统管理员控制器
*
*/
@RequestMapping("/admin/admin")
@Controller
public class AdminController {
@Autowired
private IMenuService menuService;
@Autowired
private MenuMapper menuMapper;
@Autowired
private AdminMapper adminMapper;
@Autowired
private IAdminService adminService;
@Autowired
private RoleMapper roleMapper;
@Autowired
private AuthorityMapper authorityMapper;
/**
* 管理员列表页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Model model,Integer id,String name,HttpServletRequest request,
@RequestParam(required = false, defaultValue = "1") Integer pageNum,
@RequestParam(required = false, defaultValue = "5") Integer pageSize //每页5个数据
) {
//获取列表展示有关信息
if(StringUtil.isEmpty(name)) {
//如果查询信息为空
model.addAttribute("PageInfo", adminService.getAdminListByPage(pageNum, pageSize).getData());
}else {
model.addAttribute("PageInfo", adminService.getAdminListByPageAndName(pageNum, pageSize, name).getData());
model.addAttribute("name",name);
}
model.addAttribute("RoleList", roleMapper.selectAll());
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
model.addAttribute("content",content);
}
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
return "admin/order/index";
}
/**
* 订单详情查看页面
* @param model
* @param orderId
* @return
*/
@RequestMapping(value="/view",method=RequestMethod.GET)
public String view(Model model, Long orderId) {
model.addAttribute("OrderItemList", orderService.getOrderItemByOrderId(orderId));
model.addAttribute("Order", orderService.selectByPrimaryKey(orderId));
return "admin/order/view";
}
/**
* 修改订单状态页面
* @param model
* @param orderId
* @return
*/
@RequestMapping(value="/edit_state",method=RequestMethod.GET)
public String editState(Model model, Long orderId) {
model.addAttribute("Order", orderService.selectByPrimaryKey(orderId));
return "admin/order/edit_state";
}
List<User> userList = userMapper.selectBySearchContent(content);
PageInfo pageInfo = new PageInfo<>(userList);
pageInfo.setList(userList);
return ResponseVo.success(pageInfo);
}
@Override
public User selectByPrimaryKey(Long id) {
return userMapper.selectByPrimaryKey(id);
}
@Override
public ResponseVo<Boolean> updateUserPasswd(String passwd, Long userId) {
if(StringUtil.isEmpty(passwd) || userId == null){
return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
}
User user = userMapper.selectByPrimaryKey(userId);
user.setPassword(passwd);
//统一表单验证
CodeMsg validate = ValidateEntityUtil.validate(user);
if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())) {
return ResponseVo.errorByMsg(validate);
}
//修改数据库中的用户信息
if(userMapper.updateByPrimaryKeySelective(user) <= 0){
return ResponseVo.errorByMsg(CodeMsg.USER_PASSWORD_EDIT_ERROR);
}
return ResponseVo.successByMsg(true, "修改用户密码成功!");
}
@Override
public ResponseVo<Boolean> deleteUser(Long userId) {
if(userId == null){
return ResponseVo.errorByMsg(CodeMsg.DATA_ERROR);
}
User user = userMapper.selectByPrimaryKey(userId);
if(user == null){
return ResponseVo.errorByMsg(CodeMsg.USER_NOT_EXIST);
}
if(userMapper.deleteByPrimaryKey(userId) <= 0){
return ResponseVo.errorByMsg(CodeMsg.USER_DELETE_ERROR);
}
return ResponseVo.successByMsg(true, "删除用户成功!");
}
}
package com.demo.controller.common;
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
return "admin/product/index";
}
/**
* 商品添加页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model) {
model.addAttribute("productCategoryList", productCategoryMapper.selectAll());
return "admin/product/add";
}
/**
* 商品编辑页面
* @param model
* @param id
* @return
*/
@RequestMapping(value="/edit",method=RequestMethod.GET)
public String edit(Model model,Long id) {
Product selectByPrimaryKey = productMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
model.addAttribute("productCategoryList", productCategoryMapper.selectAll());
model.addAttribute("Product", selectByPrimaryKey);
return "admin/product/edit";
}
}
}
package com.demo.controller.admin;
/**
*/
/**
* 后台管理系统商品种类控制器
*
*/
@RequestMapping("/admin/product_category")
@Controller
public class ProductCategoryController {
@Autowired
private IMenuService menuService;
/**
* 后台管理系统系统控制器
*
*/
@RequestMapping("/admin/system")
@Controller
public class SystemController {
@Autowired
private AdminMapper adminMapper;
@Autowired
private AuthorityMapper authorityMapper;
/**
* 系统登录页面
* @param model
* @return
*/
@RequestMapping(value="/login",method=RequestMethod.GET)
public String login(Model model) {
return "admin/system/login";
}
/**
* 个人信息页面
* @param model
* @return
*/
@RequestMapping(value="/person_info",method=RequestMethod.GET)
public String personInfo(Model model) {
return "admin/system/person_info";
}
//获取路径上有关信息
Menu selectByPrimaryKey = menuMapper.selectByPrimaryKey(id);
if(selectByPrimaryKey == null) {
return "error/404";
}
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("allAdmins", adminMapper.selectAll());
model.addAttribute("onThirdMenus", menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("parentMenu", menuMapper.selectByPrimaryKey(selectByPrimaryKey.getParentId()));
model.addAttribute("currentMenu", selectByPrimaryKey);
return "admin/announcement/index";
}
/**
* 公告添加页面
* @param model
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.GET)
public String add(Model model) {
return "admin/announcement/add";
}
/**
* 公告添加表单处理
* @param announcement
* @return
*/
@RequestMapping(value="/add",method=RequestMethod.POST)
@ResponseBody
public ResponseVo<Boolean> add(Announcement announcement, HttpServletRequest request){
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
return announcementService.add(announcement, loginedAdmin.getId());
}
@Controller
public class IndexController {
@Autowired
private IMenuService menuService;
@Autowired
private IAnnouncementService announcementService;
@Autowired
private MenuMapper menuMapper;
@Autowired
private AdminMapper adminMapper;
@Autowired
private AnnouncementMapper announcementMapper;
@Autowired
private MailMapper mailMapper;
@Autowired
private AttachmentMapper attachmentMapper;
@Autowired
private AuthorityMapper authorityMapper;
@Autowired
private IOrderService orderService;
/**
* 系统首页页面
* @param model
* @return
*/
@RequestMapping(value="/index",method=RequestMethod.GET)
public String index(Model model,HttpServletRequest request) {
Admin loginedAdmin = (Admin) request.getSession().getAttribute(SessionConstant.SESSION_ADMIN_LOGIN_KEY);
List<Authority> selectByRoleId = authorityMapper.selectByRoleId(loginedAdmin.getRoleId()); //获取当前用户所有权限
Set<Integer> menuIdSet = selectByRoleId.stream().map(Authority :: getMenuId).collect(Collectors.toSet());//把权限中所有菜单id取出来
List<Menu> allMenusByStateAndPrimaryKeys = menuMapper.selectByStateAndPrimaryKeys(MenuStateEnum.OPEN.getCode(), menuIdSet);
model.addAttribute("FirstMenus",menuService.getFirstMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("SecondMenus",menuService.getSecondMenus(allMenusByStateAndPrimaryKeys).getData());
model.addAttribute("ThirdMenus",menuService.getThirdMenus(allMenusByStateAndPrimaryKeys).getData());
return "admin/index/index";
}
/**
* 系统欢迎页面
* @param model
* @return