Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加CustomHandlerController样例 #20

Merged
merged 1 commit into from
Apr 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<CustomHandlerUserService> {
}
Original file line number Diff line number Diff line change
@@ -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<CustomHandlerUserRoleService> {
}
Original file line number Diff line number Diff line change
@@ -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<T extends BaseCustomHandlerService> {
@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);
}
}
57 changes: 57 additions & 0 deletions src/main/java/com/power/doc/service/BaseCustomHandlerService.java
Original file line number Diff line number Diff line change
@@ -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<T extends XbootBaseEntity> {
private Class<T> entityClass;

/**
* 构造函数,获取类型信息
*/
@PostConstruct
public void init() {
entityClass = (Class<T>) ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
}

/**
* 查询列表
*
* @return
*/
public List<T> findList() {
return new ArrayList<>();
}

/**
* 创建
*
* @param entity
*/
public void create(T entity) {

}

public Class<T> getEntityClass() {
return entityClass;
}

/**
* 通过 T 的类型信息实例化
* @return
*/
public T newEntity() {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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<UserRole> {
}
14 changes: 14 additions & 0 deletions src/main/java/com/power/doc/service/CustomHandlerUserService.java
Original file line number Diff line number Diff line change
@@ -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<User> {
}