具体错误如下: Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testDao' is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:694) at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:956) at test.Test.main(Test.java:15) 我的代码: 1.Dao层的实现类如下:
@Repository("testDao")
public class TestDaoImpl implements TestDao {
@Autowired
//使用配置文件中的JDBC模板
private JdbcTemplate JdbcTemplate;
@Override
public int update(String sql, Object[] param) {
// TODO Auto-generated method stub
return JdbcTemplate.update(sql,param);
}
@Override
public List<MyUser> query(String sql, Object[] param) {
RowMapper<MyUser> rowMapper=new BeanPropertyRowMapper<MyUser>(MyUser.class);
return JdbcTemplate.query(sql, rowMapper, param);
}
}
2.applicationContext.xml文件配置如下:
<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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<!-- 指定需要扫描的包,使注解生效 -->
<context:component-scan base-package="com.dao"/>
<!-- 1.配置数据源 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- mysql.数据库驱动 -->
<property name="driverClassName"
value="com.mysql.jdbc.Driver"></property>
<!-- 1.2.连接数据库的url -->
<property name="url"
value="jdbc:mysql://localhost:3306/springtest?characterEncoding=utf8"></property>
<!-- 1.3.连接数据库的用户名 -->
<property name="username" value="root"></property>
<!-- 1.4.连接数据库的密码 -->
<property name="password" value="123456"></property>
</bean>
<!-- 2配置JDBC模板 -->
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 默认必须使用数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
3.测试类代码如下:
public class Test {
public static void main(String[] args) {
ApplicationContext appCon=new ClassPathXmlApplicationContext("applicationContext.xml");
TestDao td= (TestDao)appCon.getBean("testDao");
//从容器中获取增强后的目标对象
String insertSql = "insert into user values(null,?,?)";
//数组param的值与insertSql中的?一一对应
Object param1[]={"张三","男"};
Object param2[]={"李四","女"};
Object param3[]={"王二","男"};
Object param4[]={"麻子","女"};
//添加用户
td.update(insertSql, param1);
td.update(insertSql, param2);
td.update(insertSql, param3);
td.update(insertSql, param4);
//查询用户
String selectSql = "select*from user";
List<MyUser> list = td.query(selectSql, null);
for(MyUser mu:list){
System.out.println(mu);
}
}
}
新手自学,,,网上搜了好多没解决,希望大神指点