优化代码,针对增改场景使用传统方式实现
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
package com.lanyuanxiaoyao.server;
|
||||
|
||||
import com.blinkfox.fenix.EnableFenix;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@@ -13,6 +16,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
* @since 2025-03-03
|
||||
*/
|
||||
@Slf4j
|
||||
@EnableFenix
|
||||
@EnableConfigurationProperties
|
||||
@SpringBootApplication
|
||||
public class ServerApplication {
|
||||
public static void main(String[] args) {
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.lanyuanxiaoyao.server.controller;
|
||||
|
||||
import com.lanyuanxiaoyao.server.controller.base.AbstractController;
|
||||
import com.lanyuanxiaoyao.server.entity.Organization;
|
||||
import com.lanyuanxiaoyao.server.service.OrganizationService;
|
||||
import com.lanyuanxiaoyao.server.service.base.AbstractService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("organization")
|
||||
public class OrganizationController extends AbstractController<Organization, Long> {
|
||||
private final OrganizationService organizationService;
|
||||
|
||||
public OrganizationController(OrganizationService organizationService) {
|
||||
this.organizationService = organizationService;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AbstractService<Organization, Long> getService() {
|
||||
return organizationService;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.lanyuanxiaoyao.server.controller.base;
|
||||
|
||||
import com.lanyuanxiaoyao.server.service.base.AbstractService;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
|
||||
public abstract class AbstractController<ENTITY, ID> {
|
||||
public abstract AbstractService<ENTITY, ID> getService();
|
||||
|
||||
@PostMapping("save")
|
||||
public ID save(@RequestBody ENTITY entity) {
|
||||
return getService().save(entity);
|
||||
}
|
||||
}
|
||||
@@ -37,4 +37,12 @@ public class Organization {
|
||||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "organization")
|
||||
@ToString.Exclude
|
||||
private Set<Department> departments;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public static final class SaveVO {
|
||||
private Long id;
|
||||
private String name;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.lanyuanxiaoyao.server.entity.mapper;
|
||||
|
||||
import com.lanyuanxiaoyao.server.entity.Department;
|
||||
import com.lanyuanxiaoyao.server.entity.mapper.qualifier.DepartmentQualifier;
|
||||
import com.lanyuanxiaoyao.server.entity.mapper.qualifier.OrganizationQualifier;
|
||||
import com.lanyuanxiaoyao.server.entity.vo.DepartmentSaveVO;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
|
||||
/**
|
||||
* 部门bean转换
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250327
|
||||
*/
|
||||
@Mapper(
|
||||
uses = {
|
||||
OrganizationQualifier.class,
|
||||
DepartmentQualifier.class,
|
||||
}
|
||||
)
|
||||
public abstract class DepartmentMapper implements EntityMapper<Department, DepartmentSaveVO> {
|
||||
@Mapping(target = "organization", source = "organizationId")
|
||||
@Mapping(target = "parent", source = "parentId")
|
||||
@Override
|
||||
public abstract Department fromVO(DepartmentSaveVO saveVO);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.lanyuanxiaoyao.server.entity.mapper;
|
||||
|
||||
interface EntityMapper<SE, CE> {
|
||||
SE fromVO(CE creationVO);
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.lanyuanxiaoyao.server.entity.mapper;
|
||||
|
||||
import com.lanyuanxiaoyao.server.entity.Organization;
|
||||
import org.mapstruct.Mapper;
|
||||
|
||||
/**
|
||||
* 组织bean转换
|
||||
*
|
||||
* @author lanyuanxiaoyao
|
||||
* @version 20250327
|
||||
*/
|
||||
@Mapper
|
||||
public interface OrganizationMapper extends EntityMapper<Organization, Organization.SaveVO> {
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.lanyuanxiaoyao.server.entity.mapper.qualifier;
|
||||
|
||||
import com.lanyuanxiaoyao.server.entity.Department;
|
||||
import com.lanyuanxiaoyao.server.repository.DepartmentRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class DepartmentQualifier implements EntityQualifier<Department, Long> {
|
||||
private final DepartmentRepository departmentRepository;
|
||||
|
||||
public DepartmentQualifier(DepartmentRepository departmentRepository) {
|
||||
this.departmentRepository = departmentRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Department get(Long id) {
|
||||
return departmentRepository.getReferenceById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.lanyuanxiaoyao.server.entity.mapper.qualifier;
|
||||
|
||||
public interface EntityQualifier<E, ID> {
|
||||
E get(ID id);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.lanyuanxiaoyao.server.entity.mapper.qualifier;
|
||||
|
||||
import com.lanyuanxiaoyao.server.entity.Organization;
|
||||
import com.lanyuanxiaoyao.server.repository.OrganizationRepository;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class OrganizationQualifier implements EntityQualifier<Organization, Long> {
|
||||
private final OrganizationRepository organizationRepository;
|
||||
|
||||
public OrganizationQualifier(OrganizationRepository organizationRepository) {
|
||||
this.organizationRepository = organizationRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Organization get(Long id) {
|
||||
return organizationRepository.getReferenceById(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.lanyuanxiaoyao.server.entity.vo;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import lombok.ToString;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@ToString
|
||||
public class DepartmentSaveVO {
|
||||
private Long id;
|
||||
private String name;
|
||||
private Long organizationId;
|
||||
private Long parentId;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.lanyuanxiaoyao.server.repository;
|
||||
|
||||
import com.blinkfox.fenix.jpa.FenixJpaRepository;
|
||||
import com.blinkfox.fenix.specification.FenixJpaSpecificationExecutor;
|
||||
import com.lanyuanxiaoyao.server.entity.Department;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface DepartmentRepository extends FenixJpaRepository<Department, Long>, FenixJpaSpecificationExecutor<Department> {
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.lanyuanxiaoyao.server.repository;
|
||||
|
||||
import com.blinkfox.fenix.jpa.FenixJpaRepository;
|
||||
import com.blinkfox.fenix.specification.FenixJpaSpecificationExecutor;
|
||||
import com.lanyuanxiaoyao.server.entity.Organization;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface OrganizationRepository extends FenixJpaRepository<Organization, Long>, FenixJpaSpecificationExecutor<Organization> {
|
||||
@Override
|
||||
Organization getReferenceById(Long aLong);
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.lanyuanxiaoyao.server.service;
|
||||
|
||||
import com.lanyuanxiaoyao.server.entity.Organization;
|
||||
import com.lanyuanxiaoyao.server.repository.OrganizationRepository;
|
||||
import com.lanyuanxiaoyao.server.service.base.AbstractService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
public class OrganizationService extends AbstractService<Organization, Long> {
|
||||
private final OrganizationRepository organizationRepository;
|
||||
|
||||
public OrganizationService(OrganizationRepository organizationRepository) {
|
||||
this.organizationRepository = organizationRepository;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long save(Organization organization) {
|
||||
Organization save = organizationRepository.saveOrUpdateByNotNullProperties(organization);
|
||||
return save.getId();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.lanyuanxiaoyao.server.service.base;
|
||||
|
||||
public abstract class AbstractService<ENTITY, ID> {
|
||||
public abstract ID save(ENTITY entity);
|
||||
}
|
||||
@@ -28,4 +28,6 @@ logging:
|
||||
datastores:
|
||||
jpql:
|
||||
query:
|
||||
DefaultQueryLogger: debug
|
||||
DefaultQueryLogger: debug
|
||||
fenix:
|
||||
print-banner: false
|
||||
Reference in New Issue
Block a user