天天看點

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、【收貨位址管理子產品】——收貨位址增、删、改、查、分頁清單、位址詳情的功能開發
2、收貨位址删除的接口測試
22、【收貨位址管理子產品】——收貨位址增、删、改、查、分頁清單、位址詳情的功能開發
3、收貨位址修改的接口測試
22、【收貨位址管理子產品】——收貨位址增、删、改、查、分頁清單、位址詳情的功能開發
4、查詢位址接口測試
22、【收貨位址管理子產品】——收貨位址增、删、改、查、分頁清單、位址詳情的功能開發
5、查詢所有位址接口測試
22、【收貨位址管理子產品】——收貨位址增、删、改、查、分頁清單、位址詳情的功能開發
22、【收貨位址管理子產品】——收貨位址增、删、改、查、分頁清單、位址詳情的功能開發