依赖:
<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 "";
}
}