一、在 Linux 上开启 mongodb 复制集的先期准备

1、开启 复制集 所有节点的端口,供远程访问

/sbin/iptables -I INPUT -p tcp --dport 28001 -j ACCEPT&&/etc/init.d/iptables save&&service iptables restart&&/etc/init.d/iptables status
/sbin/iptables -I INPUT -p tcp --dport 28002 -j ACCEPT&&/etc/init.d/iptables save&&service iptables restart&&/etc/init.d/iptables status
/sbin/iptables -I INPUT -p tcp --dport 28003 -j ACCEPT&&/etc/init.d/iptables save&&service iptables restart&&/etc/init.d/iptables status

2、创建拥有读写权限的用户

3、启动实例

二、整合

(一)添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

(二)配置 mongodb
​​​application.properties​

spring.application.name=spring-boot-mongodb
server.port=8080
management.endpoints.web.exposure.include=*

#mongodb配置项mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
spring.data.mongodb.uri=mongodb://root:root@192.168.10.130:28001,192.168.10.130:28002,192.168.10.130:28003/admin

注意 ​​uri​​​ 的书写格式:
​​​mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]​

(三)编写测试类

1、entity

import lombok.Data;

import java.io.Serializable;
import java.math.BigInteger;

/**
* @author 咸鱼
* @date 2018/12/13 16:18
*/
@Data
public class User implements Serializable {
private BigInteger id;
private String name;
private Integer age;
}

注意:
  若 POJO 对象的 id 字段为Long 类型,那 Mongodb 会自动将 id 字段转换为集合中的 _id 字段(_id:Mongodb数据库中数据条目的标识,若不指定,自动生成)。这样会带来一个问题,我们再从 Mongodb 中取 POJO 对象时,id 字段会转型失败,会报​​​with root cause org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type org.bson.types.ObjectId to type java.lang.Long​​​异常。
  为了避免这种情况出现,可以将 POJO 对象的 id 字段类型设置为 BigInteger 或 String。

2、Repository

import lombok.RequiredArgsConstructor;
import org.pc.mongodb.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* @author 咸鱼
* @date 2018/12/13 16:09
*/
@Repository
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MongodbRepository {
private final MongoTemplate mongoTemplate;

public boolean saveUser(User user){
mongoTemplate.insert(user, "imooc");
return true;
}
public List<User> findAllUsers(){
return mongoTemplate.findAll(User.class, "imooc");
}
}

3、Controller

import lombok.RequiredArgsConstructor;
import org.pc.mongodb.entity.User;
import org.pc.mongodb.repository.MongodbRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @author 咸鱼
* @date 2018/12/13 16:08
*/
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MongodbController {
private final MongodbRepository mongodbRepository;

@PostMapping("/user")
public boolean saveUser(@RequestBody User user){
return mongodbRepository.saveUser(user);
}

@GetMapping("/user/all")
public List<User> getAllUsers(){
return mongodbRepository.findAllUsers();
}
}