Java Reference
In-Depth Information
Compare this findAllBooks() method of BookDAOImpl with the findAllBooks() method of Chapter 1,
and you will see that JDBCTemplate eliminates the boilerplate code that results from using pure JDBC
to obtain a connection to our data store and cleans up resources.
Line 36 : This creates a JDBCTemplate using the data source passed to it.
Note the JDBCTemplate is instantiated this way for the sake of explanation. In a
production ready application you should inject JDBCTemplate as a dependency
like any other dependency as explained in Listing 5-15.
Line 37 : A row mapper implementation is used. The BookRowMapper is explained next.
Listing 5-27 illustrates the BookRowMapper object to query one or more rows and then transform each
row into the corresponding domain object instead of retrieving a single value.
Listing 5-27. The BookMapper Object
1. package com.apress.books.dao;
2.
3. import java.sql.ResultSet;
4. import java.sql.SQLException;
5.
6. import org.springframework.jdbc.core.RowMapper;
7.
8. import com.apress.books.model.Book;
9.
10. public class BookRowMapper implements RowMapper<Book> {
11.
12. @Override
13. public Book mapRow(ResultSet resultSet, int line) throws SQLException {
14. BookExtractor bookExtractor = new BookExtractor();
15. return bookExtractor.extractData(resultSet);
16. }
17.
18. }
Line 10 : Spring's RowMapper<T> interface (under the package
org.springframework.jdbc.core ) provides a simple way for you to perform
mapping from a JDBC resultset to POJOs.
Line 14 : This uses BookExtractor to extract the data.
Listing 5-28 illustrates the BookExtractor object.
Listing 5-28. BookExtractor Object
1. package com.apress.books.dao;
2.
3. import java.sql.ResultSet;
4. import java.sql.SQLException;
5. import java.util.ArrayList;
6. import java.util.List;
7.
 
Search WWH ::




Custom Search