简体   繁体   English

Spring Data Repository - 方法签名

[英]Spring Data Repository - Method Signatures

I have the following code: 我有以下代码:

public interface CompanyRepository extends JpaRepository<Company, String> { }
  • Where can i view the methods generated by this? 我在哪里可以查看由此生成的方法?
  • Do i have to list all the generated methods also in my service interface, before i can use them in my Controllers? 在我可以在控制器中使用它们之前,是否还必须在我的服务界面中列出所有生成的方法?

     public interface CompanyService { } 

I would liked to use the repository methods via my Service implementation class but it seems there is no way to do that. 我希望通过我的Service实现类使用存储库方法,但似乎没有办法做到这一点。

Spring Data JPA instantiated your repo for you 'on the fly' (with it's magic )). Spring Data JPA为你“动态”实例化了你的回购(用它的魔力)。 You can find a repo implementation, for example, here . 您可以在此处找到repo实现。

So in common case it's not necessary to duplicate all these methods of your repo. 因此,在通常情况下,没有必要复制您的仓库的所有这些方法。 Instead you should add your custom ' query methods ' to it. 相反,您应该为其添加自定义查询方法 ”。 For example: 例如:

public interface CompanyRepo extends JpaRepository<Company, String> {
    List<Company> findAllByName(String companyName);
}

Spring (again, with its magic) implements this method for you (you can find here about how to create such methods). Spring(再次,用它的魔力)为你实现这个方法(你可以在这里找到关于如何创建这样的方法)。 So you will have the repo with basic methods ( save , findAll , findById , delete etc.) and with your custom methods. 因此,您将拥有基本方法( savefindAllfindByIddelete等)和自定义方法的repo。

Then you can use this repo in your transactional service to implement your business logic: 然后,您可以在事务服务中使用此repo来实现业务逻辑:

@Service
@Transactional
public class CompanyService {

    private final CompanyRepo companyRepo;

    public CompanyService(CompanyRepo companyRepo) {
        this.companyRepo = companyRepo;
    }

    public Company create(CompanyDto dto) {
        Company company = dto.toCompany();
        // Some custom logic...
        return companyRepo.save(company);
    }
}

Then use the service in your controller: 然后在控制器中使用该服务:

@RestController
@RequestMapping("/companies")
public class CompanyController {
    private final CompanyService companyService;

    public CompanyController(CompanyService companyService) {
        this.companyService = companyService;
    }

    @PostMapping
    public ResponseEntity create(@RequestBody CompanyDto dto) {
        return ResponseEntity.ok(companyService.create(dto));
    }
}

They are all here: Spring Docs . 他们都在这里: Spring Docs

And no, do not expose them directly in your service, your service must use them; 不,不要直接在您的服务中公开它们,您的服务必须使用它们; even if there is simple delegate in some cases (like find\\delete , etc). 即使在某些情况下有简单的委托(比如find\\delete等)。

Exposing them in your controller directly, will create a small confusion, especially when testing - since you will need to mock them, test them etc (and spring already tested them). 将它们直接暴露在控制器中会产生一些小的混乱,特别是在测试时 - 因为你需要模拟它们,测试它们等(并且spring已经测试过它们)。 I find it much cleaner when the controller interacts with the service only, and this for example, is enforced in our project. 当控制器仅与服务交互时,我发现它更清晰,例如,这在我们的项目中强制执行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM