• 【spring boot】mybatis启动报错:Consider defining a bean of type 'com.newhope.interview.dao.UserMapper' in your configuration. 【Mapper类不能被找到】@Mapper 和@MapperScan注解的区别


    启动报错:

    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上进行配置

  • 相关阅读:
    JSONP的学习(收集整理)
    10个必备的移动UI设计资源站(转)
    iscroll4框架解析[webapp开发](转)
    IE9中Media queries在iframe无效的解决方法
    mustache模板技术
    企业级的响应式设计(Responsive design at enterprise level)译
    在JSP中使用jQuery的冲突解决(收集整理)
    Java开发 Eclipse使用技巧(转)
    Front End中Javascript兼容问题收集(转)
    vector it->和*it
  • 原文地址:https://www.cnblogs.com/sxdcgaq8080/p/8467965.html
Copyright © 2020-2023  润新知