验证dao,总是spring注入不了dao
来源:2-7 验证Dao
慕侠1185987
2019-12-26
<?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">
<!-- 配置整合mybatis过程 -->
<!-- 1.配置数据库相关参数properties的属性:${url} -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.数据库连接池 -->
<bean id="dataSource" class="com.mchang.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="driverClass" value="${jdbc.driver}" />
<property name="maxPoolSize" value="30" />
<property name="minPoolSize" value="10" />
<property name="autoCommitOnClose" value="false"></property>
<property name="checkoutTimeout" value="10000"></property>
<property name="acquireRetryAttempts" value="2"></property>
</bean>
<!-- 3.配置SqlSessionFactory对象 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!-- 扫描entity包 使用别名 -->
<property name="typeAliasesPackage" value="com.imooc.o2o.entity" />
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:AreaDao.xml" />
</bean>
<!-- 4.配置扫描Dao接口包,动态实现Dao接口,注入到spring容器中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
<!-- 给出需要扫描Dao接口包 -->
<property name="basePackage" value="com.imooc.o2o.dao" />
</bean>
</beans>
package com.imooc.o2o.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.imooc.o2o.entity.Area;
public interface AreaDao {
List<Area> queryArea();
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.imooc.o2o.dao.AreaDao">
<select id="queryArea" resultType="com.imooc.o2o.entity.Area">
select area_id,area_name,priority,create_time,last_edit_time from tb_area order by priority asc;
</select>
</mapper>
package com.imooc.o2o.dao;
import static org.junit.Assert.*;
AreaDaoTest 文件
import java.util.List;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.imooc.o2o.BaseTest;
import com.imooc.o2o.entity.Area;
public class AreaDaoTest extends BaseTest {
@Autowired
private AreaDao areaDao;
@Test
public void testQueryArea(){
List<Area> areaList = areaDao.queryArea();
assertEquals(2, areaList.size());
}
}
package com.imooc.o2o;
import org.junit.runner.RunWith;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Candy
*spring 结合junit,junit启动时加载sprintIOC容器
*/
@RunWith(SpringJUnit4ClassRunner.class)
//告诉junit spring配置文件的位置
@ContextConfiguration(locations = {"classpath*:spring-dao.xml"})
public class BaseTest {
}
最后想问下为何还报错
21:00:20.757 [main] ERROR org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@185d8b6] to prepare test instance [com.imooc.o2o.dao.AreaDaoTest@3b084709]
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.imooc.o2o.dao.AreaDaoTest': Unsatisfied dependency expressed through field 'areaDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.imooc.o2o.dao.AreaDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596)
写回答
1回答
-
纯情的慕哥
2019-12-26
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:AreaDao.xml" />你这里的配置可能导致找不到你的xml文件,我按照项目的目录结构和配置为
<!-- 扫描sql配置文件:mapper需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/*.xml"/>你可以参考一下
如果帮到你,望采纳
132019-12-31
相似问题