上篇我们讲了mongodb关于jpa的操作,这篇我们来讲课和演示MongoTemplate的使用:
1、使用sping的MongoTemplate需要引用的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2、注入MongoTemplate,链接过程的说明
代码中直接注入该类:
@Resource private MongoTemplate mongoTemplate;
那么引入该类之后,在启动的时候是如何创建链接的呢?
ctrl+shift+r搜索该类使用的配置文件mongoproperties.class的包
下面是该引用类的具体代码:
在启动的时候spring会自动读取配置文件中前缀为spring.data.mongodb的链接参数并创建链接
主要参数还是uri、host、端口号和数据库,并默认了端口27017和本地链接地址
此处的配置文件中我是配置了本地虚拟机可链接的mongodb对应地址的,所以我这边就能正常链接上。
3、关于 MongoTemplate的基本操作
具体的测试代码如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.springframework.boot.autoconfigure.mongo;
import com.mongodb.ConnectionString;
import org.bson.UuidRepresentation;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(
prefix = "spring.data.mongodb"
)
public class MongoProperties {
public static final int DEFAULT_PORT = 27017;
public static final String DEFAULT_URI = "mongodb://localhost/test";
private String host;
private Integer port = null;
private String uri;
private String database;
private String authenticationDatabase;
private String gridFsDatabase;
private String username;
private char[] password;
private String replicaSetName;
private Class<?> fieldNamingStrategy;
private UuidRepresentation uuidRepresentation;
private Boolean autoIndexCreation;
public MongoProperties() {
this.uuidRepresentation = UuidRepresentation.JAVA_LEGACY;
}
public String getHost() {
return this.host;
}
public void setHost(String host) {
this.host = host;
}
public String getDatabase() {
return this.database;
}
public void setDatabase(String database) {
this.database = database;
}
public String getAuthenticationDatabase() {
return this.authenticationDatabase;
}
public void setAuthenticationDatabase(String authenticationDatabase) {
this.authenticationDatabase = authenticationDatabase;
}
public String getUsername() {
return this.username;
}
public void setUsername(String username) {
this.username = username;
}
public char[] getPassword() {
return this.password;
}
public void setPassword(char[] password) {
this.password = password;
}
public String getReplicaSetName() {
return this.replicaSetName;
}
public void setReplicaSetName(String replicaSetName) {
this.replicaSetName = replicaSetName;
}
public Class<?> getFieldNamingStrategy() {
return this.fieldNamingStrategy;
}
public void setFieldNamingStrategy(Class<?> fieldNamingStrategy) {
this.fieldNamingStrategy = fieldNamingStrategy;
}
public UuidRepresentation getUuidRepresentation() {
return this.uuidRepresentation;
}
public void setUuidRepresentation(UuidRepresentation uuidRepresentation) {
this.uuidRepresentation = uuidRepresentation;
}
public String getUri() {
return this.uri;
}
public String determineUri() {
return this.uri != null ? this.uri : "mongodb://localhost/test";
}
public void setUri(String uri) {
this.uri = uri;
}
public Integer getPort() {
return this.port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getGridFsDatabase() {
return this.gridFsDatabase;
}
public void setGridFsDatabase(String gridFsDatabase) {
this.gridFsDatabase = gridFsDatabase;
}
public String getMongoClientDatabase() {
return this.database != null ? this.database : (new ConnectionString(this.determineUri())).getDatabase();
}
public Boolean isAutoIndexCreation() {
return this.autoIndexCreation;
}
public void setAutoIndexCreation(Boolean autoIndexCreation) {
this.autoIndexCreation = autoIndexCreation;
}
}
这里以插入为例进行讲解和演示,通过mongoTemplate操作mongodb的时候需要创建对应的实体,对应实体的类名就是存入之后的集合名称。
我们先进入czing的库,查看一下现有的集合,是存在我们要操作的Template实体对应集合的
然后执行我们的第一个插入方法:
执行成功我们去查看mongodb里面czing库下现在的集合和数据:
增加了template集合且该集合下面插入了我们测试的数据。
那么,在插入的时候mongodb是如果把操作实体的类名作为集合(collection)名称使用的呢?
具体我们可以进入mongotemplate的insert方法看下具体代码
到这里,mongotemplate的大体操作流程给大家演示完毕了,其他mongotemplate下面的 操作方法可以按需验证和使用。