基于Java得黑马头条项目------day06

今天得主要内容是登录得后端得接口得开发俗称通用后端开发,下面是接口得定义和接口得内容,详细信息入下图所示

接口代码入下所示:

   public ResponseResult login(AdUser user) {
        if(StringUtils.isEmpty(user.getName())&&StringUtils.isEmpty(user.getPassword())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"用户和密码不能为空");
        }
        AdUser adUser = adUserMapper.selectByName(user.getName());
        if(adUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"用户不存在");
        }else{
            if(user.getPassword().equals(adUser.getPassword())){
                Map<String,Object> map = new HashMap<>();
                adUser.setPassword("");
                adUser.setSalt("");
                map.put("token", AppJwtUtil.getToken(adUser));
                map.put("user",adUser);
                return ResponseResult.okResult(map);
            }else{
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        }
    }

导入前端得工程进行测试,导入之前需要先把nodelk_moudle这个文件给删除了,然后在执行npm install ,npm run dev

执行之后得效果

默账号是wukong 密码是123 ,登录成功

下面是通用增删改查得封装

下面就是通用封装得DAO,Service,Controller,都比较简单

通用封装得Dao

public interface CommonDao {

    @Select("select * from ${tableName} limit #{start},#{size}")
    @ResultType(HashMap.class)
    List<HashMap> list(@Param("tableName") String tableName,@Param("start") int start,@Param("size") int size);

    @Select("select count(*) from ${tableName}")
    @ResultType(Integer.class)
    int listCount(@Param("tableName") String tableName);

    @Select("select * from ${tableName} where 1=1 ${where} limit #{start},#{size} ")//where ==> and name = 11  and password = ddd
    @ResultType(HashMap.class)
    List<HashMap> listForWhere(@Param("tableName") String tableName,@Param("where") String where,@Param("start") int start,@Param("size") int size);

    @Select("select * from ${tableName} where 1=1 ${where} ")//where ==> and name = 11  and password = ddd
    @ResultType(Integer.class)
    int listCountForWhere(@Param("tableName") String tableName,@Param("where") String where);

    @Update("update ${tableName} set ${sets} where 1=1 ${where}")//sets==>  name=xxx,password=xsdfd
    @ResultType(Integer.class)
    int update(@Param("tableName") String tableName,@Param("where") String where,@Param("sets") String sets);

    @Insert("insert into ${tableName} (${fileds}) values (${values})")
    @ResultType(Integer.class)
    int insert(@Param("tableName") String tableName,@Param("fileds") String fileds,@Param("values")String values);

    @Delete("delete from ${tableName} where 1 = 1 ${where} limit 1")
    @ResultType(Integer.class)
    int delete(@Param("tableName") String tableName,@Param("where") String where);
}

接下来是封装得通用list得Service方法主要是分页类得方法

public ResponseResult list(CommonDto dto) {
        if(!dto.getName().isList()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        String where = getWhere(dto);
        String tableName = dto.getName().name().toLowerCase();
        int start = (dto.getPage()-1)*dto.getSize();
        if(start<-1){
            start = 0;
        }
        List<?> list = null;
        int total = 0;
        if(StringUtils.isEmpty(where)){
            list = commonDao.list(tableName, start, dto.getSize());
            total = commonDao.listCount(tableName);
        }else{
            list = commonDao.listForWhere(tableName,where,start,dto.getSize());
            total = commonDao.listCountForWhere(tableName,where);
        }
        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("total",total);

        //后处理的bean
        doFilter(dto,"list");

        return ResponseResult.okResult(map);
      
    }

通用新增得接口信息

    @Override
    public ResponseResult update(CommonDto dto) {
        String model = dto.getModel();
        String where = getWhere(dto);
        String tableName = dto.getName().name().toLowerCase();
        if("add".equals(model)){
            if(StringUtils.isNotEmpty(where)){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"新增数据不能设置条件");
            }else{
                return addData(dto,tableName);
            }
        }else{
            if(StringUtils.isEmpty(where)){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"修改条件不能为空");
            }else{
                return updateData(dto,tableName,where);
            }
        }
    }

下面这个是更新得通用接口信息

 private ResponseResult updateData(CommonDto dto, String tableName, String where) {
        String sets = getSets(dto);
        if(!dto.getName().isUpdate()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        if(StringUtils.isEmpty(sets)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"修改的参数值不能为空");
        }
        int temp = commonDao.update(tableName, where, sets);
        if(temp>0){
            doFilter(dto,"update");
        }
        return ResponseResult.okResult(temp);
    }

最后一个就是通用删除得接口信息

 @Override
    public ResponseResult delete(CommonDto dto) {
        String where = getWhere(dto);
        String tableName = dto.getName().name().toLowerCase();
        if(!dto.getName().isDelete()){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
        }
        if(StringUtils.isEmpty(where)){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"删除条件不合法");
        }
        int temp = commonDao.delete(tableName, where);
        if(temp>0){
            doFilter(dto,"delete");
        }
        return ResponseResult.okResult(temp);
    }

最后是通用得Controller得封装得方法编写

    @PostMapping("/list")
    public ResponseResult list(@RequestBody CommonDto dto){
        return commonService.list(dto);
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody CommonDto dto){
        return commonService.update(dto);
    }

    @PostMapping("/delete")
    public ResponseResult delete(@RequestBody CommonDto dto){
        return commonService.delete(dto);
    }

前后端联调成功

新增

修改

删除

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值