java.lang.IllegalArgumentException:Property 'sqlSessionFactory' or 'sqlSessionTemplate'遇到这个问题我首先发度娘翻译了一下,意思是sqlSessionFactory和sqlSessionTemplate两个工厂方式不知道选哪一个,所以首先想到是不是用来两个关系框架,我用的是Mybatis,立马查看了pom.xml文件,发现了错误
<!-- mybatis 启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!-- springBoot jpa-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
jpa集成了hibernate的jar包,hibernate与mybatis二者都是关系映射型框架,只能用一个,所以第一步把jpa的依赖删除,删除发现依旧报错,此时查看beans类
import java.io.Serializable;
import java.util.Date;
@procxy
public class UserCard implements Serializable {
private int uid;
此时发现proxy是jap开启懒加载的没有删除,删除之后运行,以为就解决问题了,too yang ,还是IllegalStateException这个异常
Consider defining a bean of type 'zzsxt.mapper.UserCardMapper' in your configuration.
2018-12-08 11:53:04.368 ERROR 12292 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@20ce78ec] to prepare test instance [UserCardTest@551de37d]
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125) ~[spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108) ~[spring-test-5.0.9.RELEASE.jar:5.0.9.RELEASE]
根据提示Mpper文件扫描不到,此时查看mpper.xml发现映射没有任何问题,查看启动项App类发现错误
@SpringBootApplication
//@EnableScheduling //开启定时器
//@EnableCaching //开启缓存
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
没错,没有给spring boot 指定扫面的mapper 路径,此时有两个方法,方法一:注解式注入
@SpringBootApplication
//@EnableScheduling //开启定时器
//@EnableCaching //开启缓存
@MapperScan("zzsxt.mapper")
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
方式二:配置文件注解,在application.yml中,配置路径
mybatis:
mapper-locations: classpath:zzsxt/mapper/*.xml
【注意!!! 注解和配置文件,是优先扫描注解的,二者选其一,如果注解配错,配置对了也没用】
至此,重启运行,解决问题!!!