22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发

1、接口开发:

新建ShippingController

22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发
image.png

在类上添加相关注解

@Controller
@RequestMapping("/shipping/")
public class ShippingController {
  
}
1、收货地址的增加:

*Controller:

//添加地址接口
    @RequestMapping(value = "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(user.getId(), shipping);
    }

*Service:

 //收货地址添加方法
    ServerResponse add(Integer userId, Shipping shipping);

*ServiceImpl:

 //收货地址添加方法
    public ServerResponse add(Integer userId, Shipping shipping){
        shipping.setUserId(userId);
        shipping.setCreateTime(new Date());
        shipping.setUpdateTime(new Date());
        int rowCount=shippingMapper.insertSelective(shipping);
        if(rowCount>=0){
            Map result= Maps.newHashMap();
            result.put("shippingId",shipping.getId());
            return ServerResponse.createBySuccess("新建地址成功",result);
        }
        return ServerResponse.createByErrorMessage("新建地址失败");
    }

insertSelective是使用逆向工程生成的代码,所以直接调用即可。

2、收货地址删除的接口的开发:

*Controller:

 //删除地址接口
    @RequestMapping(value = "del.do")
    @ResponseBody
    public ServerResponse del(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.del(user.getId(), shippingId);
    }

*Service:

//删除收货地址方法
    ServerResponse del(Integer userId,Integer shippingId);

*ServiceImpl:

  //删除收货地址方法
    public ServerResponse del(Integer userId,Integer shippingId){

        int rowCount=shippingMapper.deleteByShippingIdByUserId(userId,shippingId);
        if(rowCount>0){
            return ServerResponse.createBySuccess("删除地址成功");
        }
        return ServerResponse.createByErrorMessage("删除地址失败");
    }

由于为了防止横向越权的问题,我们使用自己封装的deleteByShippingIdByUserId方法,在删除收货地址的时候,我们不仅判断收货地址的Id,同时还判断该收货地址是否是在当前用户下。
*Mapper:

//同时根据用户Id和地址Id来删除地址,防止横向越权
    int deleteByShippingIdByUserId(@Param("userId") Integer userId, @Param("shippongId") Integer shippongId);

*Mappler.xml:

<!--同时根据用户Id和地址Id来删除地址,防止横向越权-->
  <delete id="deleteByShippingIdByUserId" parameterType="map" >
    delete
    from mmall_shipping
    where user_id=#{userId}
    and id=#{shippongId}
  </delete>
3、收货地址修改的接口编写:

*Controller:

 //修改地址接口
    @RequestMapping(value = "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);
    }

*Service:

//修改地址接口
    ServerResponse update(Integer userId,Shipping shipping);

*ServiceImpl:

 //修改地址方法
    public ServerResponse update(Integer userId,Shipping shipping){


        shipping.setUserId(userId);
        Shipping selship=shippingMapper.selectByShippingIdByUserId(userId,shipping.getId());
        if(selship == null){
            return ServerResponse.createByErrorMessage("该用户不存在此地址");
        }else {

        int rowCount= shippingMapper.updateByshipping(shipping);
        if(rowCount>=0){
            Map result= Maps.newHashMap();
            result.put("shippingId",shipping.getId());
            return ServerResponse.createBySuccess("更新地址成功",result);
        }
        }
        return ServerResponse.createByErrorMessage("更新地址失败");
    }

updateByshipping方法:
*Mapper:

  //修改地址接口
    int updateByshipping(Shipping record);

*Mappler.xml:

<!--更新地址-->
  <update id="updateByshipping" parameterType="com.mmall.pojo.Shipping">
    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}
  </update>
4、查询地址接口:

*Controller:

   //查询地址接口
    @RequestMapping(value = "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(user.getId(), shippingId);
    }

*Service:

 //查询收货地址的方法
    ServerResponse<Shipping> select(Integer userId,Integer shippingId);

*ServiceImpl:

  //查询收货地址的方法
    public  ServerResponse<Shipping> select(Integer userId,Integer shippingId){
        Shipping shipping=shippingMapper.selectByShippingIdByUserId(userId,shippingId);
        if(shipping == null){
            return ServerResponse.createByErrorMessage("无法查询到该地址");
        }
        return ServerResponse.createBySuccess("查询地址成功",shipping);
    }

*Mapper:


```  //查询收货地址接口
    Shipping selectByShippingIdByUserId(@Param("userId") Integer userId, @Param("shippongId") Integer shippongId);

*Mappler.xml:

<select id="selectByShippingIdByUserId" resultMap="BaseResultMap" parameterType="map" >
    select
    <include refid="Base_Column_List"/>
    from mmall_shipping
    where id= #{shippongId}
    and user_id=#{userId}
  </select>
5、查询所有地址接口开发(带分页):

*Controller:

 //查询所有地址接口(带分页)
    @RequestMapping(value = "list.do")
    @ResponseBody
    public ServerResponse<PageInfo> list(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum, @RequestParam(value = "pageSize",defaultValue = "10") int pageSize, HttpSession session){
        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);
    }

*Service:

//查询所有收货地址的方法
    ServerResponse<PageInfo> list(Integer userId, int pageNum, int pageSize);

*ServiceImpl:

 //查询所有收货地址的方法
    public ServerResponse<PageInfo> list(Integer userId,int pageNum, int pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<Shipping> shippingList=shippingMapper.selectByUserId(userId);

        PageInfo pageInfo= new PageInfo(shippingList);
        return ServerResponse.createBySuccess(pageInfo);
    }

*Mapper:

 //查询所有收获地址接口
    List<Shipping> selectByUserId(Integer userId);

*Mappler.xml:

<select id="selectByUserId" resultMap="BaseResultMap" parameterType="map">
    select
    <include refid="Base_Column_List"/>
    from mmall_shipping
    where user_id=#{userId}
  </select>

2、接口测试:

1、收货地址接口测试
22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发
image.png
2、收货地址删除的接口测试
22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发
image.png
3、收货地址修改的接口测试
22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发
image.png
4、查询地址接口测试
22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发
image.png
5、查询所有地址接口测试
22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发
image.png

22、【收货地址管理模块】——收货地址增、删、改、查、分页列表、地址详情的功能开发
image.png
上一篇:《Node.js入门经典》一1.5 问与答


下一篇:IPv6 link-local 地址的功能和原理