zoukankan      html  css  js  c++  java
  • 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. 测试结果
    测试后的数据库截图

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    本文转载自:https://blog.csdn.net/u013473447/article/details/121902906

  • 相关阅读:
    SQL随机排序
    根据经纬度获取所在城市的相关信息以及根据地点城市获取经纬度
    百度收集自动推送脚本——python版
    采集(未测试)
    网页代码测试工具(很有用)
    微信红包源码2020年最新版(完整测试版)
    mvc返回多个结果集,返回多个视图
    计算工龄(mssql标量值函数)
    YZMCMS发布问题以及解决方法
    用输出的方式向页面和js增加引入
  • 原文地址:https://www.cnblogs.com/jeecg158/p/15731417.html
Copyright © 2011-2022 走看看