Skip to content

ganchix/morphia-spring-boot-starter

Repository files navigation

Spring boot starter Morphia Build Status codecov Maven Central GitHub stars

FOSSA Status

Spring boot starter for use Morphia in a Spring way easily.

Table of Contents

Overview

This implementation offers a Spring way to use Morphia framework, we use the configuration of Spring Data Mongo to instantiate Morphia and Datastore objects, both objects can be injected.

The starter scan Entity annotations to add in datastore and ensure indexes.

Getting started

Code example

First of all need to configure your properties like Spring Data Mongo, a example of application.properties :

spring.data.mongodb.uri=mongodb://user:password@ip:port/database
spring.data.mongodb.repositories.enabled=true

Create your domain classes, for example:

import lombok.Data;
import org.bson.types.ObjectId;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Id;

@Data
@Entity
public class Account {
    
    @Id
    private ObjectId id;
    private String user;
    private Double amount;
    private Long createdDate;
    
}

Create your repositories:

import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
    
@Component
public class AccountRepositoryImpl implements AccountRepository {
    
    @Autowired
    private Datastore datastore;
    
    @Override
    public void delete(String user) {
        Query<Account> removeQuery = datastore.createQuery(Account.class)
                    .filter("user", user);
        datastore.delete(removeQuery);
    }
}

Integration using @SpringBootApplication or @EnableAutoConfiguration

Only add Maven dependency:

<dependency>
    <groupId>io.github.ganchix</groupId>
    <artifactId>morphia-spring-boot-starter</artifactId>
    <version>1.0.1</version>
</dependency>

Without @SpringBootApplication or @EnableAutoConfiguration

You need a @ComponentScan in the root package.

Add Maven dependency:

<dependency>
    <groupId>io.github.ganchix</groupId>
    <artifactId>morphia-spring-boot</artifactId>
    <version>1.0.1</version>
</dependency>

Import auto configuration MorphiaAutoConfiguration:

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
    
@Configuration
@Import(MorphiaAutoConfiguration.class)
public class MorphiaConfig {
    
}

License

Spring boot starter Morphia is licensed under the MIT License. See LICENSE for details.

Copyright (c) 2017 Rafael Ríos Moya

FOSSA Status