Spring Boot 整合Mybatis 常见报错一 java.lang.IllegalArgumentException:Property sqlSessionFactory orsql

         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

【注意!!! 注解和配置文件,是优先扫描注解的,二者选其一,如果注解配错,配置对了也没用】

至此,重启运行,解决问题!!!

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值