25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15 ■ <strong>Spring</strong> Data Within Your <strong>Spring</strong> Application<br />

@Override<br />

public List getAll() {<br />

return mongoTemplate.findAll(Type.class);<br />

}<br />

@Override<br />

public Type findById(String id) {<br />

Query query = query(where("typeId").is(id));<br />

return mongoTemplate.findOne(query, Type.class);<br />

}<br />

@Override<br />

public Type save(Type type) {<br />

mongoTemplate.insert(type);<br />

return type;<br />

}<br />

}<br />

Listings 15-3 and Listing 15-4 show your implementation of the DocumentDAO and TypeDAO interfaces. As you<br />

can see, you are using the MongoOperations class. This class is a template that provides functionality to operate with<br />

MongoDB. The <strong>Spring</strong> Data-Mongo provides a set of templates and annotations that help to talk with a MongoDB<br />

server, making it easy to interact with it.<br />

Here you don’t need to map any class or go row by row. The document will be saved, as is, in a Mongo format,<br />

something similar to JSON. Next, let’s see the XML configuration, shown in Listing 15-5, and how to interact with the<br />

MongoDB server.<br />

Listing 15-5. mydocuments-context.xml<br />

<br />

<br />

<br />

<br />

<br />

<br />

209

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!