Spring boot starter for use Morphia in a Spring way easily.
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.
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);
}
}
Only add Maven dependency:
<dependency>
<groupId>io.github.ganchix</groupId>
<artifactId>morphia-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
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 {
}
Spring boot starter Morphia is licensed under the MIT License. See LICENSE for details.
Copyright (c) 2017 Rafael Ríos Moya