CHAPTER 5: Building Java Web Applications with Spring Web MVC 225
- import org.springframework.dao.DataAccessException;
- import org.springframework.jdbc.core.ResultSetExtractor;
- import com.apress.books.model.Author;
- import com.apress.books.model.Book;
- public class BookExtractor implements ResultSetExtractor
{
- public Book extractData(ResultSet resultSet) throws SQLException,
- DataAccessException {
- Book book = new Book();
- Author author = new Author();
- List
authorList = new ArrayList<>();
- book.setId(resultSet.getLong(1));
- book.setCategoryId(resultSet.getLong(2));
- book.setBookTitle(resultSet.getString(3));
- book.setPublisherName(resultSet.getString(4));
- book.setAuthorId(resultSet.getLong(5));
- author.setBookId(resultSet.getLong(6));
- author.setFirstName(resultSet.getString(7));
- author.setLastName(resultSet.getString(8));
- authorList.add(author);
- book.setAuthors(authorList);
- return book;
- }
- }
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
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context=
"http://www.springframework.org/schema/context" - xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">