删除收货地址
1.持久层
1.1规划sql语句
1.在删除之前需要判断该数据是否存在,判断该条数据的归属是否是当前的用户,不需要重复开发
2.执行删除收货地址
delete from t_address where aid =?
3.如果用户删除逇是默认收货地址,将剩下的地址的某一条设置为默认的收货地址。规则可以自定义:最新修改的收货地址设置为默认的收货地址(modified_time的字段值)
select * from t_address where uid = ? order by modified desc limit 0,1
4.如果用户本身就只有一条收货地址,删除后,其他操作就可以不用进行了
1.2设计抽象方法
在addressMapper接口中进行抽象方法的设计
/**
* 根据收货地址id删除收货地址的数据
* @param aid
* @return
*/
Integer deleteByAid(Integer aid);
/**
* 根据用户uid来查询当前用户最后一次被修改的收货地址的数据
* @param uid 用户id
* @return 收货地址的数据
*/
Address findLastModified(Integer uid);
1.3映射
<delete id="deleteByAid">
delete from t_address where aid=#{aid}
</delete>
<select id="findLastModified" resultMap="AddressEntityMap">
select * from t_address where uid = #{uid} order by modified_time desc limit 0,1
</select>
1.4测试
/**
* 测试通过uid删除收货地址
*/
@Test
void testDeleteByAid(){
Integer integer = addressMapper.deleteByAid(5);
log.info("删除的返回值:{}",integer);
}
/**
* 获取最早修改的收货地址
*/
@Test
void testFindLastModfified(){
log.info("获取最后修改的收货地址:{}",addressMapper.findLastModified(10));
}
业务层
1.规划异常
在执行操作的时候可能会产生未知的异常导致数据不能够删除成功,则抛出DeleteException异常
2.抽象方法
/**
* 删除选定的某个用户的收货地址
* @param uid
* @param aid
* @param username
*/
void deleteAddress(Integer uid,Integer aid,String username);
3.实现方法与具体业务逻辑编写
/**
* 删除某条收货地址
* @param uid
* @param aid
* @param username
*/
@Override
public void deleteAddress(Integer uid, Integer aid, String username) {
//首先判断是否存在此条数据
Address byAid = addressMapper.findByAid(aid);
if (byAid == null) {
throw new AddressNotFoundException("收货地址不存在");
}
//非法访问 检测当前的收货地址的归属
if (!uid.equals(byAid.getUid())){
throw new AccessDeniedException("访问数据非法");
}
//删除数据
Integer integer = addressMapper.deleteByAid(aid);
if (integer == null|| integer !=1) {
throw new DeleteException("删除时产生异常");
}
//重新设置默认地址
Integer integer1 = addressMapper.countByUid(uid);
if (integer == 0) {
//终止程序
return;
}
if (byAid.getIsDefault() == 0){
return;
}
//重新设置默认地址
Address lastModified = addressMapper.findLastModified(uid);
Integer integer2 = addressMapper.updateDefaultByAid(lastModified.getAid(), username, new Date());
if (integer2 != 1) {
throw new UpdateException("更新时产生未知异常");
}
}
4.测试
@Test
void testDeleteByAid(){
addressService.deleteAddress(10,27,"管理员");
}
业务层与前端页面
1.处理异常
2.请求设计
/address/delete_by_aid
/get
/HttpSession session Integer aid
/JsonResult
3.方法设计以及业务逻辑
@RequestMapping("{aid}/delete_by_aid")
public JsonResult<Void> delete_by_aid(HttpSession session, @PathVariable("aid")Integer aid){
Integer uid = getUidFromSession(session);
String username = getUsernameFromSession(session);
addressService.deleteAddress(uid,aid,username);
return new JsonResult<Void>(OK);
}
}
前端页面修改
'<td><a onclick="deleteAddress(#{aid})" class="btn btn-xs add-del btn-info"><span class="fa fa-trash-o"></span> 删除</a></td>\n'
function deleteAddress(aid) {
$.ajax({
url :"/address/"+aid+"/delete_by_aid",
type:"post",
dataType:"json",
success : function(json) {
if (json.state == 200) {
showAddressList();
}else {
alert("删除地址失败")
}
},
error : function(xhr) {
alert("删除地址时产生了未知的错误异常"+xhr.message)
}
});
}
标签:uid,删除,项目,收货,地址,Integer,aid,springboot
From: https://www.cnblogs.com/wiseleer/p/16873364.html