spring bootmybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper&(代

Posted Angel挤一挤

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring bootmybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper&(代相关的知识,希望对你有一定的参考价值。

启动报错:

2018-05-16 17:22:58.161 ERROR 4080 --- Disconnected from the target VM, address: \'127.0.0.1:50529\', transport: \'socket\'
[  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field huaYangAreaMapper in com.sxd.swapping.service.impl.HuaYangServiceImpl required a bean of type \'com.sxd.swapping.dao.mybatis.HuaYangAreaMapper\' that could not be found.


Action:

Consider defining a bean of type \'com.sxd.swapping.dao.mybatis.HuaYangAreaMapper\' in your configuration.

 

解决方案:

根据错误提示

Mybatis的 Mapper类不能被找到,所以需要通过注解标明这个类可以给spring 管理并且给其他类调用的。

 

下面有两种方式提供:

方式1:使用@Mapper注解标注在Mapper类上

@Mapper
public interface HuaYangAreaMapper {

    @Select("SELECT * FROM hua_yang_area where uid = #{uid}")
    @Results({
            @Result(property = "areaName",column = "area_name",javaType = String.class),
            @Result(property = "areaPerson",column = "area_person",javaType = Long.class),
            @Result(property = "createId",column = "create_id",javaType = String.class)
    })
    HuaYangArea findOne(String uid);


}

 

 

方式2:使用@MapperScan("mapper类所在包位置")

@SpringBootApplication
@MapperScan("com.sxd.swapping.dao.mybatis")
public class SwappingApplication {

    public static void main(String[] args) {
        SpringApplication.run(SwappingApplication.class, args);
    }
}

如果有多个包需要被扫描到,可以传入字符串数组

@MapperScan({"com.sxd.swapping.dao.mybatis","com.sxd.swapping.dao.mapper"})

 

 

上面这两种方式都可以使用。

====================================

至于@Mapper 和@MapperScan注解的区别,一个只需要在启动类配置一次,一个是需要在每个mapper上进行配置

 

以上是关于spring bootmybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper&(代的主要内容,如果未能解决你的问题,请参考以下文章

spring boot integrated mybatis three ways!--转

struts2启动报错:ERROR com.opensymphony.xwork2.conversion.impl.DefaultConversionPropertiesProcessor - Co

启动spring boot报错,怎么解决

Spring-boot 启动报错 调试小技巧

spring boot启动报错

spring boot启动报错