使用MyBatis是出现的Bug

Type interface mapper.UserMapper is not known to the MapperRegistry.

1
2
3
4
5
org.apache.ibatis.binding.BindingException: Type interface mapper.UserMapper is not known to the MapperRegistry.
at org.apache.ibatis.binding.MapperRegistry.getMapper(MapperRegistry.java:47)
at org.apache.ibatis.session.Configuration.getMapper(Configuration.java:745)
at org.apache.ibatis.session.defaults.DefaultSqlSession.getMapper(DefaultSqlSession.java:292)
at test.ManyToManyTest.main(ManyToManyTest.java:18)

分1:没有引入Mapper.xml

这是因为没有在mybatis-config.xml中引入UserMapper.xml.

解决

mybatis根配置文件mybatis-config.xml中,引入UserMapper.xml即可:<mapper resource="mapper/UserMapper.xml"/>

1
2
3
4
5
6
7
8
9
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
......
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
......
</configuration>

分析2:没有引入Mapper接口路径

解决

引入StudentMapper.java接口即可:

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- 该配置文件包含对 MyBatis 系统的核心设置 -->
<configuration>
......
<mappers>
<mapper class="mapper.ClazzMapper"/>
<mapper class="mapper.StudentMapper"/>
</mappers>
......
</configuration>

总结

引入Mapper.xml映射文件,使用resource属性,而引入接口则使用class属性.

Table ‘mybatis.tb_calzz’ doesn’t exist

错误提示

1
2
3
4
5
6
7
8
9
10
DEBUG [main] ==>  Preparing: select * from tb_student where id=? 
DEBUG [main] ==> Parameters: 1(Integer)
DEBUG [main] ====> Preparing: select * from tb_calzz where id=?
DEBUG [main] ====> Parameters: 1(Integer)
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'mybatis.tb_calzz' doesn't exist
### The error may exist in mapper/ClazzMapper.java (best guess)
### The error may involve mapper.ClazzMapper.selectClazzById-Inline
### The error occurred while setting parameters
### SQL: select * from tb_calzz where id=?

分析

提示很明显,说mybatis数据库中不存在tb_calzz这个表.这种情况:

  • 要么是这个表不存在,如果是这样,创建进入数据库,创建这个表即可。
  • 要么是SQL中这个表名字打错了,这种情况,修改SQL语句,把表名改为正确的表名即可.

Error querying database. …Query was empty

1
2
3
4
5
6
org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Query was empty
### The error may exist in mapper/UserMapper.xml
### The error may involve mapper.UserMapper.selectUserById-Inline
### The error occurred while setting parameters
### SQL:

原因

映射文件mapper/UserMapper.xmlidselectUserByIdselect标签中没有写SQL语句。

解决

写上SQL语句即可。