Login
网站首页 > 文章中心 > 其它

springboot整合mongodb_springboot整合feign

作者:小编 更新时间:2023-08-24 13:52:33 浏览量:160人看过

准备工作

在开始代码实现之前,需要先安装好MongoDB和Java开发环境,以及导入所需的maven依赖.

加入项目依赖:

xml org.springframework.boot spring-boot-starter-data-mongodb-reactive

springboot整合mongodb_springboot整合feign-图1

示例1 – 使用MongoTemplate进行操作

数据库配置

在application.yml(或者properties)文件中添加如下配置:

spring.data.mongodb.uri=mongodb://12⑦0.0.1:27017/testdb

编写实体类

在Spring boot中,可以使用MongoTemplate作为MongoDB的操作入口,MongoTemplate需要的文档映射实体类必须要加上@Document注解.示例代码如下:

package com.example.demo.entity;

import java.math.BigDecimal;

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

@Document(collection = "employee")
public class Employee {

// 省略setter和getter方法
}

编写repository

在repository中,可以使用MongoTemplate作为模板进行数据访问.示例代码如下:

package com.example.demo.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.Employee;

@Repository
public class EmployeeRepository {

}
}

编写Service

Service是业务层,可以在其中调用Repository来完成具体的业务逻辑.示例代码如下:

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.entity.Employee;
import com.example.demo.repository.EmployeeRepository;

@Service
public class EmployeeService {

}
}

单元测试

在test目录下编写单元测试,我们可以使用JUnit来编写单元测试类.我们提供如下的测试代码,可以用来测试添加员工和查询员工的功能.

package com.example.demo;

import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.entity.Employee;
import com.example.demo.service.EmployeeService;

@SpringBootTest
public class EmployeeServiceTests {

}
}

示例2 – 使用ReactiveMongoTemplate进行操作

ReactiveMongoTemplate是Spring Data MongoDB提供的响应式编程模型的模板接口,需要添加相应的依赖.

数据库配置

spring.data.mongodb.uri=mongodb://12⑦0.0.1:27017/testdb

编写实体类

在这个示例中,我们依然使用Employee实体类.

package com.example.demo.entity;

import java.math.BigDecimal;

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

@Document(collection = "employee")
public class Employee {

// 省略setter和getter方法
}

编写repository

在repository中,我们需要使用ReactiveMongoTemplate作为模板进行数据访问.示例代码如下:

package com.example.demo.repository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.ReactiveMongoTemplate;
import org.springframework.stereotype.Repository;

import com.example.demo.entity.Employee;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Repository
public class EmployeeRepository {

public Mono insert(Employee employee) {
public Mono findById(String id) {
public Flux findByName(String name) {
}
}

编写Service

可以在Service中调用Repository中响应式实现的方法以完成响应式编程模型.示例代码如下:

package com.example.demo.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.demo.entity.Employee;
import com.example.demo.repository.EmployeeRepository;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@Service
public class EmployeeService {

public Mono addEmployee(Employee employee) {
public Mono getEmployeeById(String id) {
public Flux getEmployeeByName(String name) {
}
}

单元测试

编写两个单元测试用以测试添加员工和根据姓名查询员工的功能.

package com.example.demo;

import java.math.BigDecimal;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.example.demo.entity.Employee;
import com.example.demo.service.EmployeeService;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

@SpringBootTest
public class EmployeeServiceTests {

    Mono mono = employeeService.addEmployee(employee);
    Flux flux = employeeService.getEmployeeByName("张三");
}
}

以上就是土嘎嘎小编为大家整理的springboot整合mongodb相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!

版权声明:倡导尊重与保护知识产权。未经许可,任何人不得复制、转载、或以其他方式使用本站《原创》内容,违者将追究其法律责任。本站文章内容,部分图片来源于网络,如有侵权,请联系我们修改或者删除处理。

编辑推荐

热门文章