img
. . . . . . . . . .
}
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"?>
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<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 :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home