Spring中基于JdbcDaoSupport运用SimpleJdbcDaoSupport
Spring中基于JdbcDaoSupport使用SimpleJdbcDaoSupport
学习在Spring中使用数据库的时候发现Spring对JDBC的Dao有个支持类JdbcDaoSupport,通过继承这个类可以减少JdbcTemplate的代码量,于是就照着《Spring In Action 2》中的没想到出现了下列异常
自己的配置文件是
通过异常很清楚的找到了出问题的配置部分
于是接着查看spring官方API发现org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport继承了 JdbcDaoSupport,同时新增了SimpleJdbcTemplate getSimpleJdbcTemplate()和protected void initTemplateConfig()两个方法,并没有setSimpleJdbcTemplate()方法,没有这个方法当然不能进行注入了,所以会出现simpleJdbcTemplate不能设置的错误。但是我们发现了initTemplateConfig()这个方法,通过查阅API发现这个方法是通过配置的的JdbcTemplate创建一个新的SimpleJdbcTemplate。所以只要把原来出错的那段配置文件改为
附TestImpl代码
学习在Spring中使用数据库的时候发现Spring对JDBC的Dao有个支持类JdbcDaoSupport,通过继承这个类可以减少JdbcTemplate的代码量,于是就照着《Spring In Action 2》中的没想到出现了下列异常
Failed to convert property value of type org.springframework.jdbc.core.simple.SimpleJdbcTem plate] to required type [org.springframework.jdbc.core.JdbcTemplate] for property "jdbcTemplate';
自己的配置文件是
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="jdbc.properties"/> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="autoCommitOnClose" value="true"/> <property name="checkoutTimeout" value="${cpool.checkoutTimeout}"/> <property name="initialPoolSize" value="${initialPoolSize}"/> <property name="minPoolSize" value="${cpool.minPoolSize}"/> <property name="maxPoolSize" value="${cpool.maxPoolSize}"/> <property name="maxIdleTime" value="${cpool.maxIdleTime}"/> <property name="acquireIncrement" value="${cpool.acquireIncrement}"/> <property name="maxIdleTimeExcessConnections" value="${cpool.maxIdleTimeExcessConnections}"/> </bean> <bean id="simpleJdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate"> <constructor-arg><ref bean="dataSource"/></constructor-arg> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <constructor-arg><ref bean="dataSource"/></constructor-arg> </bean> <bean id="testImpl" class="spring.TestImpl"> <property name="jdbcTemplate" ref="simpleJdbcTemplate"/> </bean> </beans>
通过异常很清楚的找到了出问题的配置部分
<bean id="testImpl" class="spring.TestImpl"> <property name="jdbcTemplate" ref="simpleJdbcTemplate"/> </bean>于是就试着把这段改为
<bean id="testImpl" class="spring.TestImpl"> <property name="simpleJdbcTemplate" ref="simpleJdbcTemplate"/> </bean>没想到出现了simpleJdbcTemplate不能设置的错误。
于是接着查看spring官方API发现org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport继承了 JdbcDaoSupport,同时新增了SimpleJdbcTemplate getSimpleJdbcTemplate()和protected void initTemplateConfig()两个方法,并没有setSimpleJdbcTemplate()方法,没有这个方法当然不能进行注入了,所以会出现simpleJdbcTemplate不能设置的错误。但是我们发现了initTemplateConfig()这个方法,通过查阅API发现这个方法是通过配置的的JdbcTemplate创建一个新的SimpleJdbcTemplate。所以只要把原来出错的那段配置文件改为
<bean id="testImpl" class="spring.TestImpl"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean>就解决问题了。经过测试,问题解决。
附TestImpl代码
package spring; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.simple.ParameterizedRowMapper; import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport; public class TestImpl extends SimpleJdbcDaoSupport implements TestDao { public TestImpl() { } private static final String SEARCH_BY_ID = "select * from test where id=?"; @Override public Test searchById(int id) { List<Test> matches = this.getSimpleJdbcTemplate().query(SEARCH_BY_ID, new ParameterizedRowMapper<Test>(){ @Override public Test mapRow(ResultSet rs, int rowNum) throws SQLException { Test test= new Test(); test.setId(rs.getInt(1)); test.setMsg(rs.getString(2)); return test; } },id); return matches.size() > 0 ? matches.get(0) : null; } }