当前位置:网站首页>删除收货地址【项目 商城】
删除收货地址【项目 商城】
2022-06-11 23:13:00 【日星月云】
删除收货地址【项目 商城】
删除收货地址

1 持久层
1.1 规划需要执行的SQL语句
1.在删除之前判断改数据是否存在,判断该条地址数据的归属是否是当前的用户。
不用重复开发
2.执行删除收货地址的信息。
delete from t_address where aid=?
3.如果用户删除的是默认收货地址,将剩下的地址中的某一条设置为默认的收货地址。规则可以自定义:最新修改的收货地址设置为默认的收货地址(modified_time的字段值)。
# limit (n-1)*n,pageSize
select * from t_address where uid=? order by modified_time DESC limit 0,1
4.如果用户本身就只有一条收货地址的数据,删除后,其他的操作就可以不进行了。
1.2 设计抽象方法
在AddressMapper接口中进抽象方法的设计。
/** * 根据收货地址id删除收货地址数据 * @param aid 收货地址id * @return 受影响的行数 */
Integer deleteByAid(Integer aid);
/** * 根据用户uid查询当前用户最后一次被修改的收货地址数据 * @param uid 用户id * @return 收货地址 */
Address findLastModified(Integer uid);
1.3 映射SQL语句
在AddressMapper.xml文件中进行映射。
<select id="findByAid" resultMap="AddressEntityMap">
SELECT * FROM t_address WHERE aid=#{aid}
</select>
<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>
AddressMapper–deleteByAid
测试
单元测试方法
@Test
void deleteByAid(){
Integer rows = addressMapper.deleteByAid(1);
System.out.println(rows);
}
@Test
void findLastModified(){
System.out.println(addressMapper.findLastModified(8));
}
AddressMapperTests–deleteByAid
2 业务层
2.1 规划异常
在执行删除的时候可能会产生未知的删除异常导致数据不能够删除成功,则抛出DeleteException异常。需要定义和创建。
/**删除数据时产生的异常*/
public class DeleteException extends ServiceException{
//Ctrl+O
}
DeleteException
2.2 抽象方法设计
在IAddressService接口中进行设计抽象方法、
/** * 删除用户选中的收货地址数据 * @param aid 收货地址id * @param uid 用户id * @param username 用户名 */
void delete(Integer aid,Integer uid,String username);
2.3 实现抽象方法
业务层方法的设计和实现。
@Override
public void delete(Integer aid, Integer uid, String username) {
Address result = addressMapper.findByAid(aid);
if (result == null) {
throw new AddressNotFoundException("尝试访问的收货地址数据不存在");
}
if (!result.getUid().equals(uid)) {
throw new AccessDeniedException("非法数据访问的异常");
}
Integer rows = addressMapper.deleteByAid(aid);
if (rows!=1){
throw new DeleteException("删除数据产生未知的异常");
}
Integer count = addressMapper.countByUid(uid);
if (count==0){
//直接终止程序
return;
}
if (result.getIsDefault()==0){
return;
}
//将这条数据中is_default字符设置为1
Address address = addressMapper.findLastModified(uid);
rows=addressMapper.updateDefaultByAid(address.getAid(),username,new Date());
if (rows!=1){
throw new UpdateException("更新数据时产生未知的异常");
}
}
AddressService–delete
测试
在测试类测试该方法的功能
@Test
public void delete(){
addressService.delete(3,8,"管理员");
}
AddressServiceTests–delete
3 控制层
1.需要处理异常DeleteException。
else if (e instanceof DeleteException){
result.setState(5002);
result.setMessage("删除数据时产生未知的异常");
}
BaseController–DeleteException
2.设计请求方法
/addresses/{
aid}/delete
POST
Integer aid,HttpSession session
JsonResult<Void>
3.编写请求处理方法实现。
@RequestMapping("{aid}/delete")
public JsonResult<Void> delete(@PathVariable("aid") Integer aid,HttpSession session){
addressService.delete(
aid,
getuidFromSession(session),
getUsernameFromSession(session)
);
return new JsonResult<>(OK);
}
AddressController–delete
测试
略
4 前端页面
在address.html页面中添加删除按钮的事件。
+ '<td><a οnclick="deleteByAid(#{aid})" class="btn btn-xs add-del btn-info"><span class="fa fa-trash-o"></span> 删除</a></td>'
在去编写deleteByAid(aid)方法的具体实现。
// <!--delete-->
function deleteByAid(aid) {
$.ajax({
url: "/addresses/" + aid + "/delete",
type: "POST",
dataType: "JSON",
success: function(json) {
if (json.state == 200) {
showAddressList();
} else {
alert("删除收货地址失败!" + json.message);
}
},
error: function(json) {
alert("您的登录信息已经过期,请重新登录!HTTP响应码:" + json.status);
location.href = "login.html";
}
});
}
// <!--delete-->
address.html–delete
测试
登录系统,在访问收货地址页面进行删除的数据测试。
README–删除收货地址
边栏推荐
- Toyota suppliers shut down Japanese factories due to cyber attacks, NVIDIA counterattacks extortion gangs to prevent data leakage | global cyber security hotspot on March 1
- 2022年低压电工上岗证题目及在线模拟考试
- The second bullet of in-depth dialogue with the container service ack distribution: how to build a hybrid cloud unified network plane with the help of hybridnet
- mysql——find_in_set用法
- 华为云、OBS、
- postgresql10 進程
- SecurityContextHolder.getContext().getAuthentication().getPrincipal()获取到的是username而不是UserDetails
- Processus postgresql10
- [day15 literature extensive reading] numerical magnetic effects temporary memories but not time encoding
- [day1/5 literature intensive reading] speed constancy or only slowness: what drives the kappa effect
猜你喜欢
![[Day10 literature extensive reading] temporary cognition can affect spatial cognition more than vice versa: the effect of](/img/f7/03709322088b9ec57b20e49110d420.png)
[Day10 literature extensive reading] temporary cognition can affect spatial cognition more than vice versa: the effect of

2022 operation of simulation examination platform for safety officer C certificate
![[day13-14 intensive literature reading] cross dimensional magnetic interactions arise from memory interference](/img/e0/94602f0b7b6e50f55e29b6147a9df2.png)
[day13-14 intensive literature reading] cross dimensional magnetic interactions arise from memory interference

Toyota suppliers shut down Japanese factories due to cyber attacks, NVIDIA counterattacks extortion gangs to prevent data leakage | global cyber security hotspot on March 1

直播预告|FeatureStore Meetup V3 重磅来袭!

Fonctionnement de la plate - forme d'examen de simulation pour les agents de sécurité - Questions d'examen de certificat a en 2022

CVPR 2022 | meta learning performance in image regression task

SDNU_ ACM_ ICPC_ 2022_ Weekly_ Practice_ 1st (supplementary question)
![[naturallanguageprocessing] [multimodal] albef: visual language representation learning based on momentum distillation](/img/b7/0ccd11bd97aa0e217775b31fff3511.jpg)
[naturallanguageprocessing] [multimodal] albef: visual language representation learning based on momentum distillation

Huawei equipment configuration hovpn
随机推荐
我的创作纪念日
Tensorflow [actual Google deep learning framework] uses HDF5 to process large data sets with tflearn
直播预告|FeatureStore Meetup V3 重磅来袭!
想做钢铁侠?听说很多大佬都是用它入门的
2022 operation of simulation examination platform for safety officer C certificate
Two way leading circular linked list (C language)
Three years of college should be like this
【Day9 文献泛读】On the (a)symmetry between the perception of time and space in large-scale environments
Huawei cloud, OBS
[day13-14 intensive literature reading] cross dimensional magnetic interactions arise from memory interference
Gcache of goframe memory cache
Are you still struggling with the gold content of PMP
Analysis on the market prospect of smart home based on ZigBee protocol wireless module
Jsonparseexception: unrecognized token 'username': was expecting error when submitting login data
Research Report on development trend and competitive strategy of global wafer recycling and OEM service industry
Software installation and use, etc
The remote connection to redis is disconnected and reconnected after a while
Discrete mathematics attention points, priority problems
Research Report on development trend and competitive strategy of global non directional beacon industry
2022年安全员-A证考题模拟考试平台操作