SpringBoot与MyBatisPlus整合常见’XXXXMapper’ that could not be found问题处理方式
文章目录
- 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.
的问题;- 如果出现了类似问题,可从下文中的问题排查与处理中检查每项配置是否正确。
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属性说明:
此属性指定扫描将搜索的注释。
扫描将注册基本包中也具有指定注释的所有接口。
请注意,这可以与markerInterface组合使用。
返回值:扫描将搜索的注释
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> {
}