SpringCloud + SpringBoot + MyBatis整合 无法找到@MapperScan注解

    技术2022-07-14  63

    环境:SpringCloud + SpringBoot + MyBaties

    1)、场景还原2)、解决措施

    1)、场景还原

    1.创建DAO并且给上注解@Mapper

    @Mapper public interface LoginDao { //新增用户方案 public void addUser(PaymentUser paymentUser); //查询用户方案 public PaymentUser selectUserById(@Param("username") String username); }

    2.yml配置

    server: port: 80 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver # 当前数据源操作类型 type: com.alibaba.druid.pool.DruidDataSource # mysql驱动类 url: jdbc:mysql://localhost:3306/pro_ord?useSSL=false&serverTimezone=UTC username: root password: 123456 mybatis: mapper-locations: classpath*:mapper / *.xml type-aliases-package: fatcats.top.entities

    3.启动报错:

    ... *************************** APPLICATION FAILED TO START *************************** Description: Field loginMapper in fatcats.top.controller.UserLoginController required a bean of type 'fatcats.top.dao.LoginMapper' that could not be found. The injection point has the following annotations: - @org.springframework.beans.factory.annotation.Autowired(required=true) Action: Consider defining a bean of type 'fatcats.top.dao.LoginMapper' in your configuration. Process finished with exit code 1

    4.小编尝试换成在主配置类上使用@MapperScan注解,结果爆红没找到

    2)、解决措施

    小编查询了很多原因,发现原来是坐标版本在作怪

    <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> </dependency>

    因为小编在父pom文件导入了mybatis starter

    <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>${mybatis.spring.boot.version}</version> </dependency>

    所以将上面的坐标版本改成1.3版本一下(父pom没导入starter) 删除即可(父pom导入了)

    Processed: 0.017, SQL: 9