jeecgboot集成Mongodb

1、引入jar包依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

2、yml配置文件修改

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("存入成功");
    }
 
}

下面对mongodb进行简要介绍

1、概念说明

mongodb存储的是json文件

mongodb中的表不叫表,叫集合,Collections

集合中的数据不叫数据,叫文档,Documents

2、下载安装

db下载地址:https://www.mongodb.com/try/download/community

compass下载地址:https://www.mongodb.com/try/download/compass

3、数据查询常用语法

db.day_air.find({"mp_id":"13053400000265"});

db.hour_air.find({"data_time":ISODate("2022-02-04T00:00:00Z")})

db.day_air.find({"data_time": {$gte: ISODate("2022-02-03 15:57:17")}},{"update_time":1,"_id":0}).sort({"data_time":-1}).limit(1)



db.day_air.find({"values.avg_value":"1.#S"});

mongodb详细用法详见:https://blog.csdn.net/muguli2008/article/details/80591256

 

posted @ 2022-03-02 22:07  JackGIS  阅读(257)  评论(0编辑  收藏  举报