20211227日记 开发springboot + vue + mybatis 棉花糖English 项目Day06

20211227日记 开发springboot + vue + mybatis 棉花糖English 项目Day06,第1张

2021/12/27日记 开发springboot + vue + mybatis 棉花糖English 项目Day06 后端部分 完成了系统用户的增删改查功能

controller

//通过查询条件获取所有用户
    @RequestMapping("/getAllAdminUser")
    public ResultObj getAllAdminUser(@RequestParam(required=false,defaultValue="",name = "username") String username,
                                           @RequestParam(required=false,defaultValue="",name ="nickname") String nickname,
                                           @RequestParam(required=false,defaultValue="",name ="role_id")String role_id,
                                           @RequestParam(required=false,defaultValue="5",name ="limit")String pageSize,
                                           @RequestParam(required=false,defaultValue="1",name ="page")String pageNum){
        Integer roleId;
        if(StringUtils.hasLength(role_id)){
            roleId = Integer.parseInt(role_id);
        }
        else{
            roleId = null;
        }
        return adminUserService.getAllAdminUser(username, nickname, roleId,Integer.parseInt(pageSize), Integer.parseInt(pageNum));
    }

    //改
    @RequestMapping("/updateAdminUser")
    public ResultObj updateAdminUser(@RequestBody AdminUser user){
        return adminUserService.updateAdminUser(user);
    }

    //删
    @RequestMapping("/deleteAdminUserByIds")
    public ResultObj deleteAdminUserByIds(@RequestBody ArrayList ids){
        return adminUserService.deleteAdminUserByIds(ids);
    }

    //增
    @RequestMapping("/addAdminUser")
    public ResultObj addAdminUser(@RequestBody AdminUser user){
        return adminUserService.addAdminUser(user);
    }

serviceimpl

@Override
    public ResultObj getAllAdminUser(String username, String nickname, Integer role_id, Integer pageSize, Integer pageNum) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List adminUsers = adminUserDao.getAllAdminUser(username, nickname, role_id);
            PageInfo pageInfo = new PageInfo<>(adminUsers);
            return new ResultObj(Constant.OK, Constant.QUERY_SUCCESS, pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultObj(Constant.ERROR, Constant.QUERY_ERROR, null);
        }
    }

    @Override
    public ResultObj updateAdminUser(AdminUser user) {
        try {
            boolean flag = adminUserDao.updateAdminUser(user);
            if (flag) {
                return new ResultObj(Constant.OK, Constant.UPDATE_SUCCESS, null);
            } else {
                return new ResultObj(Constant.ERROR, Constant.UPDATE_ERROR, null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultObj(Constant.ERROR, Constant.UPDATE_ERROR, null);
        }
    }

    @Override
    @Transactional
    public ResultObj deleteAdminUserByIds(List ids) {
        try {
            boolean flag = adminUserDao.deleteAdminUserByIds(ids);
            if (flag) {
                return new ResultObj(Constant.OK, Constant.DELETE_SUCCESS, null);
            } else {
                return new ResultObj(Constant.ERROR, Constant.DELETE_ERROR, null);
            }
        } catch (Exception e) {
            return new ResultObj(Constant.ERROR, Constant.DELETE_ERROR, null);
        }
    }

    @Override
    public ResultObj addAdminUser(AdminUser user) {
        String salt = PasswordUtils.getSalt();
        String password = PasswordUtils.encode(user.getPassword(),salt);
        user.setSalt(salt);
        user.setPassword(password);
        try {
            boolean flag = adminUserDao.addAdminUser(user);
            if(flag){
                return new ResultObj(Constant.OK, Constant.ADD_SUCCESS,null);
            }
            else{
                return new ResultObj(Constant.ERROR, Constant.ADD_ERROR,null);
            }
        }catch (Exception e) {
            e.printStackTrace();
            return new ResultObj(Constant.ERROR, Constant.ADD_ERROR, null);
        }
    }

mapper