Java Reference
In-Depth Information
8. import org.springframework.dao.DataAccessException;
9. import org.springframework.jdbc.core.ResultSetExtractor;
10.
11. import com.apress.books.model.Author;
12. import com.apress.books.model.Book;
13.
14. public class BookExtractor implements ResultSetExtractor<Book> {
15.
16. public Book extractData(ResultSet resultSet) throws SQLException,
17. DataAccessException {
18.
19. Book book = new Book();
20. Author author = new Author();
21. List<Author> authorList = new ArrayList<>();
22.
23. book.setId(resultSet.getLong(1));
24. book.setCategoryId(resultSet.getLong(2));
25. book.setBookTitle(resultSet.getString(3));
26. book.setPublisherName(resultSet.getString(4));
27. book.setAuthorId(resultSet.getLong(5));
28. author.setBookId(resultSet.getLong(6));
29. author.setFirstName(resultSet.getString(7));
30. author.setLastName(resultSet.getString(8));
31. authorList.add(author);
32. book.setAuthors(authorList);
33.
34. return book;
35. }
36.
37. }
Line 14
:
BookExtractor
implements
ResultSetExtractor
provided by Spring
(under the package
org.springframework.jdbc.core
). The
RowMapper
is suitable
only for mapping to a single domain object. But since we are joining two tables
in Listing 5-26 on line 34, we need to use the
ResultSetExtractor
interface to
transform the data to a nested domain object.
Listing 5-29 illustrates the configuration file for this stand-alone application.
Listing 5-29. Configuration File
1. <?xml version="1.0" encoding="UTF-8"?>
2. <beans xmlns="
http://www.springframework.org/schema/beans
"
3. xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance
"
xmlns:context=
4. xmlns:aop="
http://www.springframework.org/schema/aop
"
5. xsi:schemaLocation="
http://www.springframework.org/schema/beans
11.