记录Invalid bound statement (not found)解决办法 & xml和mapper放在Java包中两个不同文件夹下以及放在Resources中的映射
Invalid bound statement (not found)错误原因及解决思路:
原因:
xml和mapper没有映射到;
思路:
** 一定要先确认你是怎么配置的:xml在Java包下还是在Resources包下 **
1、检查mapper的xml配置路径是否正确;
2、检查xml的namespace元素是否正确;
3、检查Mapper.java的方法在Mapper.xml是否存在,如果存在检查方法名称是否对应(注意大小写);
4、检查Mapper.java的方法返回值是否正确(select元素有没有正确配置ResultMap、parameterMap、parameterType、ResultType,这个是有区别的);
5、检查打完的包中是否有xml文件;
6、确认没有以上问题,任意修改对应的xml文件,比如添加一个空格,保存;
一、xml和mapper在Java同一个包中配置:
1、xml和mapper在Java同一个包下:
2、配置pom文件(扫描Java包下的xml文件,使得打包时能把xml文件打进来):
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- 打包跳过测试 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<!-- 扫描Java包下的xml、json文件 -->
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.json</include>
</includes>
<!-- 是否替换资源中的属性 -->
<filtering>true</filtering>
</resource>
<!-- 扫描Resources包下的xml、json、yml文件 -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.json</include>
<include>**/*.yml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
3、启动类加注解(扫描多个Java包下的mapper,单个包写一个就可以):
@MapperScan({"xxx.xxx.xxx.mapper", "xxx.xxx.xxxx.mapper"})
二、xml和mapper在Java不在同一个包中配置:
1、xml和mapper不在Java同一个包中:
2、配置同“一、xml和mapper在Java同一个包中配置:”的2、3步;
3、配置pom依赖
· 如果application中使用的mybatis-plus配置添加依赖如下:
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3.1</version>
</dependency>
· 如果application中使用的mybatis配置添加依赖如下:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
3、配置application.yml或application.properties文件,这里以yml、mybatis-plus为示例(如果要引用其他子项目工程的xml,classpath加星如:mapper-locations*: classpath:xxx/xxx/xxx/mapper/**/*.xml):
mybatis-plus:
mapper-locations: classpath:xxx/xxx/xxx/mapper/**/*.xml
global-config:
# 关闭MP3.0自带的banner
banner: false
configuration:
# 打印执行的sql,在开发或测试的时候使用
#log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
三、mapper在Java包,xml在Resources包
1、上示例图:
2、同“二、xml和mapper在Java不在同一个包中配置:”2、3步;
3、配置application.yml或application.properties文件,这里以yml、mybatis-plus为示例:
mybatis-plus:
configuration:
# 打印执行的sql,在开发或测试的时候使用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 数据库字段下划线与驼峰
map-underscore-to-camel-case: true
# mapper扫码路径
mapper-locations: mapper/*.xml
# xml配置别名
type-aliases-package: xxx.xxx.xxx.model.entity
配置项目踩坑,记录以加深记忆;
请尊重别人的劳动果实,转载劳烦注明出处;