@Document annotation in Spring Boot: principle and use

@Document annotation in Spring Boot: principle and use

Introduction

In Spring Boot, the @Document annotation is a very important annotation, which is mainly used to define the metadata information of the MongoDB document object. This article will introduce the principle and use of @Document annotation.

insert image description here

principle

In MongoDB, a document is the most basic storage unit, and each document is a JSON object. The @Document annotation is mainly used to map Java classes to document objects in MongoDB. When using the @Document annotation, you need to specify the metadata information such as the collection name and index corresponding to the document object.

The source code of the @Document annotation is as follows:

@Retention(RetentionPolicy.RUNTIME)
@Target({
    
    ElementType.TYPE})
@Documented
public @interface Document {
    
    
    String collection() default "";
    String language() default "";
    boolean strict() default false;
    String[] indexOptions() default {
    
    };
    Index[] indexes() default {
    
    };
    Collation collation() default @Collation(locale = "");
}

As you can see from the source code of the annotation, the @Document annotation has the following properties:

  • collection: Specify the collection name corresponding to the document object;
  • language: specifies the default language of the document object;
  • Strict: Specifies whether to enable the strict mode of MongoDB;
  • indexOptions: Specify the index options of the document object;
  • indexes: specifies the index of the document object;
  • collation: Specifies the collation of the document object.

use

When using the @Document annotation, you need to follow the following steps:

1. Add MongoDB dependency

Add MongoDB dependencies in the pom.xml file:

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

2. Create a document object

Create a simple document object such as:

@Document(collection = "users")
public class User {
    
    
    @Id
    private String id;
    private String name;
    private int age;
    // getter和setter方法
}

In the document object, the @Document annotation is used to specify that the collection name corresponding to the document object is "users".

3. Define the data access layer

Using the Spring Data MongoDB framework, it is easy to define a data access layer. For example:

@Repository
public interface UserRepository extends MongoRepository<User, String> {
    
    
    List<User> findByName(String name);
}

In the data access layer, the MongoRepository interface provided by the Spring Data MongoDB framework is used, which provides many common data access methods, such as findById, findAll, save, etc. Additionally, data access methods such as findByName can be customized.

4. Configure MongoDB connection information

Configure MongoDB connection information in the application.properties file:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=test

5. Run the application

Just run the Spring Boot application. For example, the application can be run with the following code:

@SpringBootApplication
public class Application {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(Application.class, args);
    }
}

Summarize

This article introduces the principle and use of the @Document annotation in Spring Boot. By using the @Document annotation, Java classes can be mapped to document objects in MongoDB, and the metadata information of document objects can be easily defined. At the same time, it also introduces the method of defining the data access layer using the Spring Data MongoDB framework and the method of configuring the MongoDB connection information.

Guess you like

Origin blog.csdn.net/JasonXu94/article/details/131485063