applicationContext.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 http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="driverManagerDateSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///gjp"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="driverManagerDataSource"/>
</bean>
</beans>
java中的代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class Test01 {
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void test1() {
// 使用JdbcTemplate来完成操作
jdbcTemplate.execute("update gjp_zhangwu set money=2330 where zwid=4");// execute方法可以执行任意sql
}
}
报错的bug是
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in class path resource [applicationContext.xml]: Cannot resolve reference to bean 'driverManagerDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'driverManagerDataSource' is defined
后来我找了半天都没找到…实在没办法,我就改了bean的id我勒个擦..成功了…
<!--bug-->
<bean id="driverManagerDateSource">
<!--我见过最无语的bug,修改id的值即可...-->