1. 添加HandlerMethodValidationException全局异常器

2. 新增防护对象类,添加Service、Mapper、Controller(Controller仍然在开发中)
3. page和pageSize添加@Min注解,限定最低整数大小
4. 将所有的批量类型方法修改为forEach,在SpringBoot中循环执行并整合为事务
This commit is contained in:
松岳 陈
2024-01-05 09:32:19 +08:00
parent b4db26c856
commit 776c7c0f6d
19 changed files with 327 additions and 82 deletions

View File

@@ -3,7 +3,7 @@ package com.realtime.protection.server.task;
import com.realtime.protection.configuration.entity.task.Task;
import com.realtime.protection.configuration.response.ResponseResult;
import jakarta.validation.Valid;
import org.apache.coyote.Response;
import jakarta.validation.constraints.Min;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@@ -40,19 +40,15 @@ public class TaskController {
@RequestParam(value = "task_type", required = false) String taskType,
@RequestParam(value = "task_name", required = false) String taskName,
@RequestParam(value = "task_creator", required = false) String taskCreator,
@RequestParam("page") Integer page,
@RequestParam("page_size") Integer pageSize) {
if (page <= 0 || pageSize <= 0) {
return new ResponseResult(400, "page or page_size is invalid")
.setData("task_list", null);
}
@RequestParam("page") @Min(1) Integer page,
@RequestParam("page_size") @Min(1) Integer pageSize) {
List<Task> tasks = taskService.queryTasks(taskStatus, taskType, taskName, taskCreator, page, pageSize);
return ResponseResult.ok()
.setData("task_list", tasks);
}
@GetMapping("/{id}/query")
public ResponseResult queryTask(@PathVariable("id") Integer id) {
public ResponseResult queryTask(@PathVariable("id") @Min(1) Integer id) {
Task task = taskService.queryTask(id);
if (task == null) {
@@ -74,7 +70,7 @@ public class TaskController {
}
@PostMapping("/{id}/update")
public ResponseResult updateTask(@PathVariable("id") Integer taskId, @RequestBody Task task) {
public ResponseResult updateTask(@PathVariable("id") @Min(1) Integer taskId, @RequestBody @Valid Task task) {
task.setTaskId(taskId);
taskService.updateTask(task);

View File

@@ -10,7 +10,7 @@ import java.util.List;
public interface TaskMapper {
void newTask(@Param("task") Task task);
void newTaskProtectObjectConcat(@Param("task_id") Integer taskId, @Param("proobj_ids") List<Integer> proobjIds);
void newTaskProtectObjectConcat(@Param("task_id") Integer taskId, @Param("proobj_id") Integer proobjId);
void newTaskStaticRuleConcat(@Param("task_id") Integer taskId,
@Param("rule_ids") List<Integer> staticRuleIds);

View File

@@ -4,6 +4,7 @@ import com.realtime.protection.configuration.entity.task.Task;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@@ -24,9 +25,11 @@ public class TaskService {
try {
taskMapper.newTask(task);
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds());
task.getProtectObjectIds().forEach(
proobjId -> taskMapper.newTaskProtectObjectConcat(task.getTaskId(), proobjId));
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
// taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
session.commit();
} catch (Exception e) {
@@ -73,34 +76,17 @@ public class TaskService {
return task;
}
@Transactional
public void updateTask(Task task) {
SqlSession session = sqlSessionFactory.openSession(false);
TaskMapper taskMapper = session.getMapper(TaskMapper.class);
try {
taskMapper.updateTask(task);
taskMapper.updateTask(task);
taskMapper.clearTaskProtectObjectConcat(task.getTaskId());
taskMapper.clearTaskConnectedStaticRule(task.getTaskId());
// taskMapper.clearTaskConnectedDynamicRule(task.getTaskId());
taskMapper.clearTaskProtectObjectConcat(task.getTaskId());
taskMapper.clearTaskConnectedStaticRule(task.getTaskId());
taskMapper.clearTaskConnectedDynamicRule(task.getTaskId());
if (task.getProtectObjectIds() != null && !task.getProtectObjectIds().isEmpty()) {
taskMapper.newTaskProtectObjectConcat(task.getTaskId(), task.getProtectObjectIds());
}
if (task.getStaticRuleIds() != null && !task.getStaticRuleIds().isEmpty()) {
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
}
if (task.getDynamicRuleIds() != null && !task.getDynamicRuleIds().isEmpty()) {
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
}
session.commit();
} catch (Exception e) {
session.rollback();
throw e;
} finally {
session.close();
}
task.getProtectObjectIds().forEach(
proobjId -> taskMapper.newTaskProtectObjectConcat(task.getTaskId(), proobjId));
taskMapper.newTaskStaticRuleConcat(task.getTaskId(), task.getStaticRuleIds());
taskMapper.newTaskDynamicRuleConcat(task.getTaskId(), task.getDynamicRuleIds());
}
}