From f6029619c3f61abe1219d86e8d5a1cdae1951cb8 Mon Sep 17 00:00:00 2001 From: Administrator Date: Fri, 1 Dec 2023 21:13:56 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=A0=E9=99=A4=E7=A9=BA=E6=B0=94=E8=B4=A8?= =?UTF-8?q?=E9=87=8F=E4=BF=A1=E6=81=AF~?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mashibing/controller/AirController.java | 39 +++++++++++++++++-- .../java/com/mashibing/mapper/AirMapper.java | 4 ++ .../com/mashibing/service/AirService.java | 6 +++ .../service/impl/AirServiceImpl.java | 17 +++++++- .../com/mashibing/mapper/AirMapperTest.java | 5 +++ .../com/mashibing/service/AirServiceTest.java | 5 +++ 6 files changed, 70 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/mashibing/controller/AirController.java b/src/main/java/com/mashibing/controller/AirController.java index f3a709c..1be1bb6 100644 --- a/src/main/java/com/mashibing/controller/AirController.java +++ b/src/main/java/com/mashibing/controller/AirController.java @@ -9,12 +9,10 @@ import com.mashibing.util.R; import com.mashibing.vo.ResultVO; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.BindingResult; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import javax.websocket.server.PathParam; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -141,4 +139,37 @@ public class AirController { //3、返回 return R.ok(); } + + + /** + * # 删除空气质量信息 + * # 请求方式&路径 + * DELETE http://localhost:8080/air/delete/{id} + * + * # 请求参数 + * 路径上的id + * + * # 业务流程 + * 1、接收参数 + * 2、删除指定id的数据 + * + * # 响应数据 + * 成功响应: + * { + * "code": 0, + * "msg": "" + * } + * 失败响应: + * { + * "code": 错误编码, + * "msg": "错误信息" + * } + */ + @DeleteMapping("/air/delete/{id}") + public ResultVO airDelete(@PathVariable Integer id){ + //1、调用service删除 + airService.deleteById(id); + //2、响应数据 + return R.ok(); + } } diff --git a/src/main/java/com/mashibing/mapper/AirMapper.java b/src/main/java/com/mashibing/mapper/AirMapper.java index 321f1e7..e257cd6 100644 --- a/src/main/java/com/mashibing/mapper/AirMapper.java +++ b/src/main/java/com/mashibing/mapper/AirMapper.java @@ -1,6 +1,7 @@ package com.mashibing.mapper; import com.mashibing.entity.Air; +import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Update; @@ -23,4 +24,7 @@ public interface AirMapper { // 修改数据 @Update("update air set district_id = #{districtId},monitor_time = #{monitorTime},pm10 = #{pm10},pm25 = #{pm25},monitoring_station = #{monitoringStation} where id = #{id}") int updateById(Air air); + + @Delete("delete from air where id = #{id}") + int deleteById(@Param("id") Integer id); } diff --git a/src/main/java/com/mashibing/service/AirService.java b/src/main/java/com/mashibing/service/AirService.java index 6509037..c1fbb65 100644 --- a/src/main/java/com/mashibing/service/AirService.java +++ b/src/main/java/com/mashibing/service/AirService.java @@ -38,4 +38,10 @@ public interface AirService { * @param air */ void updateById(Air air); + + /** + * 删除空气质量信息 + * @param id + */ + void deleteById(Integer id); } diff --git a/src/main/java/com/mashibing/service/impl/AirServiceImpl.java b/src/main/java/com/mashibing/service/impl/AirServiceImpl.java index 9813414..eae1a2b 100644 --- a/src/main/java/com/mashibing/service/impl/AirServiceImpl.java +++ b/src/main/java/com/mashibing/service/impl/AirServiceImpl.java @@ -38,7 +38,7 @@ public class AirServiceImpl implements AirService { @Override public PageInfo findAirByDistrictIdAndPage(Integer page, Integer size, Integer districtId) { //1、分页 - PageHelper.startPage(page,size); + PageHelper.startPage(page, size); //2、查询 List airList = airMapper.findBydDistrictId(districtId); //3、封装PageInfo @@ -52,7 +52,7 @@ public class AirServiceImpl implements AirService { public void add(AirAddForm airAddForm) { //1、封装数据 Air air = new Air(); - BeanUtils.copyProperties(airAddForm,air); + BeanUtils.copyProperties(airAddForm, air); //2、添加数据 int count = airMapper.insert(air); @@ -65,6 +65,7 @@ public class AirServiceImpl implements AirService { } @Override + @Transactional public void updateById(Air air) { //1、修改数据 int count = airMapper.updateById(air); @@ -74,4 +75,16 @@ public class AirServiceImpl implements AirService { throw new RuntimeException("【修改空气质量】 修改失败!!"); } } + + @Override + @Transactional + public void deleteById(Integer id) { + // 删除 + int count = airMapper.deleteById(id); + // 判断 + if (count != 1) { + System.out.println("【删除空气质量】 删除失败!!"); + throw new RuntimeException("【删除空气质量】 删除失败!!"); + } + } } diff --git a/src/test/java/com/mashibing/mapper/AirMapperTest.java b/src/test/java/com/mashibing/mapper/AirMapperTest.java index 7f03af9..e55a1f5 100644 --- a/src/test/java/com/mashibing/mapper/AirMapperTest.java +++ b/src/test/java/com/mashibing/mapper/AirMapperTest.java @@ -54,4 +54,9 @@ class AirMapperTest { air.setMonitoringStation("长沙监测站!!!!"); airMapper.updateById(air); } + + @Test + public void deleteById(){ + airMapper.deleteById(71); + } } \ No newline at end of file diff --git a/src/test/java/com/mashibing/service/AirServiceTest.java b/src/test/java/com/mashibing/service/AirServiceTest.java index 5ba701f..1c1c238 100644 --- a/src/test/java/com/mashibing/service/AirServiceTest.java +++ b/src/test/java/com/mashibing/service/AirServiceTest.java @@ -60,4 +60,9 @@ public class AirServiceTest { air.setMonitoringStation("北京监测站!!!!"); airService.updateById(air); } + + @Test + public void deleteById(){ + airService.deleteById(70); + } } \ No newline at end of file