JeecgBoot与MongoDB集成实战文档
2021/12/26 2:08:11
本文主要是介绍JeecgBoot与MongoDB集成实战文档,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
1. 坐标引入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
2. 升级积木报表
jeecgboot2.4.6/3.0等版本集成mongodb会报mongoTemplate错误,官方已经提供了解决方案,将积木报表升级到最新版即可。
<dependency> <groupId>org.jeecgframework.jimureport</groupId> <artifactId>jimureport-spring-boot-starter</artifactId> <version>1.4.2</version> </dependency>
之后在application-dev.yml文件中,加入mongoDb的配置项
spring: data: mongodb: uri: mongodb://localhost:27017/springboot-db
3. 创建实体类
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); } }
4. 创建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, String> { Customer findByFirstName(String firstName); List<Customer> findByLastName(String lastName); }
5. 测试用例
用两种方式测试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<String,String> 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("存入成功"); } }
6. 测试结果
测试后的数据库截图
这篇关于JeecgBoot与MongoDB集成实战文档的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24MongoDB资料:新手入门完全指南
- 2024-12-20go-zero 框架的 RPC 服务 启动start和停止 底层是怎么实现的?-icode9专业技术文章分享
- 2024-12-19Go-Zero 框架的 RPC 服务启动和停止的基本机制和过程是怎么实现的?-icode9专业技术文章分享
- 2024-12-18怎么在golang中使用gRPC测试mock数据?-icode9专业技术文章分享
- 2024-12-15掌握PageRank算法核心!你离Google优化高手只差一步!
- 2024-12-15GORM 中的标签 gorm:"index"是什么?-icode9专业技术文章分享
- 2024-12-11怎么在 Go 语言中获取 Open vSwitch (OVS) 的桥接信息(Bridge)?-icode9专业技术文章分享
- 2024-12-11怎么用Go 语言的库来与 Open vSwitch 进行交互?-icode9专业技术文章分享
- 2024-12-11怎么在 go-zero 项目中发送阿里云短信?-icode9专业技术文章分享
- 2024-12-11怎么使用阿里云 Go SDK (alibaba-cloud-sdk-go) 发送短信?-icode9专业技术文章分享