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

Running out of memory? Learn how Redis Enterprise enables large dataset analysis with the highest throughput and lowest latency while reducing costs over 75%! 

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);
    }
}

Running out of memory? Never run out of memory with Redis Enterprise databaseStart your free trial today.

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 }}