1
0

perf: 简化代码

This commit is contained in:
2025-08-21 10:43:21 +08:00
parent 6baefd1a8a
commit 8244b5dbdf
2 changed files with 2 additions and 29 deletions

View File

@@ -3,7 +3,6 @@ package com.lanyuanxiaoyao.service.template.service;
import com.lanyuanxiaoyao.service.template.controller.Query;
import com.lanyuanxiaoyao.service.template.entity.SimpleEntity;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.springframework.data.domain.Page;
@@ -18,13 +17,9 @@ public interface SimpleService<ENTITY extends SimpleEntity> {
Page<ENTITY> list(Query query) throws Exception;
Optional<ENTITY> detailOptional(Long id) throws Exception;
ENTITY detail(Long id) throws Exception;
ENTITY detailOrThrow(Long id) throws Exception;
ENTITY detailOrNull(Long id) throws Exception;
void remove(Long id) throws Exception;
}

View File

@@ -493,8 +493,7 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
* @param id 实体ID
* @return Optional<ENTITY> 返回实体详情的Optional包装
*/
@Override
public Optional<ENTITY> detailOptional(Long id) {
private Optional<ENTITY> detailOptional(Long id) {
if (ObjectHelper.isNull(id)) {
return Optional.empty();
}
@@ -520,7 +519,7 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
*/
@Override
public ENTITY detail(Long id) {
return detailOrNull(id);
return detailOptional(id).orElse(null);
}
/**
@@ -538,20 +537,6 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
return detailOptional(id).orElseThrow(() -> new IdNotFoundException(id));
}
/**
* 根据ID获取实体详情不存在时返回null
* <p>
* 与[detail](file:///Users/lanyuanxiaoyao/Project/IdeaProjects/spring-boot-service-template/src/main/java/com/lanyuanxiaoyao/service/template/service/SimpleService.java#L21-L21)方法功能相同。
* </p>
*
* @param id 实体ID
* @return ENTITY 返回实体详情不存在时返回null
*/
@Override
public ENTITY detailOrNull(Long id) {
return detailOptional(id).orElse(null);
}
/**
* 根据ID删除实体
* <p>
@@ -576,13 +561,6 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
* </p>
*/
public static final class IdNotFoundException extends RuntimeException {
/**
* 构造函数(无参)
*/
public IdNotFoundException() {
super("资源不存在");
}
/**
* 构造函数带ID参数
*