perf: 简化代码
This commit is contained in:
@@ -3,7 +3,6 @@ package com.lanyuanxiaoyao.service.template.service;
|
|||||||
import com.lanyuanxiaoyao.service.template.controller.Query;
|
import com.lanyuanxiaoyao.service.template.controller.Query;
|
||||||
import com.lanyuanxiaoyao.service.template.entity.SimpleEntity;
|
import com.lanyuanxiaoyao.service.template.entity.SimpleEntity;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import org.springframework.data.domain.Page;
|
import org.springframework.data.domain.Page;
|
||||||
|
|
||||||
@@ -18,13 +17,9 @@ public interface SimpleService<ENTITY extends SimpleEntity> {
|
|||||||
|
|
||||||
Page<ENTITY> list(Query query) throws Exception;
|
Page<ENTITY> list(Query query) throws Exception;
|
||||||
|
|
||||||
Optional<ENTITY> detailOptional(Long id) throws Exception;
|
|
||||||
|
|
||||||
ENTITY detail(Long id) throws Exception;
|
ENTITY detail(Long id) throws Exception;
|
||||||
|
|
||||||
ENTITY detailOrThrow(Long id) throws Exception;
|
ENTITY detailOrThrow(Long id) throws Exception;
|
||||||
|
|
||||||
ENTITY detailOrNull(Long id) throws Exception;
|
|
||||||
|
|
||||||
void remove(Long id) throws Exception;
|
void remove(Long id) throws Exception;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -493,8 +493,7 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
* @param id 实体ID
|
* @param id 实体ID
|
||||||
* @return Optional<ENTITY> 返回实体详情的Optional包装
|
* @return Optional<ENTITY> 返回实体详情的Optional包装
|
||||||
*/
|
*/
|
||||||
@Override
|
private Optional<ENTITY> detailOptional(Long id) {
|
||||||
public Optional<ENTITY> detailOptional(Long id) {
|
|
||||||
if (ObjectHelper.isNull(id)) {
|
if (ObjectHelper.isNull(id)) {
|
||||||
return Optional.empty();
|
return Optional.empty();
|
||||||
}
|
}
|
||||||
@@ -520,7 +519,7 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public ENTITY detail(Long id) {
|
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));
|
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删除实体
|
* 根据ID删除实体
|
||||||
* <p>
|
* <p>
|
||||||
@@ -576,13 +561,6 @@ public abstract class SimpleServiceSupport<ENTITY extends SimpleEntity> implemen
|
|||||||
* </p>
|
* </p>
|
||||||
*/
|
*/
|
||||||
public static final class IdNotFoundException extends RuntimeException {
|
public static final class IdNotFoundException extends RuntimeException {
|
||||||
/**
|
|
||||||
* 构造函数(无参)
|
|
||||||
*/
|
|
||||||
public IdNotFoundException() {
|
|
||||||
super("资源不存在");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 构造函数(带ID参数)
|
* 构造函数(带ID参数)
|
||||||
*
|
*
|
||||||
|
|||||||
Reference in New Issue
Block a user