springboot——集成MongoDB

本文详细介绍了如何在Spring Boot应用中集成MongoDB,从MongoDB的安装配置,到Spring Boot项目中的pom.xml与application.properties配置,再到MongoDB的测试操作,最后提供了相关参考资料链接。
摘要由CSDN通过智能技术生成

一、安装MongoDB

sudo apt-get install mongodb

二、配置MongoDB

sudo service mongodb stop
mongod --bind_ip_all

三、springboot + MongoDB配置

  1. pom.xml配置
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
  1. application.properties配置
spring.data.mongodb.uri=mongodb://10.6.3.148:27017/mydb

四、springboot + MongoDB测试

@Repository
public interface StudentRepository extends MongoRepository<Student, String> {
}
public class AAApplicationTests {
    @Autowired
    private StudentRepository studentRepository;

    private String id;

    @Before
    public void contextLoads() throws Exception {
        Student student = new Student();
        student.setName("kk_cock");
        student.setAge(10);
        studentRepository.save(student);
        System.out.println(student.getId());
        id = student.getId();
    }

    @Test
    public void testGet() throws Exception {
        Optional<Student> optional = studentRepository.findById(id);
        if (optional.isPresent()) {
            Student student = optional.get();
            System.out.println(student.getName() + "," + student.getAge());
        } else {
            System.out.println("not found!");
        }
    }
}

输出内容:

2019-09-18-09:16:08 [main] INFO  org.mongodb.driver.connection- Opened connection [connectionId{localValue:2, serverValue:15}] to 10.6.3.148:27017
5d81855883fa3544a0d97f82
kk_cock,10

五、参考连接

  1. Spring Boot中使用MongoDB数据库
  2. Ubuntu 16.04 安装MongoDB及卸载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值