Introducing Spring Framework
Introducing Spring Framework Introducing Spring Framework
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
- Page 77 and 78: Chapter 7 Testing Your Spring Appli
- Page 79 and 80: Chapter 7 ■ Testing Your Spring A
- Page 81 and 82: Chapter 7 ■ Testing Your Spring A
- Page 83 and 84: Chapter 7 ■ Testing Your Spring A
- Page 85 and 86: Chapter 7 ■ Testing Your Spring A
- Page 87 and 88: Chapter 7 ■ Testing Your Spring A
- Page 89 and 90: Chapter 8 Give Advice to Your Sprin
- Page 91 and 92: Chapter 8 ■ Give Advice to Your S
- Page 93 and 94: Chapter 8 ■ Give Advice to Your S
- Page 95 and 96: Chapter 8 ■ Give Advice to Your S
- Page 97 and 98: Chapter 8 ■ Give Advice to Your S
- Page 99 and 100: Chapter 8 ■ Give Advice to Your S
- Page 101 and 102: Chapter 8 ■ Give Advice to Your S
- Page 103 and 104: Chapter 8 ■ Give Advice to Your S
- Page 105 and 106: Chapter 8 ■ Give Advice to Your S
- Page 107 and 108: Chapter 8 ■ Give Advice to Your S
- Page 109 and 110: Chapter 8 ■ Give Advice to Your S
- Page 111 and 112: Chapter 9 ■ Adding Persistence to
- Page 113 and 114: Chapter 9 ■ Adding Persistence to
- Page 115 and 116: Chapter 9 ■ Adding Persistence to
- Page 117 and 118: Chapter 9 ■ Adding Persistence to
- Page 119 and 120: Chapter 9 ■ Adding Persistence to
- Page 121 and 122: Chapter 9 ■ Adding Persistence to
- Page 123 and 124: Chapter 9 ■ Adding Persistence to
- Page 125 and 126: Chapter 9 ■ Adding Persistence to
- Page 127: Chapter 9 ■ Adding Persistence to
- Page 131 and 132: Chapter 9 ■ Adding Persistence to
- Page 133 and 134: Chapter 10 ■ Showing Your Spring
- Page 135 and 136: Chapter 10 ■ Showing Your Spring
- Page 137 and 138: Chapter 10 ■ Showing Your Spring
- Page 139 and 140: Chapter 10 ■ Showing Your Spring
- Page 141 and 142: Chapter 10 ■ Showing Your Spring
- Page 143 and 144: Chapter 10 ■ Showing Your Spring
- Page 145 and 146: Chapter 11 ■ Integrating Your Spr
- Page 147 and 148: Chapter 11 ■ Integrating Your Spr
- Page 149 and 150: Chapter 11 ■ Integrating Your Spr
- Page 151 and 152: Chapter 11 ■ Integrating Your Spr
- Page 153 and 154: Chapter 11 ■ Integrating Your Spr
- Page 155 and 156: Chapter 11 ■ Integrating Your Spr
- Page 157 and 158: Chapter 11 ■ Integrating Your Spr
- Page 159 and 160: Chapter 11 ■ Integrating Your Spr
- Page 161 and 162: Chapter 11 ■ Integrating Your Spr
- Page 163 and 164: Chapter 11 ■ Integrating Your Spr
- Page 165 and 166: Chapter 11 ■ Integrating Your Spr
- Page 167 and 168: Chapter 12 Exposing a REST API Nowa
- Page 169 and 170: Chapter 12 ■ Exposing a REST API
- Page 171 and 172: Chapter 12 ■ Exposing a REST API
- Page 173 and 174: Chapter 12 ■ Exposing a REST API
- Page 175 and 176: Chapter 12 ■ Exposing a REST API
- Page 177 and 178: Chapter 12 ■ Exposing a REST API
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