下面我会提供一个完整的"springboot整合mongodb"攻略,同时包含两个示例说明供您参考。
在开始代码实现之前,需要先安装好MongoDB和Java开发环境,以及导入所需的maven依赖。
MongoDB的安装:可以按照官方文档进行下载和安装,具体可以访问MongoDB官网。
Java开发环境的安装:可以选择Eclipse或者IntelliJ IDEA等IDE,在IDEA中,安装好JDK后安装好"Spring Assistant"插件即可。
加入项目依赖:
xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
</dependency>
</dependencies>
在application.yml(或者properties)文件中添加如下配置:
spring.data.mongodb.uri=mongodb://127.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 {
@Id
private String id;
private String name;
private Integer age;
private BigDecimal salary;
// 省略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 {
@Autowired
private MongoTemplate mongoTemplate;
/**
* 插入数据
* @param employee
*/
public void insert(Employee employee) {
mongoTemplate.insert(employee);
}
/**
* 根据id查询数据
* @param id
* @return
*/
public Employee findById(String id) {
return mongoTemplate.findById(id, Employee.class);
}
}
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 {
@Autowired
private EmployeeRepository employeeRepository;
/**
* 添加员工
* @param employee
*/
public void addEmployee(Employee employee) {
employeeRepository.insert(employee);
}
/**
* 根据id查询员工
* @param id
* @return
*/
public Employee getEmployeeById(String id) {
return employeeRepository.findById(id);
}
}
在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 {
@Autowired
private EmployeeService employeeService;
@Test
public void testAddEmployee() {
Employee employee = new Employee();
employee.setName("张三");
employee.setAge(20);
employee.setSalary(new BigDecimal("7500"));
employeeService.addEmployee(employee);
}
@Test
public void testGetEmployeeById() {
Employee employee = employeeService.getEmployeeById("123456");
assertEquals("张三", employee.getName());
}
}
ReactiveMongoTemplate是Spring Data MongoDB提供的响应式编程模型的模板接口,需要添加相应的依赖。
在application.yml(或者properties)文件中添加如下配置:
spring.data.mongodb.uri=mongodb://127.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 {
@Id
private String id;
private String name;
private Integer age;
private BigDecimal salary;
// 省略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 {
@Autowired
private ReactiveMongoTemplate reactiveMongoTemplate;
/**
* 插入数据
*
* @param employee
* @return
*/
public Mono<Employee> insert(Employee employee) {
return reactiveMongoTemplate.insert(employee);
}
/**
* 根据id查询数据
*
* @param id
* @return
*/
public Mono<Employee> findById(String id) {
return reactiveMongoTemplate.findById(id, Employee.class);
}
/**
* 根据姓名查询
*
* @param name
* @return
*/
public Flux<Employee> findByName(String name) {
Employee employee = new Employee();
employee.setName(name);
return reactiveMongoTemplate.find(Example.of(employee), Employee.class);
}
}
可以在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 {
@Autowired
private EmployeeRepository employeeRepository;
/**
* 添加员工
*
* @param employee
* @return
*/
public Mono<Employee> addEmployee(Employee employee) {
return employeeRepository.insert(employee);
}
/**
* 根据id查询员工
*
* @param id
* @return
*/
public Mono<Employee> getEmployeeById(String id) {
return employeeRepository.findById(id);
}
/**
* 根据姓名查询员工
*
* @param name
* @return
*/
public Flux<Employee> getEmployeeByName(String name) {
return employeeRepository.findByName(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 {
@Autowired
private EmployeeService employeeService;
@Test
public void testAddEmployee() {
Employee employee = new Employee();
employee.setName("张三");
employee.setAge(20);
employee.setSalary(new BigDecimal("7500"));
Mono<Employee> mono = employeeService.addEmployee(employee);
StepVerifier.create(mono).expectNextCount(1).verifyComplete();
}
@Test
public void testGetEmployeeByName() {
Flux<Employee> flux = employeeService.getEmployeeByName("张三");
StepVerifier.create(flux).expectNextCount(1).verifyComplete();
}
}
这就是一个完整的"springboot整合mongodb"的攻略,您可以根据不同的需求和项目中,采用不同的实现方式和技术手段。
本文链接:http://task.lmcjl.com/news/18741.html