gpt4 book ai didi

java - SpringBoot CrudRepository中如何将原生查询从Service层传递到Repository层

转载 作者:行者123 更新时间:2023-12-02 10:12:05 26 4
gpt4 key购买 nike

我有 SpringBoot 应用程序。它的存储库正在使用 CrudRepository

i am trying to pass a native query from my Service layer to Repository layer. So how can i do this ? I know there is @Query annotation. but i want to know how to pass native query from my Service layer to Repository layer? If not possible can you tell me how to write below query in @Query annotations

服务层方法

public List<Application> search(Application app) {
List<Application> list = new ArrayList<>();
StringBuilder sb = new StringBuilder();
sb.append("SELECT * FROM applications WHRERE is_delete = 0 ");
try {
if(app.getFirst_name().trim().length() > 0 && app.getFirst_name() != null) {
sb.append("AND first_name = '" + app.getFirst_name() + "'");
}

if(app.getLast_name().trim().length() > 0 && app.getLast_name() != null) {
sb.append("AND last_name = '" + app.getLast_name() + "'");
}

if(app.getEmail().trim().length() > 0 && app.getEmail() != null) {
sb.append("AND email = '" + app.getEmail() + "'");
}

if(app.getPhone().trim().length() > 0 && app.getPhone() != null) {
sb.append("AND phone = '" + app.getPhone() + "'");
}

if(app.getStatus()+"".length() > 0) {
sb.append("AND status = '" + app.getStatus() + "'");
}

//Here i am preparing search query
String query = sb.toString();
list = (List<Application>) applicationDao.search(query);

} catch (Exception e) {
e.printStackTrace();
}

return list;
}

IApplicationDao

package com.Mortgage.MortgageLoanAPI.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import com.Mortgage.MortgageLoanAPI.models.Application;

public interface IApplicationDao extends CrudRepository<Application, Long>{

List<Application> search(String query);
}

最佳答案

您可以尝试 spring 数据规范。下面是例子。更多详情请引用https://dzone.com/articles/using-spring-data-jpa-specification

public class UserSpecification implements Specification<User> {

private SearchCriteria criteria;

@Override
public Predicate toPredicate
(Root<User> root, CriteriaQuery<?> query, CriteriaBuilder builder) {

if (criteria.getOperation().equalsIgnoreCase(">")) {
return builder.greaterThanOrEqualTo(
root.<String> get(criteria.getKey()), criteria.getValue().toString());
}
else if (criteria.getOperation().equalsIgnoreCase("<")) {
return builder.lessThanOrEqualTo(
root.<String> get(criteria.getKey()), criteria.getValue().toString());
}
else if (criteria.getOperation().equalsIgnoreCase(":")) {
if (root.get(criteria.getKey()).getJavaType() == String.class) {
return builder.like(
root.<String>get(criteria.getKey()), "%" + criteria.getValue() + "%");
} else {
return builder.equal(root.get(criteria.getKey()), criteria.getValue());
}
}
return null;
}
}

关于java - SpringBoot CrudRepository中如何将原生查询从Service层传递到Repository层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54943206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com