1. 程式人生 > >22、【收貨地址管理模組】——收貨地址增、刪、改、查、分頁列表、地址詳情的功能開發

22、【收貨地址管理模組】——收貨地址增、刪、改、查、分頁列表、地址詳情的功能開發

####1、介面開發:
新建ShippingController
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、收貨地址介面測試
image.png

#####2、收貨地址刪除的介面測試
image.png

#####3、收貨地址修改的介面測試
image.png

#####4、查詢地址介面測試
image.png

#####5、查詢所有地址介面測試
image.png
image.png