1. 程式人生 > >spring-boot在啟動專案時找不到Mapper。

spring-boot在啟動專案時找不到Mapper。

最近在做spring-boot專案的時候出現了找不到mapper介面的問題,控制檯列印如下:

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

Description:

A component required a bean of type 'com.ruifeng.demo.dao.UserMapper' that could not be found.


Action:

Consider defining a bean of type 'com.ruifeng.demo.dao.UserMapper' in your configuration.

出現這種問題,目前收集到了兩種解決方式:

1.在Mapper介面上使用@Mapper註解,但是這種方法有個弊端,就是當我們有很多mapper的時候,每一個介面上都需要新增@Mapper註解,這樣顯得很是繁瑣。那麼第二種方法就來了。

@Mapper
public interface UserMapper {

    User getUserByName(String name);
}

2.在Application.java(啟動類)上新增@MaperScan註解來解決,來掃描某個包下的所有Mapper介面。

@SpringBootApplication
@MapperScan(value = "com.ruifeng.demo.dao")
public class DemoApplication {

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

這樣的話,com.ruifeng.demo.dao下的所有mapper都可以被掃描到。沒有@Mapper那麼繁瑣。

廣大的猿輩門有什麼好的解決方法可以告訴我卅!!!