Implementation steps of integrating mongodb with springboot

Time:2022-3-19

1、 Technical introduction

1. What is mongodb?

Mongodb (from the English word “humongous”, which means “huge” in Chinese) is an open source database that can be applied to enterprises of all sizes, industries and applications. As a database suitable for agile development, mongodb’s data schema can be updated flexibly with the development of applications. At the same time, it also provides developers with the functions of traditional database: secondary index, complete query system, strict consistency and so on. Mongodb can make enterprises more agile and scalable. Enterprises of all sizes can use mongodb to create new applications, improve work efficiency with customers, speed up product launch time and reduce enterprise costs. Mongodb is a database designed for scalability, high performance and high availability. It can scale from single server deployment to large and complex multi data center architecture. Taking advantage of memory computing, mongodb can provide high-performance data read and write operations. Mongodb’s local replication and automatic failover capabilities give your applications enterprise class reliability and operational flexibility.

2、 Use steps

1. Import Maven Library

The code is as follows (example):


	   <parent>
	        <groupId>org.springframework.boot</groupId>
	        <artifactId>spring-boot-starter-parent</artifactId>
	        <version>2.4.1</version>
	        <relativePath/>
	    </parent>
     <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    </dependencies>

2. Specific use examples

Mongodb encapsulation:

/**
 *Mongo DB assistant
 *
 * @Author: heyuhua
 * @Date: 2021/1/19 9:52
 */
@Component
public class MongoDBHelper {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     *Preserve
     *
     * @param t
     * @param <T>
     * @return
     */
    public <T> T save(T t) {
        return mongoTemplate.save(t);
    }

    /**
     *Preserve
     *
     * @param t
     * @param collectionName
     * @param <T>
     * @return
     */
    public <T> T save(T t, String collectionName) {
        return mongoTemplate.save(t, collectionName);
    }

    /**
     *Inquiry
     *
     * @param query
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> List<T> find(Query query, Class<T> tClass) {
        return mongoTemplate.find(query, tClass);
    }

    /**
     *Inquiry所有
     *
     * @param tClass
     * @param <T>
     * @return
     */
    public <T> List<T> findAll(Class<T> tClass) {
        return mongoTemplate.findAll(tClass);
    }


}

3. Configuration file

The code is as follows (example):

server:
  port: 8088


spring:
  #Mongodb configuration
  data:
    mongodb:
      uri: mongodb://admin:[email protected]:27017/admin

4. Unit test

The test code is as follows (example):


 package com.hyh.core.test;

import com.hyh.core.test.base.HyhTest;
import com.hyh.core.test.po.Person;
import com.hyh.mongodb.helper.MongoDBHelper;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
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 org.springframework.data.mongodb.core.query.TextCriteria;

import java.util.List;

/**
 * MongoDb Test
 *
 * @Author: heyuhua
 * @Date: 2021/1/19 10:28
 */
public class MongoDBTest extends HyhTest {

    @Autowired
    private MongoDBHelper mongoDBHelper;


    @Test
    public void testSave() {
        Person person = new Person();
        person.setName("heyuhua");
        person.setAge(25);
        mongoDBHelper.save(person);
        Person person2 = new Person();
        person2.setName("hyh");
        person2.setAge(52);
        mongoDBHelper.save(person2);
    }

    @Test
    public void testSaveCollection() {
        Person person = new Person();
        person.setName("heyuhua");
        person.setAge(25);
        mongoDBHelper.save(person, "personCollection");
        Person person2 = new Person();
        person2.setName("hyh");
        person2.setAge(52);
        mongoDBHelper.save(person2, "personCollection");
    }

    @Test
    public void testFindAll() {
        List<Person> list = mongoDBHelper.findAll(Person.class);
        for (Person person : list) {
            System.out.println("name=" + person.getName() + ",age=" + person.getAge());
        }
    }

    @Test
    public void testFind() {
        Criteria criteria = new Criteria();
        criteria.and("age").gte("25");
        Query query = new Query(criteria);
        List<Person> list = mongoDBHelper.find(query,Person.class);
        for (Person person : list) {
            System.out.println("name=" + person.getName() + ",age=" + person.getAge());
        }
    }

    @Test
    @Override
    public void test() {
        System.out.println("---MongoDb Test---");
    }


}


summary

Does it feel simple? For more usage, please click below to view the source code. Follow me to reveal more advanced usage

Source address:Click here to view the source code

The above is the details of the implementation steps of springboot integrating mongodb. For more information about springboot integrating mongodb, please pay attention to other relevant articles of developeppaer!