如何为CrudRepository实现自定义方法?

时间:2014-11-12 15:47:36

标签: java spring jpa spring-batch spring-data-jpa

我想创建RepositoryItemWriter并提供自定义CrudRepository方法。

问题:CrudRepository是一个界面,如果我在public class中实现它,那么我必须明确地实现所有方法。

那么,如何在CrudRepository

中创建自定义方法实现
    @Autowired
    private MyEntityRepository repository;

    @Override
    public ItemWriter<PricingCache> temWriter() {
        RepositoryItemWriter<MyEntity> writer = new RepositoryItemWriter<>();
        writer.setRepository(repository); //expects a CrudRepository
        writer.setMethodName("save"); //how to change this to a custom method? eg firstValidatenThenSave()
        return writer;
    }

public interface MyEntityRepository extends CrudRepository<MyEntity, Long> {
}

1 个答案:

答案 0 :(得分:1)

您可以使用多种方法自定义Spring Data方法和存储库。

如果您使用的是Java8,则可以在界面上提供“默认”方法。

package com.example;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import java.util.UUID;

@RepositoryRestResource(path = "sample", collectionResourceRel = "sample")
public interface SampleRepository extends CrudRepository<Sample, Long> {

    @Override
    default Sample save(Sample sample) {

        System.out.println("DEFAULT SAVE CALLED!!");
        sample = new Sample();
        sample.setId(System.currentTimeMillis());
        sample.setWhatever("stuff");
        return sample;
    }

    @Override
    default Sample findOne(Long id){

        System.out.println("DEFAULT FIND CALLED!!");

        Sample sample = new Sample();
        sample.setId(id);
        sample.setWhatever(UUID.randomUUID().toString());

        return sample;
    }
}

如果您正在使用Spring Data Rest,那么您可以创建自己的RepositoryRestController,其请求映射“覆盖”或替换您要自定义的路径。

package com.example;

import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@RepositoryRestController
public class SampleController {

    @RequestMapping(path = "/sample", method = RequestMethod.DELETE)
    public void delete(Long deleteMe){

        System.out.println("RepositoryRestController CALLED for "+deleteMe);
    }
}

您可以使用@Query注释创建一些自定义查询方法。

@Override
@Query(value = "SELECT DISTINCT count(*) FROM SAMPLE_DISTINCT")
long count();

Spring Data Rest还有一个命名约定,可用于创建自定义搜索/ findby方法。例如,如果我的person对象有firstName和lastName,我可以创建以下方法接口,Spring Boot将为我实现它们。

List<Person> findByFirstName(String firstName);
List<Person> findByFirstNameStartsWith(String firstName);
List<Person> findByFirstNameEndsWith(String firstName);
List<Person> findByFirstNameContains(String firstName);

List<Person> findByLastName(String lastName);
List<Person> findByLastNameStartsWith(String lastName);
List<Person> findByLastNameEndsWith(String lastName);
List<Person> findByLastNameContains(String lastName);

List<Person> findByFirstNameAndLastName(String firstName, String lastName);
List<Person> findByFirstNameContainsAndLastNameContains(String firstName, String lastName);
//etc...

这应该适用于为data-rest和data-mongodb声明的存储库接口。