JeecgBoot与MongoDB集成实战文档
- 坐标引入依赖
org.springframework.boot
spring-boot-starter-data-mongodb
- 升级积木报表
jeecgboot2.4.6/3.0等版本集成mongodb会报mongoTemplate错误,官方已经提供了解决方案,将积木报表升级到最新版即可。
org.jeecgframework.jimureport
jimureport-spring-boot-starter
1.4.2
【JeecgBoot与MongoDB集成实战文档】之后在application-dev.yml文件中,加入mongoDb的配置项
spring:
data:
mongodb:
uri: mongodb://localhost:27017/springboot-db
- 创建实体类
package org.jeecg.modules.mongodb.entity;
import org.springframework.data.annotation.Id;
public class Customer {
@Id
public String id;
public String firstName;
public String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
- 创建Repository
package org.jeecg.modules.mongodb.dao;
import org.jeecg.modules.mongodb.entity.Customer;
import org.springframework.data.mongodb.repository.MongoRepository;
import java.util.List;
public interface CustomerRepository extends MongoRepository {
Customer findByFirstName(String firstName);
List findByLastName(String lastName);
}
- 测试用例
用两种方式测试mongoDB,分别为MongoRepository和MongoTemplate
package org.jeecg.modules.mongodb;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.mongodb.dao.CustomerRepository;
import org.jeecg.modules.mongodb.entity.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* 测试mongodb
*/
@RestController
@RequestMapping("/mongo")
public class MongoController {
@Autowired
private MongoTemplate mongoTemplate;
@Autowired
private CustomerRepository repository;
@GetMapping("/test1")
public Result> TestMongoDb(){
Map map = new HashMap<>();
map.put("jeecg","mongodb-jeecg");
mongoTemplate.insert(map, "testMongoDb");
return Result.OK("存入成功");
}
@GetMapping("/test2")
public Result> TestMongoDb2(){
repository.deleteAll();
// save a couple of customers
repository.save(new Customer("Alice", "Smith"));
repository.save(new Customer("Bob", "Smith"));
// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();
// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFirstName("Alice"));
System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByLastName("Smith")) {
System.out.println(customer);
}
return Result.OK("存入成功");
}
}
- 测试结果
测试后的数据库截图
文章图片
文章图片
文章图片
推荐阅读
- JAVA(抽象类与接口的区别&重载与重写&内存泄漏)
- Docker应用:容器间通信与Mariadb数据库主从复制
- 《真与假的困惑》???|《真与假的困惑》??? ——致良知是一种伟大的力量
- 第326天
- Shell-Bash变量与运算符
- MongoDB,Wondows下免安装版|MongoDB,Wondows下免安装版 (简化版操作)
- 逻辑回归的理解与python示例
- Guava|Guava RateLimiter与限流算法
- 我和你之前距离
- CGI,FastCGI,PHP-CGI与PHP-FPM