Skip to content

Instantly share code, notes, and snippets.

@SalithaUCSC
Created January 10, 2021 12:39
Custom repository implementation for ProductRepositoryCustom
package com.api.reporting.respositories;
import com.api.reporting.models.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import java.util.List;
import java.util.Optional;
public class ProductRepositoryImpl implements ProductRepositoryCustom {
private static final String COLLECTION = "products";
@Autowired
MongoTemplate mongoTemplate;
public Optional<List<Product>> findByName(String name) {
Query query = new Query().addCriteria(Criteria.where("name").is(name));
List<Product> search = mongoTemplate.find(query, Product.class, COLLECTION);
return Optional.of(search);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment