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 18 ■ <strong>Spring</strong> and Groovy<br />

Listing 18-3. mydocumentsJDBC.groovy<br />

beans{<br />

xmlns jdbc:"http://www.springframework.org/schema/jdbc"<br />

xmlns util:"http://www.springframework.org/schema/util"<br />

jdbc.'embedded-database' (id:"dataSource"){<br />

jdbc.script(location:"classpath:/META-INF/data/schema.sql")<br />

jdbc.script(location:"classpath:/META-INF/data/data.sql")<br />

}<br />

util.map(id:"sql"){<br />

entry(key:"query", value:<br />

"""<br />

select d.documentId, d.name, d.location, d.description as doc_desc,<br />

d.typeId, d.created, d.modified,<br />

t.name as type_name, t.description as type_desc, t.extension from<br />

documents d<br />

join types t<br />

on d.typeId = t.typeId<br />

""")<br />

entry(key:"find", value:"""<br />

select d.documentId, d.name, d.location, d.description as doc_desc,<br />

d.typeId, d.created, d.modified,<br />

t.name as type_name, t.description as type_desc, t.extension from<br />

documents d<br />

join types t<br />

on d.typeId = t.typeId<br />

where d.documentId = ?<br />

""")<br />

}<br />

}<br />

Listing 18-3 shows the use of the xmlns jdbc and xmlns util namespaces, equivalent to the declarations at the<br />

beginning of the XML counterpart. Then, it shows the use of the jdbc. 'embedded-database'. It’s a common rule of<br />

the DSL syntax that if a name has a hyphen (-), it must be enclosed in single quotes. Then it shows the id between<br />

parentheses. In the <strong>Spring</strong> DSL, everything that is between parentheses is treat as a Map (key:value) syntax. Next, it<br />

shows the util.map where you define all the SQL statements.<br />

Next, let’s review the Mongo version of the XML. See Listing 18-4 for the mydocumentMongo.groovy file.<br />

Listing 18-4. mydocumentsMongo.groovy<br />

import org.springframework.data.mongodb.core.MongoTemplate as MT<br />

beans{<br />

xmlns mongo:"http://www.springframework.org/schema/data/mongo"<br />

}<br />

mongo.'mongo'(id:"mongo",host:"127.0.0.1",port:27017)<br />

mongo.'db-factory'(id:"mongoDbFactory",dbname:"mydocuments")<br />

mongoTemplate(MT,ref("mongoDbFactory"))<br />

255

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

Saved successfully!

Ooh no, something went wrong!