25.02.2015 Views

Introducing Spring Framework

Introducing Spring Framework

Introducing Spring Framework

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

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

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

import com.apress.isf.java.service.SearchEngine;<br />

@RunWith(<strong>Spring</strong>JUnit4ClassRunner.class)<br />

@ContextConfiguration("classpath:META-INF/spring/mydocuments-jdbc-template-context.xml")<br />

public class MyDocumentsJDBCTemplateTest {<br />

@Autowired<br />

private SearchEngine engine;<br />

private Type webType = new Type("WEB",".url");<br />

@Test<br />

public void testJDBCTemplate() {<br />

List documents = engine.findByType(webType);<br />

assertNotNull(documents);<br />

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

assertEquals(webType.getName(),documents.get(0).getType().getName());<br />

assertEquals(webType.getExtension(),documents.get(0).getType().getExtension());<br />

}<br />

}<br />

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

assertNotNull(documents);<br />

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

Now when you run Listing 9-18 with<br />

gradle :ch09:test<br />

your test will pass without any errors.<br />

Summary<br />

In this chapter, you saw how to read from the database engine and how to combine profiles in order to access different<br />

environments, such as development. You also saw where you can embed a database in memory.<br />

You also looked at how to use a JDBC configuration in different ways, such as pure JDBC with connections and<br />

prepared statements. You also saw how to use the JDBCTemplate to query, insert, and update data. In addition, we<br />

covered how to use the JDBCTemplate to row map the data with the domain classes.<br />

There are even more ways to persist data, which I will cover in the following chapters.<br />

132

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

Saved successfully!

Ooh no, something went wrong!