简体   繁体   中英

how to pass native query from Service layer to Repository layer in SpringBoot CrudRepository

I have SpringBoot application. It's repository is using 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

Service Layer Method

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);
}

You can try spring data specification. below is the example. for more details please refer 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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