2021-08-09-MongoDB连接+Spring Data Mongo简单操作

依赖:

    <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>3.9.0</version>
    </dependency>

出问题可能要降版本

org.springframework.boot
spring-boot-starter-parent
1.5.8.RELEASE

无需密码连接:

public void a(){
        MongoClient mongoClient = new MongoClient("127.0.0.1",27017);
        MongoDatabase conn = mongoClient.getDatabase("test");
        System.out.println("连接成功");

        if(mongoClient!=null){
            mongoClient.close();
        }
    }

需要密码的连接

package com.example.demo;

import com.mongodb.MongoClient;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;

import java.util.ArrayList;

public class test01 {
    public static void main(String[] args) {
        ServerAddress serverAddress = new ServerAddress("127.0.0.1",27017);
        ArrayList<ServerAddress> serverAddressArrayList = new ArrayList<>();
        serverAddressArrayList.add(serverAddress);

        MongoCredential mongoCredential = MongoCredential.createMongoCRCredential("","","".toCharArray());
        ArrayList<MongoCredential> arrayList = new ArrayList<>();
        arrayList.add(mongoCredential);

        MongoClient mongoClient = new MongoClient(serverAddressArrayList,arrayList);
        MongoDatabase conn = mongoClient.getDatabase("test");
        System.out.println("连接成功");

        if(mongoClient!=null){
            mongoClient.close();
        }
    }
}

当然,使用Spring Data Mongo 就不用这种方法了。


依赖已经导入,先配置一下

server:
  port: 8080

spring:
  data:
    mongodb:
      authentication-database: admin
      database: test
      host: 127.0.0.1
      port: 27017

类似于JPA 先来pojo

package com.example.demo.po;


import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Person {
    @Id
    private Long id;

    private String name;

    private Integer age;


    public Person(Long id,String name, Integer age) {
        this.id=id;
        this.name = name;
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }




    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}


注入MongoTemplate

package com.example.demo.controller;


import com.example.demo.po.Person;
import com.example.demo.service.impl.MonGo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {
    @Autowired
    MonGo mongo;

    @Autowired
    private MongoTemplate mongoTemplate;


    @GetMapping("/")
    @ApiOperation("MongoSaveBYHyf")
    public String get(){
        System.out.println("/ in----------------------------------");
        mongo.testMongo();
        return "OK";
    }

    @GetMapping("/findall")
    public String getAll(){
        return  mongoTemplate.findAll(Person.class).toString();
    }

}

如果要写service就看下面吧,原理还是用MongoTemplate
service

package com.example.demo.service;

public interface MongoService {
    public String testMongo();
}

serviceimpl:

package com.example.demo.service.impl;

import com.example.demo.po.Person;
import com.example.demo.service.MongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

@Service
public class MonGo implements MongoService {
    @Autowired
    private MongoTemplate mongoTemplate;

    public String testMongo(){
        mongoTemplate.save(new Person(1L,"JAY", 22));
        mongoTemplate.save(new Person(2L,"JJ",22));
        return "";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值