我在使用springboot集成mybatis后,项目编译成功,但是在启动Application运行项目时,提示dao(mapper接口)有多个实例。
错误信息如下
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-05-22 10:51:45.858 ERROR 2076 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : ***************************
APPLICATION FAILED TO START
***************************Description:file [E:\Workspaces\springboot-mybatis\target\classes\springboot\dao\CityDao.class] required a single bean, but 4 were found:
- &cityDao: defined in file [E:\Workspaces\springboot-mybatis\target\classes\springboot\dao\CityDao.class]
- systemEnvironment: a programmatically registered singleton - contextParameters: a programmatically registered singleton - contextAttributes: a programmatically registered singletonAction:Consider ing one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumedspringboot\dao\CityDao.class,这个CityDao是我定义的一个Mapepr接口,它被service层引用。
其他配置和代码如下:
1、application.properties## jdbc source
spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver## Mybatis
mybatis.typeAliasesPackage=springboot.entity
mybatis.mapperLocations=classpath\:mapper/*.xml2、CityDao.javapublic interface CityDao {

City get(int id);}3、cityMapper.xml<?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="springboot.dao.CityDao" >
<resultMap type="springboot.entity.City" id="BaseResultMap">
<id column="id" property="id" />
<result column="province_id" property="provinceId" />
<result column="city_name" property="cityName" jdbcType="VARCHAR"/>
<result column="description" property="description" jdbcType="VARCHAR"/>
</resultMap>

<sql id="Base_Column_List" >
id,province_id,city_name,description
</sql>
<select id="get" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from city
where id = #{id}
</select>
</mapper> 4、CityServiceImpl.java
@Service
public class CityServiceImpl implements CityService{

@Autowired
private CityDao cityDao;

public City get(String id){
return cityDao.get(Integer.parseInt(id));
}}5、App.java 负责启动springbootpackage springboot;import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("springboot.dao")
public class App 
{
    public static void main( String[] args )
    {
        SpringApplication.run(App.class, args);
    }
}
我检查了半天也没找出原因,请大神们指教,谢谢!

解决方案 »

  1.   

    Consider ing one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
    感觉是bean的声明上有问题,对bean处理
      

  2.   

    个人感觉应该是CityDao定义了多份的问题。
      

  3.   

    CityDao是不是要和mapper同名?
    这个问题这么久了,请教一下你
      

  4.   

    required a single bean, but 4 were found:CityDao需要单个bean但是发现了4个
    建议:Consider ing one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
    把其中一个bean标记成主键,更新**去接受多bean或者使用@Qualifier  去定义bean你可以参考下http://blog.csdn.net/u014086917/article/details/77870779看看能不能解决
      

  5.   

    肯定出现了多个接口类型的实例,可以用LIST接收
      

  6.   

    MD,前面不小心用快捷键提交了。肯定出现了多个接口类型的实例,可以用MAP接收找找原因
    @Autowired
    private Map<String,CityDao> CityDaoMap;
    再打出来这个MAP的KEY,就知道是什么名字的对象了。
      

  7.   

    返回的是集合,你用单个对象接收的,换成List<City>