Introducing Spring Framework

Introducing Spring Framework Introducing Spring Framework

25.02.2015 Views

Chapter 9 ■ Adding Persistence to Your Spring Application } } documents = engine.listAll(); assertNotNull(documents); assertTrue(documents.size() == 4); That’s it! As you can see, the unit test (see Listing 9-14) didn’t change at all, and if you run it, the test should pass without any problems. A New Way to Collect Data: JdbcTemplate and RowMapper Reviewing your DocumentDAO implementation (see Listing 9-13), you have a lot of code still. The good news is that Spring Framework provides a JdbcTemplate that can help you to execute any query and map the document class by implementing a RowMapper class, but how you can accomplish this? How can you reduce all this code? Well, let’s look at Listing 9-15; it shows you how to use the JdbcTemplate. Listing 9-15. DocumentJdbcTemplateRepository.java package com.apress.isf.spring.jdbc; import java.util.List; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate; import com.apress.isf.java.model.Document; import com.apress.isf.spring.data.DocumentDAO; public class DocumentJdbcTemplateRepository implements DocumentDAO { } private JdbcTemplate jdbcTemplate; private DataSource dataSource; private String query; public void setDataSource(DataSource dataSource) { this.dataSource = dataSource; this.jdbcTemplate = new JdbcTemplate(this.dataSource); } public void setQuery(String query){ this.query = query; } public List getAll() { return jdbcTemplate.query(query, new DocumentRowMapper()); } 129

Chapter 9 ■ Adding Persistence to Your Spring Application Listing 9-15 shows how you can reduce the amount of code by using the JdbcTemplate class, where you can pass the query and the row mapper. The JdbcTemplate implements a Template Design pattern that does the heavy work of calling the JDBC directly, and therefore provides easy methods for executing SQL statements. Next, let’s create the DocumentRowMapper class as shown in Listing 9-16. Listing 9-16. DocumentRowMapper package com.apress.isf.spring.jdbc; import java.sql.ResultSet; import java.sql.SQLException; import org.springframework.jdbc.core.RowMapper; import com.apress.isf.java.model.Document; import com.apress.isf.java.model.Type; public class DocumentRowMapper implements RowMapper { } public Document mapRow(ResultSet rs, int rowNum) throws SQLException { Document document = new Document(); document.setDocumentId(rs.getString("documentId")); document.setName(rs.getString("name")); document.setLocation(rs.getString("location")); document.setCreated(rs.getDate("created")); document.setModified(rs.getDate("modified")); document.setDescription("doc_desc"); Type type = new Type(); type.setTypeId(rs.getString("typeId")); type.setName(rs.getString("type_name")); type.setDesc(rs.getString("type_desc")); type.setExtension(rs.getString("extension")); document.setType(type); return document; } Listing 9-16 shows how to implement the RowMapper interface. You can see that the Spring Framework will iterate every row and create the result list you need: the Document model. If you take this approach, you will have more readable code. Next, let’s create the XML configuration that will contain the new reference to the DocumentJdbcTemplateRepository class as shown in Listing 9-17. Listing 9-17. mydocuments-jdb-template-context.xml

Chapter 9 ■ Adding Persistence to Your <strong>Spring</strong> Application<br />

}<br />

}<br />

documents = engine.listAll();<br />

assertNotNull(documents);<br />

assertTrue(documents.size() == 4);<br />

That’s it! As you can see, the unit test (see Listing 9-14) didn’t change at all, and if you run it, the test should pass<br />

without any problems.<br />

A New Way to Collect Data: JdbcTemplate and RowMapper<br />

Reviewing your DocumentDAO implementation (see Listing 9-13), you have a lot of code still. The good news is that<br />

<strong>Spring</strong> <strong>Framework</strong> provides a JdbcTemplate that can help you to execute any query and map the document class by<br />

implementing a RowMapper class, but how you can accomplish this? How can you reduce all this code? Well, let’s look<br />

at Listing 9-15; it shows you how to use the JdbcTemplate.<br />

Listing 9-15. DocumentJdbcTemplateRepository.java<br />

package com.apress.isf.spring.jdbc;<br />

import java.util.List;<br />

import javax.sql.DataSource;<br />

import org.springframework.jdbc.core.JdbcTemplate;<br />

import com.apress.isf.java.model.Document;<br />

import com.apress.isf.spring.data.DocumentDAO;<br />

public class DocumentJdbcTemplateRepository implements DocumentDAO {<br />

}<br />

private JdbcTemplate jdbcTemplate;<br />

private DataSource dataSource;<br />

private String query;<br />

public void setDataSource(DataSource dataSource) {<br />

this.dataSource = dataSource;<br />

this.jdbcTemplate = new JdbcTemplate(this.dataSource);<br />

}<br />

public void setQuery(String query){<br />

this.query = query;<br />

}<br />

public List getAll() {<br />

return jdbcTemplate.query(query, new DocumentRowMapper());<br />

}<br />

129

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

Saved successfully!

Ooh no, something went wrong!