spring连接mongodb有两种方式:
- spring boot starter
maven 依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>2.1.4.RELEASE</version>
</dependency>
实现MongoRepository即可通过spring的模板类来使用mongo的方法
- spring data mongo
maven依赖
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>${mongodb-driver-version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>${spring-data-mongodb-version}</version>
<scope>compile</scope>
</dependency>
使用
public static MongoTemplate mongoTemplate;
@Override
public Object toClient() {
if(mongoTemplate==null){
mongoTemplate = new MongoTemplate(new SimpleMongoClientDatabaseFactory(url));
}
return mongoTemplate;
}
- 拓展资料