簡體   English   中英

如何僅使用 object 的某些部分和 Spring JPA 編寫查詢

[英]How to write a query using only certain parts of an object with Spring JPA

我覺得這應該很簡單,但我不確定它的實際代碼。 Basically, I have my rest controller taking in 6 arguments, passing that through the Service and then using those arguments to build the object inside of the ServiceImplementation. 從那里我使用我剛剛制作的 object 向我的倉庫返回一個調用。 此調用應嘗試查詢 object 的數據庫特定參數。

此查詢是我不確定如何使用 Spring JPA 標准編寫的部分。 我只想使用我設置 object 的變量,但我不確定我是否必須寫出查詢或者 spring JPA 可以讓它更簡單嗎?

代碼:

Controller:

@RestController
public class exampleController {

    @Autowired
    private ExampleService exampleService;

    @GetMapping("/rest/example/search")
    public exampleObj searchExample (@RequestParam(value = "exLetter") String exLetter,
            @RequestParam(value = "exLang") String exLang, @RequestParam(value = "exType")int exType, 
            @RequestParam(value = "exMethod") String exMethod, @RequestParam(value = "exCd") String exCd,
            @RequestParam(value = "exOrg") String exOrg) {

        return exampleService.getExampleLetter(exLetter, exLang, exType, exMethod, exCd, exOrg);
    }
}

示例服務:

public interface ExampleService {

    public ExampleLetter getExampleLetter(String exLetter, String exLang, int exType, String exMethod, String exCd, String exOrg);
}

示例服務實現:

@Service
public class ExampleServiceImpl implements ExampleService {
    @Autowired
    private ExampleRepository exampleRepo;

    @Override
    public ExampleLetter getExampleLetter(String exLetter, String exLang, int exType, String exMethod, String exCd, String exOrg) {

        ExampleLetter examp = new ExampleLetter();

        examp.setExCd(exCd);
        examp.getKey().setExampleNumber(exLetter);
        examp.getKey().setLanguageType(exLang);
        examp.getKey().setMethod(exMethod);
        examp.getKey().setMarketOrg(exOrg);
        examp.getKey().setType(exType);

        return exampleRepo.findExampleLetter(examp);
    }   
}

回購:

@Repository
public interface ExampleRepository extends CrudRepository<ExampleLetter, ExampleLetterKey> {

}

如果我理解正確,您正在嘗試根據可能存在或不存在的過濾值進行動態查詢。 如果是這種情況,您可以使用Specification class 動態創建查詢:

首先,在您的存儲庫 class 中,擴展JpaSpecificationExecutor<ExampleLetter>

@Repository
public interface ExampleRepository extends CrudRepository<ExampleLetter, ExampleLetterKey>, JpaSpecificationExecutor<ExampleLetter> {

}  

現在,您將需要一種方法(為了組織起見,我建議您將其放在特定的 class 中)來生成查詢本身:

public class GenerateQueryForExampleLetter {

    ExampleLetter exampleLetter;

    public Specification<ExampleLetter> generateQuery() {
        return new Specification<ExampleLetter>() {
            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<ExampleLetter> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                Predicate pred = null;

                List<Predicate> predicates = new ArrayList<Predicate>();

                if (this.exampleLetter.getExCd()!= null && !this.exampleLetter.getExCd().isEmpty()) {
                    predicates.add(builder.equal(root.<String>get("exCd"), this.exampleLetter.getExCd()));
                }

    ...................

                if (this.exampleLetter.getTheFieldYouNeed()!= null && !getTheFieldYouNeed.isEmpty()) {
                    predicates.add(builder.equal(root.<TheTypeOfTheField>get("theFieldYouNeed"), this.exampleLetter.getTheFieldYouNeed()));
                }


                if (!predicates.isEmpty()) {
                    pred = builder.and(predicates.toArray(new Predicate[] {}));
                }

                return pred;
            }
        };
    }

    public void setExampleLetter (ExampleLetter el) {
        this.exampleLetter = el;
    }
}

最后,為您服務 class:

@Override
public ExampleLetter getExampleLetter(String exLetter, String exLang, int exType, String exMethod, String exCd, String exOrg) {

    ExampleLetter examp = new ExampleLetter();

    examp.setExCd(exCd);
    examp.getKey().setExampleNumber(exLetter);
    examp.getKey().setLanguageType(exLang);
    examp.getKey().setMethod(exMethod);
    examp.getKey().setMarketOrg(exOrg);
    examp.getKey().setType(exType);

    GenerateQueryForExampleLetter queryGenerator = new GenerateQueryForExampleLetter ();

    queryGenerator.setExampleLetter(examp);

    return exampleRepo.findAll(queryGenerator.generateQuery());
}

請注意, JpaSpecificationExecutor接口添加了一些實用方法供您使用,除了過濾之外,它們還支持排序和分頁。

有關更多詳細信息,請查看此處此處答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM