Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Spring Data, MongoDB, and Spring Boot

DZone's Guide to

Spring Data, MongoDB, and Spring Boot

You really can quickly develop a microservice to access MongoDB from Java 8 with little coding. Give it a try... easy like Sunday morning.

· Database Zone ·
Free Resource

Read the 2019 State of Database DevOps Report for the very latest insights

Image title

It’s so simple, I almost don’t believe it.  And, if this seems to hard, try using JHipster.

You really can quickly develop a microservice to access MongoDB from Java 8 with little coding.   Give it a try.  Java 8 + Spring Boot + Gradle + MongoDB + YAML.  Easy like Sunday morning.

Gradle

 compile(“org.springframework.boot:spring-boot-starter-data-mongodb”)

Application.yml

spring.profiles.default: local

Application-local.yml

spring.data.mongodb.database: my-db-engine
spring.data.mongodb.uri: mongodb://localhost:2701

Repository

import org.springframework.data.repository.CrudRepository;
import com.mysite.service.stuff.model.MongoModel;

public interface IMongoModelEnabledRepository extends CrudRepository<MongoModel, String>
{
    public MongoModel findByDomain(String domain);
}

Usage

@Autowired

IMongoModelEnabledRepository myrepo;

myrepo.findByDomain(domain);

Spring Boot Application

@PropertySource(“classpath:application.yml”)
@EnableAsync
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Read the 2019 State of Database DevOps Report for latest insights into DevOps adoption among SQL Server professionals, and the benefits and challenges of including the database in DevOps initiatives

Topics:
spring architecture ,spring data ,nosql ,mongodb ,gradle ,spring boot ,yaml

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}