有一种原因是xxxMapper.xml文件编写有问题,这个原因这里不做讨论
第二种:对mapper.xml文件未加扫描
启动springboot对象上添加:@MapperScan("com.example.demo.mapper"),同时在mapper文件上加上@Mapper注解
第二种:原因就是项目运行的时候,找不到xxxMapper.xml文件,项目编译的之后,没有这个文件。
产看编译生成target:果然找不到对应的xxxMapper
因为编译的时候,没有吧xml编译过来
解决方法1:在pom.xml加上配置
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
解决方法2:
将xml放到resources/mybatis下面,并在yml配置文件中加上如下配置,
mybatis: mapper-locations: classpath:mybatis/*.xml