Spring Download with MongoTemplate

I am new to Spring Boot and MongoDb. Try some examples with Mongo Repositories and Spring Boot. But, after going through some of the documents found, the Mongolian template will be the best option. Failed to get the correct Spring Boot with Mongo Template example.

  • Can someone please help me with an example for this.

  • Do I need to create repository user interfaces and expand CRUD repositories or repositories when trying to create a Mongo template?

+10
java spring-boot mongodb mongorepository mongotemplate
source share
2 answers

I found some examples using the Mongo template

http://docs.spring.io/spring-data/data-document/docs/current/reference/html/#mongo-template

http://www.mkyong.com/mongodb/spring-data-mongodb-hello-world-example/

If you are interested in using JPA, see below.

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>{mongo.driver.version}</version> </dependency> 

application.properties

 #Mongo DB spring.data.mongodb.database= spring.data.mongodb.host= spring.data.mongodb.password= spring.data.mongodb.port= spring.data.mongodb.repositories.enabled= spring.data.mongodb.uri= spring.data.mongodb.username= 

Class springboot

 @SpringBootApplication @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) public class UserApp { 

Mongo repository

 @Repository public interface UserRepository extends MongoRepository<User, Long> {} 
+3
source share

For further explanation, you can even use both at the same time.

MongoRepository is just an abstraction layer like MongoTemplate , but with a simpler interface.

If you find that performing an operation is too complicated in Spring- creating queries and for some reason does not want to use @Query (for example, you need a hint like IDE when building queries), you can extend MongoRepository and use MongoTemplate as a query mechanism.

First, we expand our repository using our user interface.

 @Repository public interface ArticleRepository extends MongoRepository<Article, String>, CustomArticleRepository { } 

Then declare the interface.

 public interface CustomArticleRepository { List<Article> getArticleFilteredByPage(int page, int num); } 

And then implement our own repository. We can automatically connect MongoTemplate here and use it to query the database.

 public class CustomArticleRepositoryImpl implements CustomArticleRepository { @Autowired MongoTemplate mongoTemplate; @Override public List<Article> getArticleFilteredByPage(int page, int num) { return mongoTemplate.findAll(Article.class) .skip(page * num) .take(num); } } 

Finally, we use ArticleRepository .

 @Service public class ArticleServiceImpl { @Autowired private ArticleRepository articleRepository; public List<Article> getArticleByPage() { return articleRepository.getArticleFilteredByPage(1, 10); } } 
0
source share

All Articles