int rowCount =this.jdbcTemplate.queryForObject("select count(*) from t_actor",Integer.class);
以下查询使用绑定变量:
int countOfActorsNamedJoe =this.jdbcTemplate.queryForObject("select count(*) from t_actor where first_name = ?",Integer.class,"Joe");
以下查询查找字符串:
String lastName =this.jdbcTemplate.queryForObject("select last_name from t_actor where id = ?",newObject[]{1212L},String.class);
以下查询查找并填充单个域对象:
Actor actor =this.jdbcTemplate.queryForObject("select first_name, last_name from t_actor where id = ?",newObject[]{1212L},newRowMapper<Actor>() {publicActormapRow(ResultSet rs,int rowNum) throwsSQLException {Actor actor =newActor();actor.setFirstName(rs.getString("first_name"));actor.setLastName(rs.getString("last_name"));return actor; } });
以下查询查找并填充许多域对象:
List<Actor> actors =this.jdbcTemplate.query("select first_name, last_name from t_actor",newRowMapper<Actor>() {publicActormapRow(ResultSet rs,int rowNum) throwsSQLException {Actor actor =newActor();actor.setFirstName(rs.getString("first_name"));actor.setLastName(rs.getString("last_name"));return actor; } });
publicclassJdbcCorporateEventDaoimplementsCorporateEventDao {privateJdbcTemplate jdbcTemplate;publicvoidsetDataSource(DataSource dataSource) {this.jdbcTemplate=newJdbcTemplate(dataSource); }// JDBC-backed implementations of the methods on the CorporateEventDao follow...}
@RepositorypublicclassJdbcCorporateEventDaoimplementsCorporateEventDao {privateJdbcTemplate jdbcTemplate; @AutowiredpublicvoidsetDataSource(DataSource dataSource) {this.jdbcTemplate=newJdbcTemplate(dataSource); }// JDBC-backed implementations of the methods on the CorporateEventDao follow...}
下面是相应的XML文件:
<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- Scans within the base package of the application for @Component classes to configure as beans -->
<context:component-scan base-package="org.springframework.docs.test" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<context:property-placeholder location="jdbc.properties"/>
</beans>
// some JDBC-backed DAO class...privateNamedParameterJdbcTemplate namedParameterJdbcTemplate;publicvoidsetDataSource(DataSource dataSource) {this.namedParameterJdbcTemplate=newNamedParameterJdbcTemplate(dataSource);}publicintcountOfActors(Actor exampleActor) {// notice how the named parameters match the properties of the above 'Actor' classString sql ="select count(*) from T_ACTOR where first_name = :firstName and last_name = :lastName";SqlParameterSource namedParameters =newBeanPropertySqlParameterSource(exampleActor);returnthis.namedParameterJdbcTemplate.queryForObject(sql, namedParameters,Integer.class);}
privateJdbcTemplate jdbcTemplate;publicvoidsetDataSource(DataSource dataSource) {// create a JdbcTemplate and set data sourcethis.jdbcTemplate=newJdbcTemplate();this.jdbcTemplate.setDataSource(dataSource);// create a custom translator and set the DataSource for the default translation lookupCustomSQLErrorCodesTranslator tr =newCustomSQLErrorCodesTranslator();tr.setDataSource(dataSource);this.jdbcTemplate.setExceptionTranslator(tr);}publicvoidupdateShippingCharge(long orderId,long pct) {// use the prepared JdbcTemplate for this updatethis.jdbcTemplate.update("update orders"+" set shipping_charge = shipping_charge * ? / 100"+" where id = ?", pct, orderId);}