JdbcTemplate错误总结

一、Spring对jdbc操作的简化

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

二、JdbcTemplate的问题

在简化jdbc操作过程中,JdbcTemplate是不可或缺的重要一环。具体问题咱们在代码中解释理解吧。
下面是通过xml配置文件依赖注入:JdbcTemplate对象



    
    
    
    
       
       
       
       
       
	   
	   
	   
    
    
    
       
     
    
    
     
    

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 list = jdbcTemplate.query(sql, new MyResult(),id);
		return (list!=null&&list.size()>0) ? list.get(0) : null;
	}
	
	class MyResult implements RowMapper{
       
		//如何封装一条记录
		@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.(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:85)
	at step10_jdbc.App.(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对象,希望明天可以得到解决,在此做一下记录,如果有相同错误已得到解决的朋友可以指点一下,谢谢啦!

你可能感兴趣的:(框架学习)