框架:Spring Boot + Mybatis Plus
今天跑项目的时候,项目老是起不来,一直报错

 Unsatisfied dependency expressed through bean property 'sqlSessionFactory'
 Unsatisfied dependency expressed through field 'baseMapper'

疯狂报这两个错误,根据以前的经验应该是我的Mapper有没有被扫描到,如果没有扫描到是没有办法 创建的,所以我第一步查了看有没有@MapperScan(“com.xx.xx”)

  1. 看你的Mapper有没有被扫描到,我是自己加了个注解@MapperScan(“com.xx.xx”)这种形式,com.xx.xx就是你Mapper所在的包的包名
    代码可参考如下,自己写了个配置类。
@Configuration
@MapperScan("com.xx.xx")
public class MybatisPlusConfig {}

我发现这步没有什么问题,于是我又启动了一遍当然还是报错了,吐血。

  1. 然后我又检查了我的配置文件里面,看mybatis的映射文件有没有写对
mybatis-plus:
  mapper-locations: classpath*:/com/xx/xx/xx/xml/*.xml
  type-aliases-package: com.zctt.menu.entity
  global-config:
    banner: false
    db-config:
      id-type: input

检查了一下并没有什么问题。
3. 然后我又在想会不会我的xml文件没有打包进来,然后看了下我的pom文件里面

   <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>false</filtering>
                <includes>
                    <include>com/xx/xx/mapper/xml/*.xml</include>
                </includes>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

这就很奇怪了我也打包好了,看下target里面也是有我的xml文件的。

最后看日志看到了这样的一句话

The XML location is ‘file [E:\wl\workspace\menu\menu-mapper\target\classes\com\xx\xx\mapper\xml\MenuMapper.xml]’. Cause: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}.

不会是我xml文件里面出了问题吧结果去看了一下。
在这里插入图片描述
还真是我xml文件里面出了问题,这边当时写太快 ,漏写了一个,然后改掉之后 ,程序就跑起来了
在这里插入图片描述

总结

  1. 先看看@MapperScan(“com.xx.xx.xx”)或者在mapper的接口上面就加上@Mapper也行
  2. 看看配置文件里面映射有没有写好
  3. 看看编译好的文件里面有没有xml文件,如果没有的话就是maven打包没有把xml编译到文件下面,在代码里面加入以下代码就可以了。
      <resources>
          <resource>
              <directory>src/main/java</directory>
              <filtering>false</filtering>
              <includes>
                  <include>com/xx/xx/mapper/xml/*.xml</include>
              </includes>
          </resource>
      </resources>
  </build>
  1. 看看你的xml文件里面有没有标签或者什么东西写错了,检查检查你的xml文件。

还有记得好好看看日志,我就上了没看日志的当,让我头发又少了几根!!!!

一个不会写文章的程序员不是一个好的打野。
欢迎关注我微信公众号呀:千珏,有什么问题就后台留言给我吧,看到必回。

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐