JdbcTemplate错误总结

一、Spring对jdbc操作的简化

1、连接管理
2、jdbc操作重复代码封装

二、JdbcTemplate的问题

在简化jdbc操作过程中,JdbcTemplate是不可或缺的重要一环。具体问题咱们在代码中解释理解吧。
下面是通过xml配置文件依赖注入: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"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    
    
    <!-- 设置c3p0连接对象 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
       <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/study_of_mypool"></property>
       <property name="user" value="root"></property>
       <property name="password" value="root"></property>
       <property name="initialPoolSize" value="3"></property>
	   <property name="maxPoolSize" value="10"></property>
	   <property name="maxStatements" value="100"></property>
	   <property name="acquireIncrement" value="2"></property>
    </bean>
    <!-- c创建jdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
       <property name="dataSource" ref="dataSource"></property>
    </bean> 
    
    <!-- **userDao部分** -->
     <!-- <bean id="userDao" class="step10_jdbc.UserDao">
       <property name="jdbcTemplate" value="jdbcTemplate"></property>
    </bean>  -->
    
</beans>

UserDao实体类部分:


public class UserDao {
	/**
	 * spring 可以做到对jdbc操作的简化:
	 *  1、连接管理 
	 * 2、jdbc操作重复代码封装
	 */
	// 注入IOC容器
	public JdbcTemplate jdbcTemplate;
	public void setjdbcTemplate(JdbcTemplate jdbcTemplate) {
	
		this.jdbcTemplate = jdbcTemplate;
	}
	//使用JdbcTemplate组件
	public void save() {
		
			String sql = "INSERT INTO detp(deptName) VALUES('test');";
			
			jdbcTemplate.update(sql);
			
			
	}
	/**
	 * 2、jdbc操作重复代码封装
	 */
	public Detp findById(int id) {
		String sql = "select * from detp where deptId=?";
		List<Detp> list = jdbcTemplate.query(sql, new MyResult(),id);
		return (list!=null&&list.size()>0) ? list.get(0) : null;
	}
	
	class MyResult implements RowMapper<Detp>{
       
		//如何封装一条记录
		@Override
		public Detp mapRow(ResultSet arg0, int arg1) throws SQLException {
			Detp detp = new Detp();
			detp.setDeptId(arg0.getInt("deptId"));
			detp.setDeptName(arg0.getString("deptName"));
			return detp;
		}
	}
}

主程序调用:

public class App {
	
	//获取IOC容器对象
	ApplicationContext ac = new ClassPathXmlApplicationContext("step10_jdbc/application.xml");
	
	@Test
	public void testjdbc()
	{
//		UserDao userDao =  (UserDao) ac.getBean("userDao");

		JdbcTemplate jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTemplate");
		UserDao userDao = new UserDao();
		userDao.setjdbcTemplate(jdbcTemplate);
		userDao.save();
		
//		System.out.println(userDao.findById(2));
	}
}

当采用userDao依赖注入,在主程序通过**ac.getBean(“jdbcTemplate”);**调用时会出现以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in class path resource [step10_jdbc/application.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.jdbc.core.JdbcTemplate' for property 'jdbcTemplate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.jdbc.core.JdbcTemplate' for property 'jdbcTemplate': no matching editors or conversion strategy found
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:584)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at step10_jdbc.App.<init>(App.java:11)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
	at java.lang.reflect.Constructor.newInstance(Unknown Source)
	at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
	at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
Caused by: org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.jdbc.core.JdbcTemplate' for property 'jdbcTemplate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.jdbc.core.JdbcTemplate' for property 'jdbcTemplate': no matching editors or conversion strategy found
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:590)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertForProperty(AbstractNestablePropertyAccessor.java:604)
	at org.springframework.beans.BeanWrapperImpl.convertForProperty(BeanWrapperImpl.java:219)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.convertForProperty(AbstractAutowireCapableBeanFactory.java:1697)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1653)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1400)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575)
	... 33 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'org.springframework.jdbc.core.JdbcTemplate' for property 'jdbcTemplate': no matching editors or conversion strategy found
	at org.springframework.beans.TypeConverterDelegate.convertIfNecessary(TypeConverterDelegate.java:299)
	at org.springframework.beans.AbstractNestablePropertyAccessor.convertIfNecessary(AbstractNestablePropertyAccessor.java:585)
	... 39 more


在一直未能解决的情况下,我将UserDao对象未注入IOC容器,而是直接创建对象,通过**ac.getBean(“jdbcTemplate”)**来获取JdbcTemplate,并将其传入UserDao对象,然后调用save方法,这样可以顺利通过。

花费几个小时时间也不能理解为什么不能注入UserDao对象,希望明天可以得到解决,在此做一下记录,如果有相同错误已得到解决的朋友可以指点一下,谢谢啦!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值