}
public DataSource getDataSource() {
return dataSource;
}
public String getFirstNameById(Long id) {
List<String> result = sfFirstNameById.execute(id);
return result.get(0);
}
}
In Listing 8-53, upon data source injection, an instance of SfFirstNameById is constructed. Then in
the getFirstNameById() method, its execute() method was called, passing in the contact ID. The method
will return a list of Strings, and we need only the first one, because there should be only one record
returned in the resultset.
Listing 8-54 shows the Spring configuration file for connecting to MySQL (app-context-sf.xml).
Listing 8-54. Spring Configuration for MySQL
<?xml version="1.0" encoding="UTF-8"?>
<import resource="datasource-dbcp.xml"/>
<context:component-scan base-package="com.apress.prospring3.ch8.dao.jdbc.annotation"/>
<context:annotation-config/>
</beans>
In Listing 8-54, the file datasource-dbcp.xml is imported, which holds the datasource configuration
to the MySQL database. To run the program, the dependency on commons-dbcp should be added to the
project, as shown in Table 8-5.
Table 8-5. Dependency for commons-dbcp
Group ID
Artifact ID
Version
Description
1.4
Apache commons-dbcp database connection pool library
commons-dbcp
commons-dbcp
Listing 8-55 shows the testing program.
Listing 8-55. Testing Stored Function in MySQL
package com.apress.prospring3.ch8;
import org.springframework.context.support.GenericXmlApplicationContext;
Search WWH :