SpringBoot与MyBatisPlus整合常见‘XXXXMapper‘ that could not be found问题处理方式

22 篇文章 0 订阅
7 篇文章 0 订阅

SpringBoot与MyBatisPlus整合常见’XXXXMapper’ that could not be found问题处理方式

  1. SpringBoot整合Mybaits或MyBatis-Plus后,配置不当会出现Field XXXXMapper in com.xxxx.service.XXXXServiceImpl required a bean of type 'com.xxxx.service.mapper.XXXXMapper' that could not be found.的问题;
  2. 如果出现了类似问题,可从下文中的问题排查与处理中检查每项配置是否正确。

1. 错误信息提示

启动Springboot应用时,错误信息如下

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

Description:

Field XXXXMapper in com.xxxx.service.XXXXServiceImpl required a bean of type 'com.xxxx.service.mapper.XXXXMapper' 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 'com.xxxx.service.mapper.XXXXMapper' in your configuration.

Disconnected from the target VM, address: '127.0.0.1:60506', transport: 'socket'

Process finished with exit code 1

2. 问题排查与处理

1. 检查application.yml配置是否正确

1.Mybtis配置

mybatis:
  typeAliasesPackage: com.xxx.entity
  mapperLocations: classpath:mapper/**/*.xml

2. MyBatis-Plus配置

# mybatis-plus配置
mybatis-plus:
  mapper-locations: classpath:mapper/**/*Mapper.xml,classpath:mapper/**/*Mapper.xml
  # Mybatis返回Map类型,当数据库字段值为空时,不显示字段问题配置,true:显示值为空的字段
  configuration:
    call-setters-on-nulls: true
  global-config:
    db-config:
      id-type: input
  configuration:
     #配置返回数据库(column下划线转java实体类为驼峰命名),自动匹配无需as(没开启这个,SQL需要写as:如 select test_id as testId from xxx)
     map-underscore-to-camel-case: true
     cache-enabled: false
     #配置JdbcTypeForNull, oracle数据库必须配置
     jdbc-type-for-null: "null"     

2. 检查主启动类标上注解是否正确

1. 将接口与对应的实现类放在与主启动类的同一个目录或者其子目录下;
 2. 在启动类上加上@MapperScan@MapperScans@ComponentScan注解,并配置自动扫描的包,如下

package com.yuan;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@SpringBootApplication
/*@MapperScans(value = {
        @MapperScan({"com.aa.*.mapper"}),
        @MapperScan("com.bb.*.mapper")
})*/
//或
@MapperScan({"com.yuan.*.mapper","com.yuan.aa.mapper"})
//或
//@ComponentScan(basePackages = {"com.xxx.xxx.mapper"})
public class YuanBoot3AdminApplication {

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

}

3. 检测XXXMaper接口上的注解是否正确

XXXMapper接口上标注@Mapper注解,如果在主启动类中启用了@MapperScan注解,则可以不加此注解

package com.yuan.sys.mapper;

import com.yuan.sys.entity.App;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

/**
 * <p>
 * 应用 Mapper 接口
 * </p>
 *
 * @author Yuan Jinsheng
 * @since 2023-07-19
 */
@Mapper
public interface AppMapper extends BaseMapper<App> {

}

4. 检查MapperScan注解中是否指定了annotationClasss属性

如果@MapperScan注解中annotationClass属性指定了扫描哪一类注解下的接口,则对应的XXXXMapper接口中要标注此类注解,否则也扫描不到。

annotationClass属性说明:

  1. 此属性指定扫描将搜索的注释。

  2. 扫描将注册基本包中也具有指定注释的所有接口。

  3. 请注意,这可以与markerInterface组合使用。

  4. 返回值:扫描将搜索的注释

1. 主启动注解配置

package com.yuan;

import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Repository;

import java.net.InetAddress;
import java.net.UnknownHostException;

@Slf4j
@SpringBootApplication
@MapperScan(value = {"com.yuan.*.mapper"},annotationClass = Repository.class)
public class YuanBoot3AdminApplication {
	public static void main(String[] args) throws UnknownHostException {
		SpringApplication.run(YuanBoot3AdminApplication.class, args);
	}
}

2. XXXXMapper接口

注意因为主启动类上标注了@MapperScan(value = {"com.yuan.*.mapper"},annotationClass = Repository.class)

则会扫描@Repository下的接口,此时XXXMapper接口中标注的@Mapper接口不生效

package com.yuan.sys.mapper;

import com.yuan.sys.entity.App;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;

/**
 * <p>
 * 应用 Mapper 接口
 * </p>
 *
 * @author Yuan Jinsheng
 * @since 2023-07-19
 */
@Repository //生效
//@Mapper 不生效
public interface AppMapper extends BaseMapper<App> {

}
  • 12
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值