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:
* Download the jar through Maven: * Download the jar through Maven:
<dependency> ```xml
<groupId>org.springframework.data</groupId> <dependency>
<artifactId>spring-data-mongodb</artifactId> <groupId>org.springframework.data</groupId>
<version>1.2.0.BUILD-SNAPSHOT</version> <artifactId>spring-data-mongodb</artifactId>
</dependency> <version>1.2.1.RELEASE</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>
### MongoTemplate ### MongoTemplate
MongoTemplate is the central support class for Mongo database operations. It provides 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
### Easy Data Repository generation ### 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 The Repository interface is
public interface Repository<T, ID extends Serializable> { ```java
public interface Repository<T, ID extends Serializable> {
T save(T entity);
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 The `MongoRepository` extends `Repository` and will in future add more Mongo specific methods.
Repository<T, ID> {
}
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"> You can have Spring automatically create a proxy for the interface as shown below:
<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>
<bean class="org.springframework.data.document.mongodb.repository.MongoRepositoryFactoryBean"> ```xml
<property name="template" ref="template" /> <bean id="template" class="org.springframework.data.document.mongodb.MongoTemplate">
<property name="repositoryInterface" value="org.springframework.data.document.mongodb.repository.PersonRepository" /> <constructor-arg>
</bean> <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 This will find the repository interface and register a proxy object in the container. You can use it as shown below:
public class MyService {
@Autowired ``java
PersonRepository repository; @Service
public class MyService {
@Autowired
private final PersonRepository repository;
public void doWork() { public void doWork() {
repository.deleteAll(); repository.deleteAll();
Person person = new Person(); Person person = new Person();
person.setFirstname("Oliver"); person.setFirstname("Oliver");
person.setLastname("Gierke"); person.setLastname("Gierke");
person = repository.save(person); 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 Contributing to Spring Data

Loading…
Cancel
Save