Springboot集成mongodb简明教程

springboot集成mongodb是非常简单的,只需要引入依赖,增加配置就可以搞定了,软件环境mongodb5和idea工具(引入lombok)

引入依赖

 
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-data-mongodb
        
        
    

增加配置

修改application.yml配置

spring:
 data:
  mongodb:
    host: data101
    port: 27017
    database: user
    authentication-database: admin
    username: lin
    password: elkg1237abc

示例代码

import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;

@Data
@Document("computer")//集合名
public class Computer {
    String name;
    String factory;
    float price;
    String commit;
    String name_url;
}
import lombok.Data;

@Data
public class CountInfo {
    private String factory;
    private int count;
}

测试代码 

package com.example.mongdbproject;

import com.example.mongdbproject.pojo.Computer;
import com.example.mongdbproject.pojo.CountInfo;
import com.mongodb.BasicDBObject;
import org.apache.poi.ss.formula.functions.AggregateFunction;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.CriteriaDefinition;
import org.springframework.data.mongodb.core.query.Query;


import java.util.List;
import java.util.Random;

@SpringBootTest
class MongdbProjectApplicationTests {
    @Autowired
    MongoTemplate mongoTemplate;

    @Test
    void contextLoads() {
        Random random=new Random();
      for(int i=1;i<10000;i++){
          Computer computer=new Computer();
          computer.setCommit("10万+条评价");
          computer.setFactory("航向者组装电脑旗舰店第"+String.valueOf(random.nextInt(50)+"分店"));
          computer.setPrice(969.0f);
          computer.setName_url("https://item.jd.com/11930425326.html");
          computer.setName("航向者 英特尔i5酷睿i7升八核/GTX1060 6G吃鸡台式电脑主机DIY组装整机全套游戏企业办公 电脑主机 套三 intel六核5640丨16G丨4G游戏独显");
          mongoTemplate.insert(computer);
      }

    }
    @Test
    public void getInfo(){

        Query query=new Query();
        query.addCriteria(Criteria.where("factory").is("航向者组装电脑旗舰店第44分店"));
        List iterator=  mongoTemplate.find(query,Computer.class,"computer");
        System.out.println(iterator);





    }
    /**聚合数据
     * */
    @Test
    public void aggInfo(){
    Aggregation aggregation=Aggregation.newAggregation(
            Aggregation.group("factory").count().as("count"),
            Aggregation.project("count").and("factory").previousOperation()
    );
    AggregationResults aggregations=mongoTemplate.aggregate(aggregation, Computer.class,CountInfo.class);

    List countInfos=aggregations.getMappedResults();
    for(CountInfo countInfo:countInfos){
        System.out.println(countInfo);
    }


    }

}

你可能感兴趣的:(後端代碼實踐,mongodb,spring,boot,数据库)