springboot集成mybatis时提示有多个Mapper(dao)实例

古甲哈醒 2017-05-22 11:19:27
我在使用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 singleton

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed



springboot\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/*.xml

2、CityDao.java

public 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 负责启动springboot

package 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);
}
}


我检查了半天也没找出原因,请大神们指教,谢谢!
...全文
3240 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
你看咩啊 2018-08-01
  • 打赏
  • 举报
回复
返回的是集合,你用单个对象接收的,换成List<City>
herriman 2018-07-30
  • 打赏
  • 举报
回复
MD,前面不小心用快捷键提交了。

肯定出现了多个接口类型的实例,可以用MAP接收找找原因
@Autowired
private Map<String,CityDao> CityDaoMap;
再打出来这个MAP的KEY,就知道是什么名字的对象了。
herriman 2018-07-30
  • 打赏
  • 举报
回复
肯定出现了多个接口类型的实例,可以用LIST接收
Cathy313 2017-11-03
  • 打赏
  • 举报
回复
required a single bean, but 4 were found:CityDao需要单个bean但是发现了4个 建议:Consider marking 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看看能不能解决
owhen 2017-11-02
  • 打赏
  • 举报
回复
CityDao是不是要和mapper同名? 这个问题这么久了,请教一下你
sinat_21706277 2017-05-24
  • 打赏
  • 举报
回复
跟你遇到了一样的问题。。。
一剑侵心 2017-05-23
  • 打赏
  • 举报
回复
个人感觉应该是CityDao定义了多份的问题。
北九道 2017-05-23
  • 打赏
  • 举报
回复
Consider marking 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处理

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧