Java項(xiàng)目:校園超市管理系統(tǒng)(java+SSM+Mysql+Maven+Bootstrap)

前端模板框架為Bootstrap,系統(tǒng)分為前臺和后臺。后臺主要為管理員角色,功能有:

商品類型管理、商品管理、訂單管理、會員管理、管理員管理等。前臺用戶功能有:登錄、注冊、查看商品、加入購物車、付款、查看訂單、個(gè)人中心等。該系統(tǒng)總共9張表

運(yùn)行環(huán)境:windows/linux、jdk1.8、mysql5.x、maven3.5\3.6、tomcat7.0

image-20211118212501232
image-20211118212521609
image-20211118212535704
image-20211118212547097
image-20211118212602292
image-20211118212616224

前端商品控制器:

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n980" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">/**

  • <p>
  • 前端控制器
  • </p>
    */
    @RestController
    @RequestMapping("/goods")
    public class GoodsController {

@Autowired
private GoodsService goodsService;

@Autowired
private ProviderService providerService;

@Autowired
private CategoryService categoryService;

/**

  • 商品模糊查詢
  • @param
  • @return
    */
    @SysLog("商品查詢操作")
    @RequestMapping("/goodsList")
    public DataGridViewResult goodsList(GoodsVO goodsVO) {
    //創(chuàng)建分頁信息 參數(shù)1 當(dāng)前頁 參數(shù)2 每頁顯示條數(shù)
    IPage<Goods> page = new Page<>(goodsVO.getPage(), goodsVO.getLimit());
    QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
    queryWrapper.eq(goodsVO.getProviderid() != null && goodsVO.getProviderid() != 0, "providerid", goodsVO.getProviderid());
    queryWrapper.like(!StringUtils.isEmpty(goodsVO.getGname()), "gname", goodsVO.getGname());
    IPage<Goods> goodsIPage = goodsService.page(page, queryWrapper);
    List<Goods> records = goodsIPage.getRecords();
    for (Goods goods : records) {
    Provider provider = providerService.getById(goods.getProviderid());
    if (null != provider) {
    goods.setProvidername(provider.getProvidername());
    }
    }
    return new DataGridViewResult(goodsIPage.getTotal(), records);
    }

/**

  • 添加商品信息
  • @param goods
  • @return
    */
    @SysLog("商品添加操作")
    @PostMapping("/addgoods")
    public Result addGoods(Goods goods) {
    String id = RandomStringUtils.randomAlphanumeric(8);
    if (goods.getGoodsimg()!=null&&goods.getGoodsimg().endsWith("_temp")){
    String newName = AppFileUtils.renameFile(goods.getGoodsimg());
    goods.setGoodsimg(newName);
    }
    goods.setGnumbering(id);
    boolean bool = goodsService.save(goods);
    if (bool) {
    return Result.success(true, "200", "添加成功!");
    }
    return Result.error(false, null, "添加失敗!");
    }

/**

  • 修改商品信息
  • @param goods
  • @return
    */
    @SysLog("商品修改操作")
    @PostMapping("/updategoods")
    public Result updateGoods(Goods goods) {
    //商品圖片不是默認(rèn)圖片
    if (!(goods.getGoodsimg()!=null&&goods.getGoodsimg().equals(Constast.DEFAULT_IMG))){
    if (goods.getGoodsimg().endsWith("_temp")){
    String newName = AppFileUtils.renameFile(goods.getGoodsimg());
    goods.setGoodsimg(newName);
    //刪除原先的圖片
    String oldPath = goodsService.getById(goods.getGid()).getGoodsimg();
    AppFileUtils.removeFileByPath(oldPath);
    }
    }
    boolean bool = goodsService.updateById(goods);
    if (bool) {
    return Result.success(true, "200", "修改成功!");
    }
    return Result.error(false, null, "修改失敗!");
    }

/**

  • 刪除單條數(shù)據(jù)
  • @param id
  • @return
    */
    @SysLog("商品刪除操作")
    @RequestMapping("/deleteOne")
    public Result deleteOne(int id) {

boolean bool = goodsService.removeById(id);
if (bool) {
return Result.success(true, "200", "刪除成功!");
}
return Result.error(false, null, "刪除失敗!");
}

/**

  • 根據(jù)id查詢當(dāng)前商品擁有的類別
  • @param id
  • @return
    */
    @RequestMapping("/initGoodsByCategoryId")
    public DataGridViewResult initGoodsByCategoryId(int id) {
    List<Map<String, Object>> mapList = null;
    try {
    //查詢所有類別列表
    mapList = categoryService.listMaps();
    //根據(jù)商品id查詢商品擁有的類別
    Set<Integer> cateIdList = categoryService.findGoodsByCategoryId(id);
    for (Map<String, Object> map : mapList) {
    //定義標(biāo)記 默認(rèn)不選中
    boolean flag = false;
    int cateId = (int) map.get("cateid");
    for (Integer cid : cateIdList) {
    if (cid == cateId) {
    flag = true;
    break;
    }
    }
    map.put("LAY_CHECKED", flag);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return new DataGridViewResult(Long.valueOf(mapList.size()), mapList);

}

/**

  • 根據(jù)商品id加載商品信息
  • @param goodsid
  • @return
    */
    @GetMapping("/loadGoodsById")
    public DataGridViewResult loadGoodsById(int goodsid) {

QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
goodsQueryWrapper.eq(goodsid != 0, "gid", goodsid);
Goods goods = goodsService.getById(goodsid);

return new DataGridViewResult(goods);

}

/**

  • 為商品分配類別
  • @param categoryids
  • @param goodsid
  • @return
    */
    @SysLog("類別添加操作")
    @RequestMapping("/saveGoodsCategory")
    public Result saveGoodsCategory(String categoryids, int goodsid) {

try {
if (goodsService.saveGoodsCategory(goodsid, categoryids)) {
return Result.success(true, null, "分配成功");
}

} catch (Exception e) {
e.printStackTrace();
}
return Result.error(false, null, "分配失敗");

}

/**

  • 加載下拉框
  • @return
    */
    @RequestMapping("/loadAllGoods")
    public DataGridViewResult loadAllGoods() {
    QueryWrapper<Goods> queryWrapper = new QueryWrapper<>();
    List<Goods> list = goodsService.list(queryWrapper);
    return new DataGridViewResult(list);

}

/**

  • 根據(jù)供應(yīng)商查商品下拉框
  • @param providerid
  • @return
    */
    @RequestMapping("/loadGoodsByProvidreId")
    public DataGridViewResult loadGoodsByProvidreId(Integer providerid) {
    QueryWrapper<Goods> goodsQueryWrapper = new QueryWrapper<>();
    goodsQueryWrapper.eq(providerid != null, "providerid", providerid);
    List<Goods> list = goodsService.list(goodsQueryWrapper);
    for (Goods goods : list) {
    Provider provider = providerService.getById(goods.getProviderid());
    if (null != provider) {
    goods.setProvidername(provider.getProvidername());
    }

}
return new DataGridViewResult(list);

}
}</pre>

前端銷售控制器:

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n985" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">/**

  • <p>
  • 前端控制器
  • </p>
    */
    @RestController
    @RequestMapping("/sale")
    public class SaleController {
    @Autowired
    private SaleService saleService;

@Autowired
private GoodsService goodsService;

@Autowired
private CustomerService customerService;

/**

  • 銷售查詢
  • @param
  • @return
    */
    @SysLog("銷售查詢操作")
    @RequestMapping("/saleList")
    public DataGridViewResult saleList(SaleVO saleVO) {

//創(chuàng)建分頁信息 參數(shù)1 當(dāng)前頁 參數(shù)2 每頁顯示條數(shù)
IPage<Sale> page = new Page<>(saleVO.getPage(), saleVO.getLimit());
QueryWrapper<Sale> queryWrapper = new QueryWrapper<>();
queryWrapper.like(!StringUtils.isEmpty(saleVO.getNumbering()),"numbering", saleVO.getNumbering());
queryWrapper.eq(saleVO.getGid() != null && saleVO.getGid() != 0, "gid", saleVO.getGid());
queryWrapper.ge(saleVO.getStartTime() != null, "buytime", saleVO.getStartTime());
queryWrapper.le(saleVO.getEndTime() != null, "buytime", saleVO.getEndTime());

queryWrapper.orderByDesc("buytime");

IPage<Sale> saleIPage = saleService.page(page, queryWrapper);

List<Sale> records = saleIPage.getRecords();

for (Sale sale : records) {
sale.setAllmoney(sale.getMoney()*sale.getBuyquantity());
Customer customer = customerService.getById(sale.getCustid());

if (null != customer) {
sale.setCustomervip(customer.getCustvip());
sale.setCustomername(customer.getCustname());
}
Goods goods = goodsService.getById(sale.getGid());
if (null != goods) {

sale.setGoodsname(goods.getGname());
sale.setGnumbering(goods.getGnumbering());
}
}

return new DataGridViewResult(saleIPage.getTotal(), records);

}

/**

  • 添加銷售單信息
  • @param sale
  • @return
    */
    @SysLog("銷售添加操作")
    @PostMapping("/addsale")
    public Result addsale(Sale sale, HttpSession session) {
    if (sale.getGid()==0){
    return Result.error(false, null, "添加失敗!未選商品");
    }
    Goods goods = goodsService.getById(sale.getGid());
    Integer gquantity = goods.getGquantity();
    if(gquantity<sale.getBuyquantity()){
    return Result.error(false, null, "添加失敗!庫存不足,庫存為:"+gquantity);
    }
    User user = (User) session.getAttribute("username");
    String num = RandomStringUtils.randomAlphanumeric(7);
    sale.setNumbering(num);
    sale.setPerson(user.getUsername());
    sale.setBuytime(new Date());
    sale.setRealnumber(sale.getBuyquantity());
    boolean bool = saleService.save(sale);
    if (bool) {
    return Result.success(true, "200", "添加成功!");
    }
    return Result.error(false, null, "添加失??!庫存不足");
    }

/**

  • 修改銷售單信息
  • @param sale
  • @return
    */
    @SysLog("銷售修改操作")
    @PostMapping("/updatesale")
    public Result updatesale(Sale sale, HttpSession session) {

User user = (User) session.getAttribute("username");
sale.setPerson(user.getUsername());
sale.setBuytime(new Date());
boolean bool = saleService.updateById(sale);
if (bool) {
return Result.success(true, "200", "修改成功!");
}
return Result.error(false, null, "修改失??!");
}
/**

  • 刪除單條數(shù)據(jù)
  • @param id
  • @return
    */
    @SysLog("銷售刪除操作")
    @RequestMapping("/deleteOne")
    public Result deleteOne(int id) {

boolean bool = saleService.removeById(id);
if (bool) {
return Result.success(true, "200", "刪除成功!");
}
return Result.error(false, null, "刪除失?。?);
}

}</pre>

前端用戶控制器:

<pre class="md-fences md-end-block ty-contain-cm modeLoaded" spellcheck="false" lang="java" cid="n990" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;">/**

  • <p>
  • 前端控制器
  • </p>

*/
@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
private UserService userService;

@Autowired
private RoleService roleService;

/**

  • 登錄
  • @param username 用戶名
  • @param password 密碼
  • @param request
  • @return
    */

@SysLog("登陸操作")
@PostMapping("/login")
public Result login(String username, String password, HttpServletRequest request) {
try {
//獲取當(dāng)前登錄主體對象
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken(username, password);
subject.login(token);
LoginUserVO userDTO = (LoginUserVO) subject.getPrincipal();
request.getSession().setAttribute("username", userDTO.getUser());
return Result.success(true, "200", "登錄成功");
} catch (UnknownAccountException e) {
e.printStackTrace();
return Result.error(false, "400", "登錄失敗,用戶名不存在");
}catch (IncorrectCredentialsException e) {
e.printStackTrace();
return Result.error(false, "400", "登錄失敗,密碼錯(cuò)誤");
}catch (AuthenticationException e) {
e.printStackTrace();
return Result.error(false, "400", "登錄失敗,賬戶禁用");
}
}

/**

  • 得到登陸驗(yàn)證碼
  • @param response
  • @param session
  • @throws IOException
    */
    @RequestMapping("/getCode")
    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {
    //定義圖形驗(yàn)證碼的長和寬
    LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,5);
    session.setAttribute("code",lineCaptcha.getCode());
    try {
    ServletOutputStream outputStream = response.getOutputStream();
    lineCaptcha.write(outputStream);
    outputStream.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

/**

  • 查詢所有用戶信息
  • @param userVO
  • @return
    /
    @SysLog("用戶查詢操作")
    @RequestMapping("/userList")
    public DataGridViewResult userList(UserVO userVO) {
    //分頁構(gòu)造函數(shù)
    IPage<User> page = new Page<>(userVO.getPage(), userVO.getLimit());
    QueryWrapper<User> queryWrapper = new QueryWrapper<>();
    queryWrapper.like(!StringUtils.isEmpty(userVO.getUsername()), "username", userVO.getUsername());
    queryWrapper.like(!StringUtils.isEmpty(userVO.getUname()), "uname", userVO.getUname());
    /
    *
  • 翻頁查詢
  • @param page 翻頁對象
  • @param queryWrapper 實(shí)體對象封裝操作類
    */
    IPage<User> userIPage = userService.page(page, queryWrapper);
    return new DataGridViewResult(userIPage.getTotal(), userIPage.getRecords());
    }

/**

  • 添加用戶信息
  • @param user
  • @return
    */
    @SysLog("用戶添加操作")
    @PostMapping("/adduser")
    public Result addRole(User user) {

user.setUcreatetime(new Date());
String salt = UUIDUtil.randomUUID();
user.setPassword(PasswordUtil.md5("000000", salt, 2));
user.setSalt(salt);
user.setType(1);
boolean bool = userService.save(user);

try {
if (bool) {
return Result.success(true, "200", "添加成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
return Result.error(false, null, "添加失?。?);
}

/**

  • 校驗(yàn)用戶名是否存在
  • @param username
  • @return
    */

@RequestMapping("/checkUserName")
public String checkUserName(String username) {
Map<String, Object> map = new HashMap<>();
try {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("username", username);
User user = userService.getOne(queryWrapper);
if (user != null) {
map.put("exist", true);
map.put("message", "用戶名已存在");
} else {
map.put("exist", false);
map.put("message", "用戶名可以使用");
}
} catch (Exception e) {
e.printStackTrace();
}
return JSON.toJSONString(map);
}

/**

  • 修改用戶信息
  • @param user
  • @return
    */
    @SysLog("用戶修改操作")
    @PostMapping("/updateuser")
    public Result updateUser(User user) {

boolean bool = userService.updateById(user);
try {
if (bool) {
return Result.success(true, "200", "修改成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
return Result.error(false, null, "修改失敗!");
}

/**

  • 刪除單條數(shù)據(jù)
  • @param id
  • @return
    */
    @SysLog("用戶刪除操作")
    @RequestMapping("/deleteOne")
    public Result deleteOne(int id) {
    boolean bool = userService.removeById(id);
    try {
    if (bool) {
    return Result.success(true, "200", "刪除成功!");
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return Result.error(false, null, "刪除失??!");
    }

/**

  • 重置密碼
  • @param id
  • @return
    */
    @SysLog("用戶修改操作")
    @PostMapping("/resetPwd")
    public Result resetPwd(int id) {

User user = new User();
String salt = UUIDUtil.randomUUID();
user.setUid(id);
user.setPassword(PasswordUtil.md5("000000", salt, 2));
user.setSalt(salt);
boolean bool = userService.updateById(user);

try {
if (bool) {
return Result.success(true, "200", "重置成功!");
}
} catch (Exception e) {
e.printStackTrace();
}
return Result.error(false, null, "重置失敗!");
}

/**

  • 根據(jù)id查詢當(dāng)前用戶擁有的角色
  • @param id
  • @return
    */
    @RequestMapping("/initRoleByUserId")
    public DataGridViewResult initRoleByUserId(int id) {
    List<Map<String, Object>> mapList = null;
    try {
    //查詢所有角色列表
    mapList = roleService.listMaps();
    //根據(jù)用戶id查詢用戶擁有的角色
    Set<Integer> roleIdList = userService.findRoleByUserId(id);
    for (Map<String, Object> map : mapList) {
    //定義標(biāo)記 默認(rèn)不選中
    boolean flag = false;
    int roleId = (int) map.get("roleid");
    for (Integer rid : roleIdList) {
    if (rid == roleId) {
    flag = true;
    break;
    }
    }
    map.put("LAY_CHECKED", flag);
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
    return new DataGridViewResult(Long.valueOf(mapList.size()), mapList);

}

/**

  • 為用戶分配角色
  • @param roleids
  • @param userid
  • @return
    */
    @SysLog("用戶添加操作")
    @RequestMapping("/saveUserRole")
    public Result saveUserRole(String roleids, int userid) {

try {
if (userService.saveUserRole(userid, roleids)) {
return Result.success(true, null, "分配成功");
}

} catch (Exception e) {
e.printStackTrace();
}
return Result.error(false, null, "分配失敗");

}

/**

  • 修改密碼
  • @param newPassWord1
  • @param newPassWord2
  • @return
    */
    @RequestMapping("/updateUserPassWord")
    public Result updateUserPassWord(String newPassWord1, String newPassWord2,HttpSession session) {
    User sessionUser = (User) session.getAttribute("username");

if (newPassWord1.equals(newPassWord2)){
User user = new User();
String salt = UUIDUtil.randomUUID();
user.setUid(sessionUser.getUid());
user.setPassword(PasswordUtil.md5(newPassWord1, salt, 2));
user.setSalt(salt);
boolean bool = userService.updateById(user);
if (bool){
return Result.success(true,null,"修改成功");
}else {
return Result.error(false,null,"修改失敗!");
}
}else {
return Result.error(false,null,"修改失敗,兩次密碼不一致!");
}

}

}
</pre>

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容