Browse Source

DATAMONGO-658 - Pull forward changes made to README in 1.2.x branch.

pull/33/merge
Oliver Gierke 13 years ago
parent
commit
bd3aac8342
  1. 133
      README.md

133
README.md

@ -29,19 +29,13 @@ For those in a hurry: @@ -29,19 +29,13 @@ For those in a hurry:
* Download the jar through Maven:
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.0.BUILD-SNAPSHOT</version>
</dependency>
<repository>
<id>spring-maven-snapshot</id>
<snapshots><enabled>true</enabled></snapshots>
<name>Springframework Maven SNAPSHOT Repository</name>
<url>http://maven.springframework.org/snapshot</url>
</repository>
```xml
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
```
### MongoTemplate
MongoTemplate is the central support class for Mongo database operations. It provides
@ -54,93 +48,98 @@ Future plans are to support optional logging and/or exception throwing based on @@ -54,93 +48,98 @@ 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, ID extends Serializable> {
T save(T entity);
```java
public interface Repository<T, ID extends Serializable> {
List<T> save(Iterable<? extends T> entities);
T save(T entity);
T findById(ID id);
List<T> save(Iterable<? extends T> entities);
boolean exists(ID id);
T findById(ID id);
List<T> findAll();
boolean exists(ID id);
Long count();
List<T> findAll();
void delete(T entity);
Long count();
void delete(Iterable<? extends T> entities);
void delete(T entity);
void deleteAll();
}
void delete(Iterable<? extends T> entities);
void deleteAll();
}
```
The MongoRepository extends Repository and will in future add more Mongo specific methods.
public interface MongoRepository<T, ID extends Serializable> extends
Repository<T, ID> {
}
The `MongoRepository` extends `Repository` and will in future add more Mongo specific methods.
SimpleMongoRepository is the out of the box implementation of the MongoRepository you can use for basid CRUD operations.
```java
public interface MongoRepository<T, ID extends Serializable> extends Repository<T, ID> {
}
```
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.
`SimpleMongoRepository` is the out of the box implementation of the `MongoRepository` you can use for basid CRUD operations.
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
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.
public interface PersonRepository extends MongoRepository<Person, Long> {
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
List<Person> findByLastname(String lastname);
```java
public interface PersonRepository extends MongoRepository<Person, Long> {
List<Person> findByFirstnameLike(String firstname);
}
List<Person> findByLastname(String lastname);
You can have Spring automatically generate the implemention as shown below
List<Person> findByFirstnameLike(String firstname);
}
```
<bean id="template" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg>
<bean class="com.mongodb.Mongo">
<constructor-arg value="localhost" />
<constructor-arg value="27017" />
</bean>
</constructor-arg>
<constructor-arg value="database" />
<property name="defaultCollectionName" value="springdata" />
</bean>
You can have Spring automatically create a proxy for the interface as shown below:
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean">
<property name="template" ref="template" />
<property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository" />
</bean>
```xml
<bean id="template" class="org.springframework.data.document.mongodb.MongoTemplate">
<constructor-arg>
<bean class="com.mongodb.Mongo">
<constructor-arg value="localhost" />
<constructor-arg value="27017" />
</bean>
</constructor-arg>
<constructor-arg value="database" />
<property name="defaultCollectionName" value="springdata" />
</bean>
This will register an object in the container named PersonRepository. You can use it as shown below
<mongo:repositories base-package="com.acme.repository" />
```
@Service
public class MyService {
This will find the repository interface and register a proxy object in the container. You can use it as shown below:
@Autowired
PersonRepository repository;
``java
@Service
public class MyService {
@Autowired
private final PersonRepository repository;
public void doWork() {
public void doWork() {
repository.deleteAll();
repository.deleteAll();
Person person = new Person();
person.setFirstname("Oliver");
person.setLastname("Gierke");
person = repository.save(person);
Person person = new Person();
person.setFirstname("Oliver");
person.setLastname("Gierke");
person = repository.save(person);
List<Person> lastNameResults = repository.findByLastname("Gierke");
List<Person> lastNameResults = repository.findByLastname("Gierke");
List<Person> firstNameResults = repository.findByFirstnameLike("Oli*");
List<Person> firstNameResults = repository.findByFirstnameLike("Oli*");
}
}
}
}
```
Contributing to Spring Data

Loading…
Cancel
Save