Mybatis之Mapper开发报错处理
- 一、项目结构说明
-
- 1. 接口DataMapper.java
- 2. 映射文件DataMapper.xml
- 3. 实体类Data.java
- 4. 创建SqlSession的工具类
- 5. MybatisDemo.xml
- 6. pom.xml
- 二、排错过程
在利用mapper代理进行mybatis开发时,报错:Exception in thread “main” org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): Mapper.DataMapper.queryAll。这一类型的错误均属于无法找到接口中的方法。我也是折腾了好久才发现错误,因此对此类错误做一个总结。
一、项目结构说明
1. 接口DataMapper.java
package Mapper;
import entity.Data;import java.util.List;public interface DataMapper {List<Data> queryAll();
}
2. 映射文件DataMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="Mapper.DataMapper"><select id="queryAll" resultType="entity.Data">select * from yangtingting</select>
</mapper>
3. 实体类Data.java
package entity;public class Data {private int id;private String name;private int age;public Data() {}public Data(String name, int age) {this.name = name;this.age = age;}public void setId(int id) {this.id = id;}public void setName(String name) {this.name = name;}public void setAge(int age) {this.age = age;}public int getId() {return id;}public String getName() {return name;}public int getAge() {return age;}@Overridepublic String toString() {return "Data{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}
}
4. 创建SqlSession的工具类
package util;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;public class MybatisUtils {private static SqlSessionFactory ssf=null;private static ThreadLocal<SqlSession> threadLocal=null;static{try{InputStream is= Resources.getResourceAsStream("MybatisDemo.xml");ssf=new SqlSessionFactoryBuilder().build(is);threadLocal=new ThreadLocal<>();}catch (IOException e){e.printStackTrace();}}public static SqlSession getSqlsession(){SqlSession session=threadLocal.get();if(session==null){session=ssf.openSession(true);}return session;}public static void closeSqlsession(){SqlSession session=threadLocal.get();if(session!=null){threadLocal.remove();session.close();}}
}
5. MybatisDemo.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!-- DTD:定义约束-->
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- Mybatis核心配置文件-->
<configuration><!-- 环境 environments 复数 环境集合(MYSQL/Oracle)default:默认(环境的id)--><environments default="development"><!--environments:代表一个环境id:唯一标识transactionManager:事务管理器dataSource:数据源(连接池),必须配置四个参数--><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/ytt?3useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8"/><property name="username" value="root"/><property name="password" value="admin"/></dataSource></environment></environments><mappers><package name="Mapper"/></mappers>
</configuration>
6. 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.mybatis</groupId><artifactId>MybatisMapper</artifactId><version>1.0-SNAPSHOT</version><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target></properties><build><resources><resource><directory>src/main/resources</directory><includes><include>*.xml</include></includes></resource><resource><directory>src/main/java/Mapper</directory><includes><include>*.xml</include></includes></resource></resources></build><dependencies><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.3.0</version></dependency><!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.17</version></dependency></dependencies></project>
二、排错过程
-
检查映射文件和mapper接口
映射文件是最容易出现错误的,因此放在第一个处理步骤。查错主要分为下面几点:
- 映射文件和mapper接口是否同名(除后缀外均相同),是否在同一个包下面。
- 映射文件的namespace名称是否与接口的全限定名称一致。
- 接口中的方法名称和映射文件中的标签保持一致,方法中的参数和映射文件中的parameterType或parameterMap保持一致,且方法的返回值类型和映射文件的resultType或resultMap保持一致
经过检查,我发现我的配置均无问题。
- 检查数据库相关配置文件MybatisDemo.xml
- 查看是否写入了mapper映射
- 映射文件夹是否正确
我的这一项配置也没问题。
-
检查pom.xml文件
实际上IDEA很奇怪,它只会自动编译resources文件夹下的.xml文件,其他目录下的.xml文件无法主动编译,这一点一定要Mark一下,经常作妖。
因此,需要自行添加相关的resources代码,比如我在xml文件中就定义了:
<resource><directory>src/main/java/Mapper</directory><includes><include>*.xml</include></includes></resource>
但是啊,这个地方必须要注意 <directory>
中只能写文件夹的路径,实际上我的问题就出在这里了。要注意啊,我的Mapper实际上是一个包!!!
将上述代码修改为:
<resource><directory>src/main/java</directory><includes><include>Mapper/*.xml</include></includes></resource>
再次运行,发现再也不报错了,简直了。。。。