版权声明:转载请注明出处~ https://blog.csdn.net/czjlghndcy/article/details/83045150
}
- 1
- 2
- 3
- 4
- 5
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
*Service
:
//收货地址添加方法
ServerResponse add(Integer userId, Shipping shipping);
- 1
- 2
*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("新建地址失败");
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
*Service
:
//删除收货地址方法
ServerResponse del(Integer userId,Integer shippingId);
- 1
- 2
*ServiceImpl
:
//删除收货地址方法 public ServerResponse del(Integer userId,Integer shippingId){
<span class="token keyword">int</span> rowCount<span class="token operator">=</span>shippingMapper<span class="token punctuation">.</span><span class="token function">deleteByShippingIdByUserId</span><span class="token punctuation">(</span>userId<span class="token punctuation">,</span>shippingId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>rowCount<span class="token operator">></span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ServerResponse<span class="token punctuation">.</span><span class="token function">createBySuccess</span><span class="token punctuation">(</span><span class="token string">"删除地址成功"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> ServerResponse<span class="token punctuation">.</span><span class="token function">createByErrorMessage</span><span class="token punctuation">(</span><span class="token string">"删除地址失败"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
由于为了防止横向越权的问题,我们使用自己封装的deleteByShippingIdByUserId
方法,在删除收货地址的时候,我们不仅判断收货地址的Id
,同时还判断该收货地址是否是在当前用户下。
*Mapper
:
//同时根据用户Id和地址Id来删除地址,防止横向越权
int deleteByShippingIdByUserId(@Param("userId") Integer userId, @Param("shippongId") Integer shippongId);
- 1
- 2
*Mappler.xml
:
<!--同时根据用户Id和地址Id来删除地址,防止横向越权-->
<delete id="deleteByShippingIdByUserId" parameterType="map" >
delete
from mmall_shipping
where user_id=#{userId}
and id=#{shippongId}
</delete>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
*Service
:
//修改地址接口
ServerResponse update(Integer userId,Shipping shipping);
- 1
- 2
*ServiceImpl
:
//修改地址方法 public ServerResponse update(Integer userId,Shipping shipping){
shipping<span class="token punctuation">.</span><span class="token function">setUserId</span><span class="token punctuation">(</span>userId<span class="token punctuation">)</span><span class="token punctuation">;</span> Shipping selship<span class="token operator">=</span>shippingMapper<span class="token punctuation">.</span><span class="token function">selectByShippingIdByUserId</span><span class="token punctuation">(</span>userId<span class="token punctuation">,</span>shipping<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>selship <span class="token operator">==</span> null<span class="token punctuation">)</span><span class="token punctuation">{</span> <span class="token keyword">return</span> ServerResponse<span class="token punctuation">.</span><span class="token function">createByErrorMessage</span><span class="token punctuation">(</span><span class="token string">"该用户不存在此地址"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span><span class="token keyword">else</span> <span class="token punctuation">{</span> <span class="token keyword">int</span> rowCount<span class="token operator">=</span> shippingMapper<span class="token punctuation">.</span><span class="token function">updateByshipping</span><span class="token punctuation">(</span>shipping<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">if</span><span class="token punctuation">(</span>rowCount<span class="token operator">>=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">{</span> Map result<span class="token operator">=</span> Maps<span class="token punctuation">.</span><span class="token function">newHashMap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> result<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">"shippingId"</span><span class="token punctuation">,</span>shipping<span class="token punctuation">.</span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ServerResponse<span class="token punctuation">.</span><span class="token function">createBySuccess</span><span class="token punctuation">(</span><span class="token string">"更新地址成功"</span><span class="token punctuation">,</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> <span class="token keyword">return</span> ServerResponse<span class="token punctuation">.</span><span class="token function">createByErrorMessage</span><span class="token punctuation">(</span><span class="token string">"更新地址失败"</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
updateByshipping
方法:
*Mapper
:
//修改地址接口
int updateByshipping(Shipping record);
- 1
- 2
*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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
*Service
:
//查询收货地址的方法
ServerResponse<Shipping> select(Integer userId,Integer shippingId);
- 1
- 2
*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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
*Mapper
:
```//查询收货地址接口
Shipping selectByShippingIdByUserId(@Param("userId") Integer userId, @Param("shippongId") Integer shippongId);
- 1
- 2
- 3
*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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
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);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
*Service
:
//查询所有收货地址的方法
ServerResponse<PageInfo> list(Integer userId, int pageNum, int pageSize);
- 1
- 2
*ServiceImpl
:
//查询所有收货地址的方法 public ServerResponse<PageInfo> list(Integer userId,int pageNum, int pageSize){ PageHelper.startPage(pageNum,pageSize); List<Shipping> shippingList=shippingMapper.selectByUserId(userId);
PageInfo pageInfo<span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">PageInfo</span><span class="token punctuation">(</span>shippingList<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token keyword">return</span> ServerResponse<span class="token punctuation">.</span><span class="token function">createBySuccess</span><span class="token punctuation">(</span>pageInfo<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
*Mapper
:
//查询所有收获地址接口
List<Shipping> selectByUserId(Integer userId);
- 1
- 2
*Mappler.xml
:
<select id="selectByUserId" resultMap="BaseResultMap" parameterType="map">
select
<include refid="Base_Column_List"/>
from mmall_shipping
where user_id=#{userId}
</select>
- 1
- 2
- 3
- 4
- 5
- 6
2、接口测试:
1、收货地址接口测试
2、收货地址删除的接口测试
3、收货地址修改的接口测试
4、查询地址接口测试
5、查询所有地址接口测试
</div>
<link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-7f770a53f2.css" rel="stylesheet">
</div>