优化前后端交互
This commit is contained in:
@@ -2,6 +2,8 @@ package com.lanyuanxiaoyao.server.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.server.controller.base.AbstractController;
|
||||
import com.lanyuanxiaoyao.server.entity.Organization;
|
||||
import com.lanyuanxiaoyao.server.entity.mapper.EntityMapper;
|
||||
import com.lanyuanxiaoyao.server.entity.mapper.OrganizationMapper;
|
||||
import com.lanyuanxiaoyao.server.service.OrganizationService;
|
||||
import com.lanyuanxiaoyao.server.service.base.AbstractService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -11,15 +13,22 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("organization")
|
||||
public class OrganizationController extends AbstractController<Organization, Long> {
|
||||
public class OrganizationController extends AbstractController<Organization, Long, Organization.SaveVO> {
|
||||
private final OrganizationService organizationService;
|
||||
private final OrganizationMapper organizationMapper;
|
||||
|
||||
public OrganizationController(OrganizationService organizationService) {
|
||||
public OrganizationController(OrganizationService organizationService, OrganizationMapper organizationMapper) {
|
||||
this.organizationService = organizationService;
|
||||
this.organizationMapper = organizationMapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractService<Organization, Long> getService() {
|
||||
return organizationService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityMapper<Organization, Organization.SaveVO> getEntityMapper() {
|
||||
return organizationMapper;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,21 +1,24 @@
|
||||
package com.lanyuanxiaoyao.server.controller.base;
|
||||
|
||||
import com.lanyuanxiaoyao.server.entity.mapper.EntityMapper;
|
||||
import com.lanyuanxiaoyao.server.service.base.AbstractService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
public abstract class AbstractController<ENTITY, ID> {
|
||||
public abstract class AbstractController<ENTITY, ID, SAVE_VO> {
|
||||
public abstract AbstractService<ENTITY, ID> getService();
|
||||
|
||||
public abstract EntityMapper<ENTITY, SAVE_VO> getEntityMapper();
|
||||
|
||||
@GetMapping("/get/{id}")
|
||||
public ENTITY get(@PathVariable("id") ID id) {
|
||||
return getService().get(id);
|
||||
}
|
||||
|
||||
@PostMapping("/save")
|
||||
public ID save(@RequestBody ENTITY entity) {
|
||||
return getService().save(entity);
|
||||
public ID save(@RequestBody SAVE_VO entity) {
|
||||
return getService().save(getEntityMapper().fromVO(entity));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ public class Organization {
|
||||
@GeneratedValue(generator = "snowflakeId")
|
||||
@GenericGenerator(name = "snowflakeId", type = SnowflakeId.IdGenerator.class)
|
||||
private Long id;
|
||||
private String code;
|
||||
private String name;
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "organization")
|
||||
@@ -43,6 +44,7 @@ public class Organization {
|
||||
@ToString
|
||||
public static final class SaveVO {
|
||||
private Long id;
|
||||
private String code;
|
||||
private String name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ import org.mapstruct.Mapping;
|
||||
DepartmentService.class,
|
||||
}
|
||||
)
|
||||
public abstract class DepartmentMapper implements EntityMapper<Department, DepartmentSaveVO> {
|
||||
public interface DepartmentMapper extends EntityMapper<Department, DepartmentSaveVO> {
|
||||
@Mapping(target = "organization", source = "organizationId")
|
||||
@Mapping(target = "parent", source = "parentId")
|
||||
@Override
|
||||
public abstract Department fromVO(DepartmentSaveVO saveVO);
|
||||
Department fromVO(DepartmentSaveVO saveVO);
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
package com.lanyuanxiaoyao.server.entity.mapper;
|
||||
|
||||
interface EntityMapper<SE, CE> {
|
||||
public interface EntityMapper<SE, CE> {
|
||||
SE fromVO(CE creationVO);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.lanyuanxiaoyao.server.entity.mapper;
|
||||
|
||||
import com.lanyuanxiaoyao.server.entity.Organization;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
/**
|
||||
* 组织bean转换
|
||||
@@ -11,4 +12,7 @@ import org.mapstruct.Mapper;
|
||||
*/
|
||||
@Mapper
|
||||
public interface OrganizationMapper extends EntityMapper<Organization, Organization.SaveVO> {
|
||||
@Mapping(target = "code", defaultExpression = "java(String.valueOf(creationVO.getId()))")
|
||||
@Override
|
||||
Organization fromVO(Organization.SaveVO creationVO);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user