springboot使用MongoTemplate

75 阅读1分钟

引入依赖

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

数据源配置

在application.properties中添加配置

spring.data.mongodb.uri=mongodb://${mongodb.user}:${mongodb.pwd}@${mongodb.nodes}/${mongodb.db}

使用MongoTemplate

在service中直接注入MongoTemplate

@Resource 
private MongoTemplate mongoTemplate;

MongoTemplate的api使用可参考:learnku.com/articles/60…