-
Notifications
You must be signed in to change notification settings - Fork 181
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1. 重构基础Controller,将基础Controller的读操作与其它操作分离,以支持“视图”相关Controller的编写。
- Loading branch information
1 parent
3db2668
commit f0a28af
Showing
31 changed files
with
256 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...-crud/src/main/java/cn/herodotus/eurynome/crud/controller/BaseReadableRestController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (c) 2019-2021 Gengwei Zheng ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Project Name: eurynome-cloud | ||
* Module Name: eurynome-cloud-crud | ||
* File Name: BaseReadableRestController.java | ||
* Author: gengwei.zheng | ||
* Date: 2021/07/07 17:38:07 | ||
*/ | ||
|
||
package cn.herodotus.eurynome.crud.controller; | ||
|
||
import cn.herodotus.eurynome.common.definition.entity.AbstractEntity; | ||
import cn.herodotus.eurynome.common.domain.Result; | ||
import io.swagger.annotations.ApiImplicitParam; | ||
import io.swagger.annotations.ApiImplicitParams; | ||
import io.swagger.annotations.ApiOperation; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import java.io.Serializable; | ||
import java.util.Map; | ||
|
||
/** | ||
* <p>Description: 只读RestController </p> | ||
* | ||
* @author : gengwei.zheng | ||
* @date : 2021/7/7 17:38 | ||
*/ | ||
public abstract class BaseReadableRestController<E extends AbstractEntity, ID extends Serializable> implements ReadableController<E, ID> { | ||
|
||
@ApiOperation(value = "分页查询数据", notes = "通过pageNumber和pageSize获取分页数据", produces = "application/json") | ||
@ApiImplicitParams({ | ||
@ApiImplicitParam(name = "pageNumber", required = true, value = "当前页数", dataType = "int", paramType = "query"), | ||
@ApiImplicitParam(name = "pageSize", required = true, value = "每页显示数据条目", dataType = "int", paramType = "query") | ||
}) | ||
@GetMapping | ||
@Override | ||
public Result<Map<String, Object>> findByPage( | ||
@RequestParam("pageNumber") Integer pageNumber, | ||
@RequestParam("pageSize") Integer pageSize) { | ||
return ReadableController.super.findByPage(pageNumber, pageSize); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2019-2021 Gengwei Zheng([email protected]) | ||
* Copyright (c) 2019-2021 Gengwei Zheng ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -15,15 +15,15 @@ | |
* | ||
* Project Name: eurynome-cloud | ||
* Module Name: eurynome-cloud-crud | ||
* File Name: AbstractController.java | ||
* File Name: Controller.java | ||
* Author: gengwei.zheng | ||
* Date: 2021/05/07 11:28:07 | ||
* Date: 2021/07/07 17:24:07 | ||
*/ | ||
|
||
package cn.herodotus.eurynome.crud.controller; | ||
|
||
import cn.herodotus.eurynome.common.domain.Result; | ||
import cn.herodotus.eurynome.common.definition.entity.AbstractEntity; | ||
import cn.herodotus.eurynome.common.domain.Result; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.collections4.MapUtils; | ||
import org.apache.commons.lang3.ObjectUtils; | ||
|
@@ -35,14 +35,16 @@ | |
import java.util.Map; | ||
|
||
/** | ||
* <p> Description : BaseController </p> | ||
* <p>Description: Controller基础定义 </p> | ||
* <p> | ||
* 这里只在方法上做了泛型,主要是考虑到返回的结果数据可以是各种类型,而不一定受限于某一种类型。 | ||
* | ||
* @author : gengwei.zheng | ||
* @date : 2020/4/29 18:56 | ||
* @date : 2021/7/7 17:24 | ||
*/ | ||
public abstract class AbstractController { | ||
public interface Controller { | ||
|
||
protected <E extends AbstractEntity> Result<E> result(E domain) { | ||
default <E extends AbstractEntity> Result<E> result(E domain) { | ||
Result<E> result = new Result<>(); | ||
if (ObjectUtils.isNotEmpty(domain)) { | ||
return result.ok().message("操作成功!").data(domain); | ||
|
@@ -51,7 +53,7 @@ protected <E extends AbstractEntity> Result<E> result(E domain) { | |
} | ||
} | ||
|
||
protected <E extends AbstractEntity> Result<List<E>> result(List<E> domains) { | ||
default <E extends AbstractEntity> Result<List<E>> result(List<E> domains) { | ||
Result<List<E>> result = new Result<>(); | ||
if (CollectionUtils.isNotEmpty(domains)) { | ||
return result.ok().message("查询数据成功!").data(domains); | ||
|
@@ -60,7 +62,7 @@ protected <E extends AbstractEntity> Result<List<E>> result(List<E> domains) { | |
} | ||
} | ||
|
||
protected <E extends AbstractEntity> Result<Map<String, Object>> result(Page<E> pages) { | ||
default <E extends AbstractEntity> Result<Map<String, Object>> result(Page<E> pages) { | ||
Result<Map<String, Object>> result = new Result<>(); | ||
if (ObjectUtils.isNotEmpty(pages)) { | ||
return result.ok().message("查询数据成功!").data(getPageInfoMap(pages)); | ||
|
@@ -69,7 +71,7 @@ protected <E extends AbstractEntity> Result<Map<String, Object>> result(Page<E> | |
} | ||
} | ||
|
||
protected Result<Map<String, Object>> result(Map<String, Object> map) { | ||
default Result<Map<String, Object>> result(Map<String, Object> map) { | ||
Result<Map<String, Object>> result = new Result<>(); | ||
if (MapUtils.isNotEmpty(map)) { | ||
return result.ok().message("查询数据成功!").data(map); | ||
|
@@ -78,7 +80,7 @@ protected Result<Map<String, Object>> result(Map<String, Object> map) { | |
} | ||
} | ||
|
||
protected <ID extends Serializable> Result<String> result(ID parameter) { | ||
default <ID extends Serializable> Result<String> result(ID parameter) { | ||
Result<String> result = new Result<>(); | ||
if (ObjectUtils.isNotEmpty(parameter)) { | ||
return result.ok().message("操作成功!"); | ||
|
@@ -87,11 +89,11 @@ protected <ID extends Serializable> Result<String> result(ID parameter) { | |
} | ||
} | ||
|
||
protected <E extends AbstractEntity> Map<String, Object> getPageInfoMap(Page<E> pages) { | ||
default <E extends AbstractEntity> Map<String, Object> getPageInfoMap(Page<E> pages) { | ||
return getPageInfoMap(pages.getContent(), pages.getTotalPages(), pages.getTotalElements()); | ||
} | ||
|
||
protected <E extends AbstractEntity> Map<String, Object> getPageInfoMap(List<E> content, int totalPages, long totalElements) { | ||
default <E extends AbstractEntity> Map<String, Object> getPageInfoMap(List<E> content, int totalPages, long totalElements) { | ||
Map<String, Object> result = new HashMap<>(8); | ||
result.put("content", content); | ||
result.put("totalPages", totalPages); | ||
|
63 changes: 63 additions & 0 deletions
63
...me-cloud-crud/src/main/java/cn/herodotus/eurynome/crud/controller/ReadableController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (c) 2019-2021 Gengwei Zheng ([email protected]) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Project Name: eurynome-cloud | ||
* Module Name: eurynome-cloud-crud | ||
* File Name: ReadableController.java | ||
* Author: gengwei.zheng | ||
* Date: 2021/07/07 17:27:07 | ||
*/ | ||
|
||
package cn.herodotus.eurynome.crud.controller; | ||
|
||
import cn.herodotus.eurynome.common.definition.entity.AbstractEntity; | ||
import cn.herodotus.eurynome.common.domain.Result; | ||
import cn.herodotus.eurynome.crud.service.BaseReadableService; | ||
import org.springframework.data.domain.Page; | ||
|
||
import java.io.Serializable; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* <p>Description: 只读Controller </p> | ||
* | ||
* @author : gengwei.zheng | ||
* @date : 2021/7/7 17:27 | ||
*/ | ||
public interface ReadableController<E extends AbstractEntity, ID extends Serializable> extends Controller { | ||
|
||
/** | ||
* 获取Service | ||
* | ||
* @return Service | ||
*/ | ||
BaseReadableService<E, ID> getBaseReadableService(); | ||
|
||
default Result<Map<String, Object>> findByPage(Integer pageNumber, Integer pageSize) { | ||
Page<E> pages = getBaseReadableService().findByPage(pageNumber, pageSize); | ||
return result(pages); | ||
} | ||
|
||
default Result<List<E>> findAll() { | ||
List<E> domains = getBaseReadableService().findAll(); | ||
return result(domains); | ||
} | ||
|
||
default Result<E> findById(ID id) { | ||
E domain = getBaseReadableService().findById(id); | ||
return result(domain); | ||
} | ||
} |
Oops, something went wrong.