mongodb的sping操作MongoTemplate的使用②

上篇我们讲了mongodb关于jpa的操作,这篇我们来讲课和演示MongoTemplate的使用:

mongodb的jpa操作

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下面的 操作方法可以按需验证和使用。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值