在允许我使用一种方法而不是另一种方法的服务中传递参数

Pass a parameter in a service that allows me to use one method rather than another method

是否可以在允许我使用一种方法而不是另一种方法的服务中传递参数?

下面是控制器和服务。我想传递一个允许我使用 mybatis 而不是 jpa 的 (persistenceType) 参数。我将在 REST 调用中将其作为查询参数传递。

@Service
public class ManufacturerService implements IManufacturerService {

    @Autowired
    private ManufacturerRepository manufacturerRepository;

    @Autowired
    private ManufacturerMapper manufacturerMapper;

    @Override
    @Transactional
    public Manufacturer save(Manufacturer manufacturer) {
        //if persistenceType.equals(MYBATIS) 
        //manufacturerMapper.insert(manufacturer);
        //else manufacturerRepository.save(manufacturer);
        
        manufacturerMapper.insert(manufacturer);
        return null;
        //return manufacturerRepository.save(manufacturer);
    }
}
@RestController
@RequestMapping("/manufacturers")
public class ManufacturesController {

    public static final Logger LOG = LogManager.getLogger(ManufacturesController.class);

    @Autowired
    private ManufacturerService manufacturerService;

    @PostMapping
    public ResponseEntity<Manufacturer> createManufacturer(@RequestBody ManufacturerDTO manufacturer, @Param persistenceType) {
        LOG.info("START - createManufacturer");
        try {
            Manufacturer _manufacturer = ManufacturerMapper.toEntity(manufacturer);
            manufacturerService(persistenceType).save(_manufacturer);
            LOG.info("STOP - createManufacturer");
            return new ResponseEntity<>(_manufacturer, HttpStatus.CREATED);
        } catch (Exception e) {
            LOG.error("Error description: ", e);
            LOG.info("STOP - createManufacturer");
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

我知道不是这样的,但是让你明白我的目的是对的。

如果您在 application.properties 文件中指定与 persistenceType 相对应的 属性,这将允许您在 mybatisjpa 之间动态切换].

有关更多信息,这里有一些文档:
https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config

完全有可能。我将从创建一个 PersistenceType 枚举开始:

enum PersistenceType {
  JPA,
  MYBATIS
}

然后您需要声明您需要在您的 POST 端点接收一个 PersistenceType 并将其传递给基础 ManufacturerService,如下所示:

@RestController
@RequestMapping("/manufacturers")
public class ManufacturesController {

    public static final Logger LOG = LogManager.getLogger(ManufacturesController.class);

    @Autowired
    private ManufacturerService manufacturerService;

    @PostMapping
    public ResponseEntity<Manufacturer> createManufacturer(@RequestBody ManufacturerDTO manufacturer, @RequestParm(required = true) PersistenceType persistenceType) {
        LOG.info("START - createManufacturer");
        try {
            Manufacturer _manufacturer = ManufacturerMapper.toEntity(manufacturer);
            manufacturerService.save(_manufacturer, persistenceType);
            LOG.info("STOP - createManufacturer");
            return new ResponseEntity<>(_manufacturer, HttpStatus.CREATED);
        } catch (Exception e) {
            LOG.error("Error description: ", e);
            LOG.info("STOP - createManufacturer");
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}

最后一件事是更改您的 ManufacturerService 以在保存方法中接受一个附加参数:

@Service
public class ManufacturerService implements IManufacturerService {

    @Autowired
    private ManufacturerRepository manufacturerRepository;

    @Autowired
    private ManufacturerMapper manufacturerMapper;

    @Override
    @Transactional
    public Manufacturer save(Manufacturer manufacturer, PersistenceType persistenceType) {
        if (persistenceType.equals(PersistenceType.MYBATIS)) {
            return manufacturerMapper.insert(manufacturer);
        } else {
            return manufacturerRepository.save(manufacturer);
        }
    }
}

作为建议,我会在请求正文中包含 persistenceType,而不是作为单独的查询参数。它按原样工作,但如果所有内容都在请求正文中传递(或作为路径变量),那么对于 API 的消费者来说会更简洁、更容易理解。