SpringBoot easily integrates the whole process record of MongoDB

  • 2021-08-12 02:46:21
  • OfStack

Preface

MongoDB is a product between relational database and non-relational database, which has the richest functions and is most like relational database.

Tip: The following is the main content of this article, and the following cases can be used for reference

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 for agile development, MongoDB's data schema can be flexibly updated as applications evolve. At the same time, it also provides developers with the functions of traditional databases: level 2 index, complete query system, strict 1-character and so on. MongoDB enables enterprises to be more agile and scalable. Enterprises of all sizes can use MongoDB to create new applications, improve work efficiency with customers, speed up product time to market, 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 reading and writing operations. MongoDB's local replication and automatic failover capabilities give your applications enterprise-class reliability and operational flexibility.

2. Use steps

1. Introducing 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 usage examples

MongoDB encapsulation:


/**
 * mongo db Assistant 
 *
 * @Author: heyuhua
 * @Date: 2021/1/19 9:52
 */
@Component
public class MongoDBHelper {

 @Autowired
 private MongoTemplate mongoTemplate;

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

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

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

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


}

3. Configuration files

The code is as follows (example):


server:
 port: 8088


spring:
 #mongodb Configure 
 data:
 mongodb:
  uri: mongodb://admin:admin@127.0.0.1:27017/admin


4. Unit testing

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---");
 }


}


Summarize

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

Source code address: Click here to view the source code.


Related articles: