Setting up a Spring Boot application with MongoDB🍃

Spring Boot offers glorious help for functions accessing MongoDB as a database.

The next preparations are essential to create our personal utility utilizing Spring Information MongoDB.

Configuration

As a document-oriented database, MongoDB is not going to be related to our utility utilizing Hibernate as an ORM layer. As a substitute, we add spring-boot-starter-data-mongodb as a dependency to our app, offering an analogous method to accessing MongoDB utilizing POJOs and repositories.

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

Exit fullscreen mode

Required Maven dependency

With this dependency in place, we are able to lengthen our utility.ymlor utility.properties with a URI. If the database doesn’t exist but, MongoDB will create it mechanically. With setting auto-index-creation to true, distinctive indexes are added throughout the startup of our app if they’re annotated in our paperwork with @Listed(distinctive = true).

spring.information.mongodb.uri=mongodb://localhost:27017/mongo
spring.information.mongodb.auto-index-creation=true
Enter fullscreen mode

Exit fullscreen mode

Solely the URI is required for a profitable database connection

In our configuration class,@EnableMongoRepositories must be set. This can seek for courses within the specified bundle that reach MongoRepository – an instance of this will likely be given in a second. By offering that ValidatingMongoEventListener all paperwork are validated earlier than persisting, we are able to lengthen their fields with javax.validation constraints like @NotNull.

@Configuration
@EnableMongoRepositories("io.bootify.mongo.repos")
public class MongoConfig {

    @Bagya
    public ValidatingMongoEventListener validatingMongoEventListener(
            remaining LocalValidatorFactoryBean manufacturing facility) {
        return new ValidatingMongoEventListener(manufacturing facility);
    }

}
Enter fullscreen mode

Exit fullscreen mode

The primary model of our config

Moreover, we need to allow transaction help for our utility. This permits us, for instance, to mark the strategies of our companies as @Transactional. Please observe that this requires our MongoDB occasion to be initialized as a reproduction set to help this function.

@Bagya
public MongoTransactionManager transactionManager(remaining MongoDatabaseFactory databaseFactory) {
    return new MongoTransactionManager(databaseFactory);
}
Enter fullscreen mode

Exit fullscreen mode

Including transaction help to our config

Instance Doc and Repository

With these preparations, we are able to already outline a easy doc and its related repository. For our instance, we use a major key of sort String, which is mechanically generated by MongoDB as ObjectID if we don’t present it.

@Doc
public class Buyer {

    @Id
    personal String id;

    @Measurement(max = 255)
    personal String firstName;

    @Measurement(max = 255)
    personal String lastName;

    @Listed(distinctive = true)
    @NotNull
    @Measurement(max = 255)
    personal String electronic mail;

}
Enter fullscreen mode

Exit fullscreen mode

Our first POJO mapped to a group named “buyer”

We add our repository by extending MongoRepository. It really works in the identical approach as recognized from Spring Information and will be prolonged with customized queries.

public interface CustomerRepository extends MongoRepository<Buyer, String> {

    Buyer findByEmail(String electronic mail);

}
Enter fullscreen mode

Exit fullscreen mode

Primary Repository for MongoDB

With this, all preparations are carried out to attach our Spring Boot utility with MongoDB. In Bootify’s Free plan, we are able to generate the appliance with our customized database schema with out registration. The runnable supply code of our utility is then straight accessible for obtain.

Add a Comment

Your email address will not be published. Required fields are marked *