当前位置:网站首页>删除收货地址【项目 商城】
删除收货地址【项目 商城】
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–删除收货地址
边栏推荐
- What are the pitfalls of redis's current network: using a cache and paying for disk failures?
- [technology sharing] after 16 years, how to successfully implement the dual active traffic architecture of zhubajie.com
- Method for debugging wireless data packet capturing of Internet of things zigbee3.0 protocol e18-2g4u04b module
- Meetup回顾|DevOps&MLOps如何在企业中解决机器学习困境?
- Read the logstash principle
- H.265编码原理入门
- 一文读懂Logstash原理
- Leetcode must review 20 lintcode (5466421166978227)
- 2022年起重机司机(限桥式起重机)考试题模拟考试题库及模拟考试
- Unity3d C#开发微信小游戏音频/音效播放问题解决过程分享
猜你喜欢

2022年安全员-A证考题模拟考试平台操作

The top ten trends of 2022 industrial Internet security was officially released

2022 safety officer-a certificate test question simulation test platform operation

2022新兴市场品牌出海线上峰会即将举办 ADVANCE.AI CEO寿栋将受邀出席

Wake up wrist - neural network and deep learning (tensorflow application) updating

Want to be iron man? It is said that many big men use it to get started

Data visualization platform based on template configuration

Fonctionnement de la plate - forme d'examen de simulation pour les agents de sécurité - Questions d'examen de certificat a en 2022
![[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

产品力进阶新作,全新第三代荣威RX5盲订开启
随机推荐
2022年高处安装、维护、拆除操作证考试题库模拟考试平台操作
Research Report on development trend and competitive strategy of global wafer recycling and OEM service industry
IEEE floating point mantissa even round - round to double
机器学习之数据处理与可视化【鸢尾花数据分类|特征属性比较】
[day13-14 intensive literature reading] cross dimensional magnetic interactions arise from memory interference
Software installation and use, etc
Google搜索为什么不能无限分页?
【Day2 文献精读】Time in the mind: Using space to think about time
Zigbee3.0 wireless packet capturing installation method based on e18-2g4u04b
C language simple exercise No.17, about the combination of for and while loops
产品力进阶新作,全新第三代荣威RX5盲订开启
双向带头循环链表(C语言)
JsonParseException: Unrecognized token ‘username‘: was expecting表单提交登陆数据报错
一文读懂Logstash原理
Simulated examination question bank and simulated examination of 2022 crane driver (limited to bridge crane)
Is it too troublesome to turn pages manually when you encounter a form? I'll teach you to write a script that shows all the data on one page
Research Report on development trend and competitive strategy of global non directional beacon industry
Wireless communication comparison of si4463, si4438 and Si4432 schemes of wireless data transmission module
唤醒手腕 - 神经网络与深度学习(Tensorflow应用)更新中
Solr之基础讲解入门