商城项目服务端实践SSM(十)-------前台_收货地址接口

一、增加地址

  • controller
 @RequestMapping("add.do")
    @ResponseBody
    public ServerResponse add(HttpSession session, Shipping shipping){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
            return iShippingService.add(shipping,user.getId());
    }
  • impl
//新增地址
    public ServerResponse add(Shipping shipping,Integer userId){
       shipping.setUserId(userId);
       int rowCount=shippingMapper.insert(shipping);
       if (rowCount > 0){
           Map result= Maps.newHashMap();
           result.put("shippingId",shipping.getId());
           return ServerResponse.createBySuccess("新建地址成功",result);
       }
       return ServerResponse.createByErrorMessage("新建地址失败");
    }

二、修改地址

  • controller
    //修改地址
    @RequestMapping("update.do")
    @ResponseBody
    public ServerResponse update(HttpSession session,Shipping shipping){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.update(user.getId(),shipping);
    }
  • impl
//修改地址
    public ServerResponse update(Integer userId,Shipping shipping){
        shipping.setUserId(userId);
        int rowCount=shippingMapper.updateByShipping(shipping);
        if (rowCount>0){
            return ServerResponse.createBySuccess("修改地址成功");
        }
        return ServerResponse.createByErrorMessage("修改地址失败");
    }

三、删除地址

  • controller
//删除地址
    @RequestMapping("delete.do")
    @ResponseBody
    public ServerResponse delete(HttpSession session,Integer shippingId){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.delete(user.getId(),shippingId);
    }
  • impl
//删除地址
    public ServerResponse delete(Integer userId,Integer shippingId){
        int rowCount=shippingMapper.deleteByUserIdShippingId(userId,shippingId);
        if (rowCount>0){
            return ServerResponse.createBySuccess("删除地址成功");
        }
        return ServerResponse.createByErrorMessage("删除地址失败");
    }

四、选中查看具体的地址详情

  • controller
    //选中查看具体的地址详情
    @RequestMapping("select.do")
    @ResponseBody
    public ServerResponse select(HttpSession session,Integer shippingId){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.select(shippingId);
    }
  • impl
    public ServerResponse select(Integer shippingId){
        Shipping shipping=shippingMapper.selectByPrimaryKey(shippingId);
        if (shipping == null){
            return ServerResponse.createByErrorMessage("查看详情失败");
        }
        return ServerResponse.createBySuccess(shipping);
    }

五、地址列表

  • controller
    //地址列表
    @RequestMapping("list.do")
    @ResponseBody
    public ServerResponse list(HttpSession session, @RequestParam(value="pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10")int pageSize){
        User user= (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDesc());
        }
        return iShippingService.list(user.getId(),pageNum,pageSize);
    }
  • impl
    //地址列表
    public ServerResponse list(Integer userId,int pageNum,int pageSize){
        //设置起始页和每页数量大小
        PageHelper.startPage(pageNum,pageSize);
        //查询
        List shippingList=shippingMapper.selectByUserId(userId);
        //放在pageInfo里
        PageInfo pageInfo=new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }

六、Mybatis--Mapper.java

public interface ShippingMapper {

    int insert(Shipping record);

    Shipping selectByPrimaryKey(Integer id);
    //修改地址
    int updateByShipping(Shipping record);
    //根据用户Id和地址Id删除地址
    int deleteByUserIdShippingId(@Param(value = "userId") Integer userId,@Param(value = "shippingId") Integer shippingId);
    //地址列表
    List selectByUserId(@Param(value = "userId") Integer userId);
}

七、Mybatis--Mapper.xml




  
    
      
      
      
      
      
      
      
      
      
      
      
      
    
  
  
    id, user_id, receiver_name, receiver_phone, receiver_mobile, receiver_province, receiver_city, 
    receiver_district, receiver_address, receiver_zip, create_time, update_time
  
  

    insert into mmall_shipping (id, user_id, receiver_name, 
      receiver_phone, receiver_mobile, receiver_province, 
      receiver_city, receiver_district, receiver_address, 
      receiver_zip, create_time, update_time
      )
    values (#{id,jdbcType=INTEGER}, #{userId,jdbcType=INTEGER}, #{receiverName,jdbcType=VARCHAR}, 
      #{receiverPhone,jdbcType=VARCHAR}, #{receiverMobile,jdbcType=VARCHAR}, #{receiverProvince,jdbcType=VARCHAR}, 
      #{receiverCity,jdbcType=VARCHAR}, #{receiverDistrict,jdbcType=VARCHAR}, #{receiverAddress,jdbcType=VARCHAR}, 
      #{receiverZip,jdbcType=VARCHAR},now(),now()
      )
  
 
    delete from mmall_shipping
    where user_id=#{userId} and id=#{shippingId}
  
  
  
    update mmall_shipping
    set
      receiver_name = #{receiverName,jdbcType=VARCHAR},
      receiver_phone = #{receiverPhone,jdbcType=VARCHAR},
      receiver_mobile = #{receiverMobile,jdbcType=VARCHAR},
      receiver_province = #{receiverProvince,jdbcType=VARCHAR},
      receiver_city = #{receiverCity,jdbcType=VARCHAR},
      receiver_district = #{receiverDistrict,jdbcType=VARCHAR},
      receiver_address = #{receiverAddress,jdbcType=VARCHAR},
      receiver_zip = #{receiverZip,jdbcType=VARCHAR},
      create_time = #{createTime,jdbcType=TIMESTAMP},
      update_time = now()
    where id = #{id,jdbcType=INTEGER}
    and user_id = #{userId,jdbcType=INTEGER}
  

 

你可能感兴趣的:(商城项目实践)