SpringBoot注入Mapper失敗,可能是因為沒有加掃描Mapper層的注解
方式一:在所有mapper接口使用@Mapper注解
1 @Mapper 2 public interface UserMapper { 3 UserInfo getUserById(@Param("userId") String userId); 4 }
方式二:在springboot配置類或啟動類使用@MapperScan注解
(作用:將指定包中的所有接口都標注為DAO層接口,相當於在每一個接口上寫@Mapper)
1 @Configuration 2 @MapperScan(basePackages = "com.test.dao") 3 public class ApplicationConfig {
4 }
方式三:在springboot配置類或啟動類使用@ComponentScan注解
(作用:掃描指定包中的所有接口,相當於在每一個接口上寫@Service或@Component或@Repository或@Controller)
1 @ComponentScan(basePackages = "com.test.dao") 2 public class ApplicationConfig { 3 }