Home >Java >javaTutorial >What is the method for integrating mongodb with springboot?

What is the method for integrating mongodb with springboot?

WBOY
WBOYforward
2023-05-26 14:50:311409browse

1.Mongodb installation and introduction

1.1 Introduction

As an open source database system, MongoDB is built using C language and uses distributed file storage technology. Under high load conditions, adding more nodes can ensure server performance. The goal of MongoDB is to provide a scalable and high-performance data storage solution to serve web applications. MongoDB uses documents based on key-value pairs to store data structures. MongoDB documents are similar to JSON objects. Field values ​​can contain other documents, arrays, and document arrays.

What is the method for integrating mongodb with springboot?

1.2 Installation

There are many online installation tutorials, so I won’t say more here.

1.3 Configure environment variables and check the installation situation

The configuration here is similar to the jdk configuration, no more details! Check the installation and startup database

What is the method for integrating mongodb with springboot?

2.springboot integrates mongodb

In order to save space, many comments and blank lines have been removed, everyone, please forgive me!

2.1Introduction of maven in pom file

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

2.2properties file configuration

#################mongodb################
#spring.data.mongodb.uri=mongodb://username:password@localhost:27017/test
spring.data.mongodb.uri=mongodb://localhost:27017/springboot

2.3dao layer preparation

package com.lengmo.dao;
 
import com.lengmo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import java.util.List;
 
@Component
public class MongoDbDao {
    @Autowired
    private MongoTemplate mongoTemplate;
    public void save(Student s) {
        mongoTemplate.save(s);
    }
    public Student get(Query query) {
        return mongoTemplate.findOne(query,Student.class);
    }
    public List<Student> findAll() {
        return mongoTemplate.findAll(Student.class);
    }
    public void update(Query query, Update update) {
        mongoTemplate.updateMulti(query,update,Student.class);
    }
    public void delete(Query query) {
        mongoTemplate.remove(query,Student.class);
    }
}

2.4service layer preparation

Here you need to be familiar with some common operating methods of mongodb, just use it more and practice more! ! ! !

package com.lengmo.service;
 
import com.lengmo.dao.MongoDbDao;
import com.lengmo.entity.Hobbies;
import com.lengmo.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import java.util.List;
 
@Service
public class MongoDbService {
    @Autowired
    private MongoDbDao mongoDbDao;
    public void save() {
        Student s=new Student();
        s.setName("lis");
        s.setAge(17);
        s.setSex(1);
        s.setHeight(182);
        Hobbies h=new Hobbies();
        h.setHname("swing");
        s.setHobbies(h);
        mongoDbDao.save(s);
    }
    public Student get() {
       //and查询
        /* Criteria criteriaName=Criteria.where("name").is("lisi");
        Criteria criteriaAage=Criteria.where("age").is(17);
        Criteria andCriteria = new Criteria();
        andCriteria.andOperator(criteriaName,criteriaAage);
        Query query=new Query(andCriteria);*/
        //or查询
        Criteria criteriaName=Criteria.where("name").is("lisi");
        Criteria criteriaAage=Criteria.where("age").gt(16);
        Criteria orCriteria = new Criteria();
        orCriteria.orOperator(criteriaName,criteriaAage);
        Query query=new Query(orCriteria);
 
        return  mongoDbDao.get(query);
    }
    public List<Student> findAll() {
        return mongoDbDao.findAll();
    }
    public void update() {
        Query query=new Query(Criteria.where("name").is("zhangsan"));
        Update update=new Update();
        update.set("age",30);
        update.set("height",188);
        update.set("hobbies.hname","basketball");
        mongoDbDao.update(query,update);
    }
    public void delete() {
        Query query=new Query(Criteria.where("name").is("zhangsan"));
        mongoDbDao.delete(query);
    }
}

2.5 Preparation of the conreoller layer

package com.lengmo.controller;
 
import com.lengmo.entity.Student;
import com.lengmo.service.MongoDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
 
@RestController
@RequestMapping("/mongodb")
public class MongoDbController {
    @Autowired
    private MongoDbService mongoDbService;
    @RequestMapping("/save")
    public void save(){
        mongoDbService.save();
    }
    @RequestMapping("/get")
    public Student get(){
        return mongoDbService.get();
    }
    @RequestMapping("/findAll")
    public List<Student> findAll(){
        return mongoDbService.findAll();
    }
 
    @RequestMapping("/update")
    public void update(){
        mongoDbService.update();
    }
    @RequestMapping("/delete")
    public void delete(){
        mongoDbService.delete();
    }
}

2.6 Preparation of the entity class layer (the two classes are put together, please remember to separate them)

package com.lengmo.entity;
import lombok.Data;
import javax.persistence.Id;
 
@Data
//之所以没有像 mysql那样需要@table标签去标注我们的表名与实体类的对应关系。是因为 mongodb默认类名即与表名对应。
public class Student{
    @Id
    private String id;
    private String name;
    private Integer age;
    private Integer sex;
    private Integer height;
    private Hobbies hobbies;
}
 
 
 
package com.lengmo.entity;
import lombok.Data;
 
@Data
public class Hobbies {
    private  String hname;
}

The above is the detailed content of What is the method for integrating mongodb with springboot?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete