SpringBoot 找不到Dao注解和sqlSessionFactory’ or ‘sqlSessionTemplate’ are required,

SpringBoot简单搭建报错:Field userDao in com.example.service.UserService required a bean of type ‘com.example.dao.IUserDao’ that could not be found

刚开始写了一个demo玩玩只是这句报错,然后调了好久,没出来就去干其他事了,周一找组里大神调了一下,加了这句:这里写图片描述
这句是扫描那个Dao的包,加上后不报找不到的错了,又报这个:sqlSessionFactory’ or ‘sqlSessionTemplate’ are required,
提示说SqlSession乱七八糟找不到了,然后在网上搜了一下,说是pom里面的mybatis版本太高,我降到1.2.1还是报错,最后网上找的另一个方案是加这个这里写图片描述
最后,成功启动,并查询,哎呀,这个注解真的有时候搞的人很蒙蔽!!!

**报错内容为** <br></br> ``` Field vulLibMapper in com.shtec.dataonweb.service.TestService required a bean of type 'com.shtec.dataonweb.dao.VulLibMapper' that could not be found. The injection point has the following annotations: @org.springframework.beans.factory.annotation.Autowired(required=true) ``` <br></br> **Service内容为** <br></br> ``` package com.shtec.dataonweb.service; import com.shtec.dataonweb.bean.VulLibExample; import com.shtec.dataonweb.dao.VulLibMapper; import com.shtec.dataonweb.bean.VulLib; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class TestService { @Autowired VulLibMapper vulLibMapper; public VulLib testUpdated() { VulLibExample example = new VulLibExample(); List<VulLib> list = vulLibMapper.selectByExample(example); return list.get(0); } } ``` <br></br> **Mapper内容就不列出来了,都是根据generatorConfig.xml自动生成的,mybatis-generator-core的版本为1.3.4,mybatis-spring-boot-starter的版本为2.1.3,mybatis-generator-maven-plugin的版本为1.3.2。 <br></br> 很多方法都试过,给Mapper加@Repository无效。如果用@MapperScan则会报如下错误** <br></br> ``` nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required ``` <br></br> 我自己搞定了,原因是没配置application.yml内的mybatis配置,把xml文件和接口文件都放在一个包下,然后配置: ``` mybatis: mapper-locations: com.shtec.dataonweb.mapper.*.xml type-aliases-package: com.shtec.dataonweb.bean ``` 再入口类加上@MapperScan("com.shtec.dataonweb.mapper")自动扫描。 <br></br> 最后在pom.xml的bulid里加上: ``` <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources> ``` 大功告成~
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117) at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70) Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productCategoryMapper' defined in file [C:\Users\C\IdeaProjects\waimai\target\classes\cn\bzu\waimai\mapper\ProductCategoryMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1630) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:481) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:312) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:308) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:745) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128) at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:281) at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:257) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98) at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116) ... 24 more Caused by: java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required at org.springframework.util.Assert.notNull(Assert.java:134) at org.mybatis.spring.support.SqlSessionDaoSupport.checkDaoConfig(SqlSessionDaoSupport.java:74) at org.mybatis.spring.mapper.MapperFactoryBean.checkDaoConfig(MapperFactoryBean.java:73) at org.springframework.dao.support.DaoSupport.afterPropertiesSet(DaoSupport.java:44) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1688) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626) ... 39 more 21:29:08.532 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - After test class: context [DefaultTestContext@3cb1ffe6 testClass = WaimaiApplicationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [MergedContextConfiguration@3dfc5fb8 testClass = WaimaiApplicationTests, locations = '{}', classes = '{}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', contextCustomizers = set[[ImportsContextCustomizer@467aecef key = [@org.mybatis.spring.annotation.MapperScan(sqlSessionFactoryRef=, sqlSessionTemplateRef=, annotationClass=interface java.lang.annotation.Annotation, basePackageClasses=[], markerInterface=class java.lang.Class, nameGenerator=interface org.springframework.beans.factory.support.BeanNameGenerator, basePackages=[], value=[cn.bzu.waimai.mapper], factoryBean=class org.mybatis.spring.mapper.MapperFactoryBean), @org.springframework.context.annotation.Import(value=[class org.mybatis.spring.annotation.MapperScannerRegistrar]), @org.junit.runner.RunWith(value=class org.springframework.test.context.junit4.SpringRunner)]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@73ad2d6, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@5700d6b1, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@0, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@31dc339b], contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader', parent = [null]]], class annotated with @DirtiesContext [false] with mode [null]. Process finished with exit code -1
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页