收货地址
增上改查功能
依旧,先新建Controller,Service
添加地址
@Controller
@RequestMapping("/shipping")
public class ShippingController {
@Autowired
private IShippingService iShippingService;
@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(user.getId(),shipping);
}
}
在参数里直接传shipping对象,MVC数据绑定中的对象数据绑定,就让前端传地址需要的几个字段就可以接收到了
@Service("shippingService")
public class ShippingServiceImpl implements IShippingService {
@Autowired
private ShippingMapper shippingMapper;
public ServerResponse add(Integer userId, Shipping shipping){
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("新建地址失败");
}
}
这里要修改一下insert方法,因为要在insert之后立刻拿到它的id。
默认insert节点返回的是插入的生效行数,新增两个配置 useGeneratedKeys="true" keyProperty="id" 它的返回值就是插入成功之后的id
<insert id="insert" parameterType="com.mmall.pojo.Shipping" useGeneratedKeys="true" keyProperty="id">
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()}
)
</insert>
删除地址
@RequestMapping("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);
}
不能直接使用Mybatis的deleteByPrimaryKey,因为只需要一个地址id,这样一个用户可以删除不属于他的地址,属于横向越权,我们重新写一个删除方法
<delete id="deleteByShippingIdUserId" parameterType="map">
DELETE FROM mmall_shipping
WHERE user_id = #{userId}
AND id = #{shippingId}
</delete>
在service调用
public ServerResponse<String> del(Integer userId,Integer shippingId){
int resultCount = shippingMapper.deleteByShippingIdUserId(userId,shippingId);
if(resultCount > 0){
return ServerResponse.createBySuccess("删除地址成功");
}
return ServerResponse.createByErrorMessage("删除地址失败");
}
更新地址 查询单个地址
依旧有上面相同的横向越权问题,详见代码
查询地址列表
也就多了个分页查询,详见代码
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);
}
网友评论