From 926cea943e2cf75070c6744a7b94f649f751308b Mon Sep 17 00:00:00 2001 From: younker Date: Sun, 23 Apr 2023 13:12:43 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0CustomHandlerController?= =?UTF-8?q?=E6=A0=B7=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CustomHandlerUserController.java | 15 +++++ .../CustomHandlerUserRoleController.java | 15 +++++ .../parent/BaseCustomHandlerController.java | 43 ++++++++++++++ .../doc/service/BaseCustomHandlerService.java | 57 +++++++++++++++++++ .../service/CustomHandlerUserRoleService.java | 14 +++++ .../doc/service/CustomHandlerUserService.java | 14 +++++ 6 files changed, 158 insertions(+) create mode 100644 src/main/java/com/power/doc/controller/CustomHandlerUserController.java create mode 100644 src/main/java/com/power/doc/controller/CustomHandlerUserRoleController.java create mode 100644 src/main/java/com/power/doc/controller/parent/BaseCustomHandlerController.java create mode 100644 src/main/java/com/power/doc/service/BaseCustomHandlerService.java create mode 100644 src/main/java/com/power/doc/service/CustomHandlerUserRoleService.java create mode 100644 src/main/java/com/power/doc/service/CustomHandlerUserService.java diff --git a/src/main/java/com/power/doc/controller/CustomHandlerUserController.java b/src/main/java/com/power/doc/controller/CustomHandlerUserController.java new file mode 100644 index 0000000..e0741e0 --- /dev/null +++ b/src/main/java/com/power/doc/controller/CustomHandlerUserController.java @@ -0,0 +1,15 @@ +package com.power.doc.controller; + +import com.power.doc.controller.parent.BaseCustomHandlerController; +import com.power.doc.service.CustomHandlerUserService; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author yangyang + * @date 2023/4/23 + */ +@RestController +@RequestMapping("/customHandler/user") +public class CustomHandlerUserController extends BaseCustomHandlerController { +} diff --git a/src/main/java/com/power/doc/controller/CustomHandlerUserRoleController.java b/src/main/java/com/power/doc/controller/CustomHandlerUserRoleController.java new file mode 100644 index 0000000..7e40d4f --- /dev/null +++ b/src/main/java/com/power/doc/controller/CustomHandlerUserRoleController.java @@ -0,0 +1,15 @@ +package com.power.doc.controller; + +import com.power.doc.controller.parent.BaseCustomHandlerController; +import com.power.doc.service.CustomHandlerUserRoleService; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author yangyang + * @date 2023/4/23 + */ +@RestController +@RequestMapping("/customHandler/userRole") +public class CustomHandlerUserRoleController extends BaseCustomHandlerController { +} diff --git a/src/main/java/com/power/doc/controller/parent/BaseCustomHandlerController.java b/src/main/java/com/power/doc/controller/parent/BaseCustomHandlerController.java new file mode 100644 index 0000000..c6e99df --- /dev/null +++ b/src/main/java/com/power/doc/controller/parent/BaseCustomHandlerController.java @@ -0,0 +1,43 @@ +package com.power.doc.controller.parent; + +import com.power.doc.entity.XbootBaseEntity; +import com.power.doc.service.BaseCustomHandlerService; +import jakarta.servlet.http.HttpServletRequest; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.List; + +/** + * 基础Controller,提供通用curd方法 + * + * @author yangyang + * @date 2023/4/23 + */ +public abstract class BaseCustomHandlerController { + @Autowired + private T service; + + /** + * 查询列表 + * @return + */ + @GetMapping() + public List findList() { + return service.findList(); + } + + /** + * 创建实例 + * @param request + */ + @PostMapping() + public void create(HttpServletRequest request) { + XbootBaseEntity t = service.newEntity(); + /** + * 通过request中的数据信息进行绑定 + */ + service.create(t); + } +} diff --git a/src/main/java/com/power/doc/service/BaseCustomHandlerService.java b/src/main/java/com/power/doc/service/BaseCustomHandlerService.java new file mode 100644 index 0000000..ab70539 --- /dev/null +++ b/src/main/java/com/power/doc/service/BaseCustomHandlerService.java @@ -0,0 +1,57 @@ +package com.power.doc.service; + +import com.power.doc.entity.XbootBaseEntity; +import jakarta.annotation.PostConstruct; + +import java.lang.reflect.ParameterizedType; +import java.util.ArrayList; +import java.util.List; + +/** + * 基础Service,提供通用curd方法 + * 此处泛型值传递T,即Entity信息,实际场景中传递的泛型要更多 + * + * @author yangyang + * @date 2023/4/23 + */ +public abstract class BaseCustomHandlerService { + private Class entityClass; + + /** + * 构造函数,获取类型信息 + */ + @PostConstruct + public void init() { + entityClass = (Class) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0]; + } + + /** + * 查询列表 + * + * @return + */ + public List findList() { + return new ArrayList<>(); + } + + /** + * 创建 + * + * @param entity + */ + public void create(T entity) { + + } + + public Class getEntityClass() { + return entityClass; + } + + /** + * 通过 T 的类型信息实例化 + * @return + */ + public T newEntity() { + return null; + } +} diff --git a/src/main/java/com/power/doc/service/CustomHandlerUserRoleService.java b/src/main/java/com/power/doc/service/CustomHandlerUserRoleService.java new file mode 100644 index 0000000..fa2235a --- /dev/null +++ b/src/main/java/com/power/doc/service/CustomHandlerUserRoleService.java @@ -0,0 +1,14 @@ +package com.power.doc.service; + +import com.power.doc.entity.UserRole; +import org.springframework.stereotype.Service; + +/** + * 用户角色管理Service + * + * @author yangyang + * @date 2023/4/23 + */ +@Service +public class CustomHandlerUserRoleService extends BaseCustomHandlerService { +} diff --git a/src/main/java/com/power/doc/service/CustomHandlerUserService.java b/src/main/java/com/power/doc/service/CustomHandlerUserService.java new file mode 100644 index 0000000..d637071 --- /dev/null +++ b/src/main/java/com/power/doc/service/CustomHandlerUserService.java @@ -0,0 +1,14 @@ +package com.power.doc.service; + +import com.power.doc.entity.User; +import org.springframework.stereotype.Service; + +/** + * 用户管理Service + * + * @author yangyang + * @date 2023/4/23 + */ +@Service +public class CustomHandlerUserService extends BaseCustomHandlerService { +}