@MapperScan scanning range is too large to cause an interface error

I recently encountered a problem during development.

My controller uses @Autowired to introduce service, as follows:

@Slf4j
@RestController
@RequestMapping("/business")
public class BusinessController {

    @Autowired
    private MyService myService;

    @GetMapping(value = "/myBusiness")
    public BaseResponse<List<BusinessVo>> getMyBusiness(@RequestParam(value = "name") String name){
        return customerService.getMyBusiness(name, UserUtils.getId());
    }
}

//接口
public interface MyService {

    
    BaseResponse<List<BusinessVo>> getMyBusiness(String name, Long id);
}

//实现类
@Slf4j
@Service
public class MyServiceImpl implements CustomerOrderService {

    @Autowired
    private BusinessFeign businessFeign;

    
    public BaseResponse<List<BusinessVo>> getMyBusiness(String name, Long id){
        //这里远程调用其他服务的代码
    }
}

After running successfully, a 500 error occurs when accessing the interface.

 rear end:

 

Then I found out. The implementation class of myService of my BusinessController is actually: com.baomidou.mybatisplus.core.override.MybatisMapperProxy@5fc23d0

 This is definitely not true. The object of myService here should not be the reflection object of mybaties. It should be MyServiceImpl. This is only used when mapping the dao of Mapper.xml.

I guess the reason for this is that when configuring the path of the mapper, the path of the mapper file is enlarged. As a result, the reflection implementation of mybaties was used, but the corresponding mapper.xml was found. So the program throws a match exception.

So I went to find where the project configures the mapper path:

@SpringBootApplication
@EnableDiscoveryClient
@ServletComponentScan
@EnableAsync
@EnableFeignClients(basePackages = {"com.zhong.test.centre.*"})
@MapperScan(basePackages = { "com.zhong.test.centre" })
public class ServerApp {
    public static void main( String[] args ){
        SpringApplication.run(ServerApp.class, args);
    }
}

As can be seen from @MapperScan(basePackages = { "com.zhong.test.centre" }), this mapper scan configuration includes all the files of the entire project (com.zhong.test.centre is the upper path of all packages ), which caused the project to use the reflection implementation of mybaties when obtaining the implementation class of myservice (I have not studied the logic in it, it may be very complicated).

Solution:

1. If the project does not use a database, you can delete @MapperScan(basePackages = { "com.zhong.test.centre" }).

2. Narrow down the path to the specific path of our Mapper file. For example: @MapperScan(basePackages = { "com.zhong.test.centre.dao.mapper" })

Guess you like

Origin blog.csdn.net/qq_34484062/article/details/126480541