在开始代码实现之前,需要先安装好MongoDB和Java开发环境,以及导入所需的maven依赖.
加入项目依赖:
xml org.springframework.boot spring-boot-starter-data-mongodb-reactive
在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中,可以使用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是业务层,可以在其中调用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 {
}
}
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中,我们需要使用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中调用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相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!