Spring Boot with MongoTemplate

十年热恋 提交于 2020-12-30 05:54:50

问题


I am new to Spring Boot and MongoDb. Trying some examples with Mongo Repositories and Spring Boot. But after going through some of the documents found that Mongo Template is will be a better option. Unable to get a proper Spring Boot with Mongo Template example.

  1. Can someone please help me out with an example for the same.

  2. Do we need to create a User defined Repositories interface and extend Repositories or CRUD Repository, while trying for Mongo Template ?


回答1:


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

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

If you found doing some kind of operation is too complicated with Spring query-creation, and somehow doesn't want to use @Query (for example, you want IDE type hint when constructing queries), you can extend the MongoRepository and use MongoTemplate as the query mechanism.

First we extend our repository with our custom 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 custom repository. We can autowire the 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);
    }
}

Last, we use the ArticleRepository.

@Service
public class ArticleServiceImpl {

    @Autowired
    private ArticleRepository articleRepository;

    public List<Article> getArticleByPage() {
        return articleRepository.getArticleFilteredByPage(1, 10);
    }
}



回答2:


I have found some examples using 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, please 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=

SpringBoot class

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

Mongo Repository

@Repository
public interface UserRepository extends MongoRepository<User, Long> {}


来源:https://stackoverflow.com/questions/38288258/spring-boot-with-mongotemplate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!