MongoTemplate is the central support class for Mongo database operations. It provides
@ -54,11 +48,12 @@ Future plans are to support optional logging and/or exception throwing based on
@@ -54,11 +48,12 @@ Future plans are to support optional logging and/or exception throwing based on
### Easy Data Repository generation
To simplify the creation of Data Repositories a generic Repository interface and default implementation is provided. Furthermore, Spring will automatically create a Repository implementation for you that adds implementations of finder methods you specify on an interface.
To simplify the creation of data repositories a generic `Repository` interface and default implementation is provided. Furthermore, Spring will automatically create a Repository implementation for you that adds implementations of finder methods you specify on an interface.
The Repository interface is
public interface Repository<T,IDextendsSerializable> {
```java
public interface Repository<T,IDextendsSerializable> {
T save(T entity);
@ -77,31 +72,36 @@ The Repository interface is
@@ -77,31 +72,36 @@ The Repository interface is
void delete(Iterable<? extends T> entities);
void deleteAll();
}
}
```
The MongoRepository extends Repository and will in future add more Mongo specific methods.
The `MongoRepository` extends `Repository` and will in future add more Mongo specific methods.
public interface MongoRepository<T,IDextendsSerializable> extends
Repository<T,ID> {
}
```java
public interface MongoRepository<T,IDextendsSerializable> extends Repository<T,ID> {
}
```
SimpleMongoRepository is the out of the box implementation of the MongoRepository you can use for basid CRUD operations.
`SimpleMongoRepository` is the out of the box implementation of the `MongoRepository` you can use for basid CRUD operations.
To go beyond basic CRUD, extend the MongoRepository interface and supply your own finder methods that follow simple naming conventions such that they can be easily converted into queries.
To go beyond basic CRUD, extend the `MongoRepository` interface and supply your own finder methods that follow simple naming conventions such that they can be easily converted into queries.
For example, given a Person class with first and last name properties, a PersonRepository interface that can query for Person by last name and when the first name matches a regular expression is shown below
For example, given a `Person` class with first and last name properties, a `PersonRepository` interface that can query for `Person` by last name and when the first name matches a regular expression is shown below
public interface PersonRepository extends MongoRepository<Person,Long> {
```java
public interface PersonRepository extends MongoRepository<Person,Long> {
@ -110,21 +110,19 @@ You can have Spring automatically generate the implemention as shown below
@@ -110,21 +110,19 @@ You can have Spring automatically generate the implemention as shown below
This will register an object in the container named PersonRepository. You can use it as shown below
This will find the repository interface and register a proxy object in the container. You can use it as shown below:
@Service
public class MyService {
``java
@Service
public class MyService {
@Autowired
PersonRepository repository;
private final PersonRepository repository;
public void doWork() {
@ -140,7 +138,8 @@ This will register an object in the container named PersonRepository. You can u
@@ -140,7 +138,8 @@ This will register an object in the container named PersonRepository. You can u