(亲测解决)Unsatisfied dependency expressed through bean property 'sqlSessionFactory'

版权声明:本文为博主原创文章,转载请注明出处! https://blog.csdn.net/sinat_34104446/article/details/83473712

目录

一、问题描述

二、问题原因

三、问题解决


一、问题描述

    1.使用Maven+SpringBoot+MyBatis测试,启动项目报错:Unsatisfied dependency expressed through bean property 'sqlSessionFactory',也就是无法实例化,所有代码都没有问题

二、问题分析

    1.检查mapper的namespace和查询返回值对应的类都存在

     2.确保在SpringBoot配置文件中加载了mapper.xml文件

    3.确保Maven的依赖关系-加载了mybatis-spring-boot

    4.确保在启动类上加载了mapper.xml接口所在包@MapperScan(basePackages = {"mapper.xml接口所在包"})

    5.上述检查都没有问题,最后才知道原来mapper.xml被Maven给拦截了,导致加载不上,从而无法实例化sqlSessionFactory

三、问题解决

     1.在maven的pom.xml中放行mapper.xml,在Maven的build中加入以下配置,完整代码在最后

  2.完整参考pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.codecoord</groupId>
    <artifactId>JavaEEFrameReview</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>JavaEEFrameReview Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- springboot核心包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>

        <!-- jdbc驱动包 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
            <version>2.0.5.RELEASE</version>
        </dependency>

        <!-- mysql数据库驱动 包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.42</version>
        </dependency>

        <!-- Oracle驱动包 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.3</version>
        </dependency>

        <!-- MyBatis整合SpringBoot包 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>
    </dependencies>
    <build>
        <!-- 添加资源 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- src/main/resources下的指定资源放行 -->
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>
</project>

猜你喜欢

转载自blog.csdn.net/sinat_34104446/article/details/83473712