其餘亂七八糟配置就不扯了,先上項目結構圖java
配置好參數後我再src/test/java類測試訪問數據庫時發現bean沒有正確的注入。值得注意的是,這個項目的啓動類是叫App.javaspring
因此咱們必須在這個測試類上面加上註解:數據庫
@RunWith(SpringRunner.class) @SpringBootTest(classes = App.class)
注意:SpringBoot(classes = App.class) classes後面跟的是啓動類的class,千萬不要隨便抄網上的配置,寫一些Application.class之類的,這種Application之類的類名和一些官方包裏的類名同樣,容易引入錯誤的包。express
剛開始發現這個問題瘋狂去網上看別人的配置文件是怎麼寫的,試了一天都沒用,後來靜下心來,把錯誤信息copy出來文本里仔細看springboot
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.springboot.LibrarySystem.mapper.UserMapperTest': Unsatisfied dependency expressed through field 'userMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.sb.LibrarySystem.mapper.UserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
仍是從這個Test類下手app
原本個人類是這樣的:測試
@RunWith(SpringRunner.class) @SpringBootTest(classes = Application.class) public class UserMapperTest {
}
修改後就是這樣,和個人啓動類的類名是一致的:ui
@RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class UserMapperTest {
完美解決!spa
若是百度的時候,發現查看的問題愈來愈深,愈來愈偏離最開始的問題,那十有八九是方向偏了,從新整理一下,從新開始吧code