Personal learning series – spring boot integrates mongodb to add, delete, modify and query data

Time:2021-5-14

Mongodb is a product between relational database and non relational database, which has the most abundant functions and is most like relational database.

Mongodb installation

Because I’m simply integrating, I’m installing mongodb into windows.

1. Official websiteDownload installation package

After entering the download page, you can see the download link directly on the right, select the version platform you want to download, and click download.
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data

2. Direct installation

2.1 select the complete installation, and customize the installation at will.

Personal learning series - spring boot integrates mongodb to add, delete, modify and query data

2.2 selection of address and installation mode

Personal learning series - spring boot integrates mongodb to add, delete, modify and query data

2.3 selection of visualization tools

Personal learning series - spring boot integrates mongodb to add, delete, modify and query data

2.4 installation completed

Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data

3. Handwritten code

3.1 connect mongodb with Navicat

Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Nothing is found on the connection. Don’t worry, just roll up the code.

3.2 new spring boot project

I won’t repeat here. If you use idea to create a new project, ha.

3.2.1 pom.xml file
<!--  Web Services -- >
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

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

<!--  Lombok relies on -- >
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
3.2.2 application.yml file
spring:
  data:
    mongodb:
      uri:  mongodb://localhost : 27017 / test # mongodb connection address
3.2.3 user.java entity class
@Data
public class User implements Serializable {

    /**
     *ID primary key
     */
    private Integer id;

    /**
     *User name
     */
    private String name;

    /**
     *Age
     */
    private Integer age;

}
3.2.4 userrepository.java mongodb operation class

Spring data mongodb provides a set of shortcut operation methods for mongodb, creating Dao, inheriting mongorepository, and specifying entity type and primary key type.

public interface UserRepository extends MongoRepository<User, Integer> {

    /**
     *Search by name
     * @param name
     * @return
     */
    User findByName(String name);

    /**
     *Fuzzy query by name
     * @param name
     * @return
     */
    List<User> findByNameLike(String name);

}
3.2.5 control layer of usercontroller.java
@RestController
public class UserController {

    @Resource
    UserRepository userRepository;

    /**
     *New
     * @param user
     * @return
     */
    @PostMapping("/user")
    public User saveUser(@RequestBody User user){
        return userRepository.save(user);
    }

    /**
     *Delete
     * @param id
     */
    @DeleteMapping("/user")
    public void saveUser(Integer id){
        userRepository.deleteById(id);
    }

    /**
     *Modification
     * @param user
     */
    @PutMapping("/user")
    public void updateUser(@RequestBody User user){
        userRepository.save(user);
    }

    /**
     *Match exactly by name
     * @param name
     * @return
     */
    @GetMapping("/userByName")
    public User userByName(String name){
        return userRepository.findByName(name);
    }

    /**
     *Fuzzy query by name
     * @param name
     * @return
     */
    @GetMapping("/userList")
    public List<User> userList(String name){
        return userRepository.findByNameLike(name);
    }

}
3.2.6 operation test

Calling interface with postman for testing
newly added:
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Delete:
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Revision:
Let’s add and then modify
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Query:
Now the database has only one piece of data named modify, so query
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
No data was found in the query named repair
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Fuzzy query
Personal learning series - spring boot integrates mongodb to add, delete, modify and query data
Other complex query operations later to study ha!

Personal blog address:

http://www.zhouzhaodong.xyz

Source code address:

https://github.com/zhouzhaodo…