问题一 (不存在的列)
Unknown column ‘name’ in ‘field list’
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'name' in 'field list'
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'name' in 'field list'
; bad SQL grammar []
主要原因:
Mybatis XML中的SQL语句查询的列,不在数据库中。
新增SQL语句中的列不在数据库中,或列对应的值,数据类型不一致。
解决办法: 根据项目实际情况,通常有以下三种解决办法:
修改SQL语句中,将不存在的列从语句中去掉。
在数据库中,新增该不存在的列。
在新增时,不要使用中文符号的``表示字符串。也就是~符号对应的键。
注意事项:
删除了数据库中的列。在SQL语句中,没有同步删除该字段的。会比较容易出现该问题。
这里还有一个比较特殊的场景,会导致上述问题的发生。
以下SQL在windows环境下会显示上述错误,不会进行新增操作:
insert into t_base_user(name,created_time,updated_time)values(name
,now(),now());
但在Linux环境下,其会进行新增操作。但是 name 的值不会进行新增。
问题二 (模棱两可的字段)
Column ‘oid’ in field list is ambiguous
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'oid' in field list is ambiguous
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Column 'oid' in field list is ambiguous; SQL []; Column 'oid' in field list is ambiguous
主要原因:连接查询时没有指定共有字段的所属表。 也就是说。A表有name字段,B表也有name字段。连接查询时查询name时。SQL服务器不知道返回哪个表的name字段导致。
解决方案:明确查询列的所属表。
问题三 (不一致的接收对象)
TooManyResultsException:
Exception in thread "main" org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.TooManyResultsException: Expected one result (or null) to be returned by selectOne(), but found: 9
主要原因:在接口中指定单个返回对象。但执行SQL后,有多条符合条件的数据。
解决方案:
根据实际的业务场景,通常有以下两种解决办法:
修改接口的返回结果为集合。
修改SQL语句使其只返回一个符合条件的结果。
问题四 (重复的方法名)
Mapped Statements collection already contains value for UserDao.getUserByName
Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.lang.RuntimeException: Error parsing Mapper XML. Cause: java.lang.IllegalArgumentException: Mapped Statements collection already contains value for UserDao.getUserByName
原因: 在UserDao中,有两个同名方法getUserByName导致。
解决办法: 重命名其中一个方法名即可。
问题五 (重复的方法名)
Mapped Statements collection does not contain value for ‘xxx’
Cause: java.lang.IllegalArgumentExeception: Mapped Statements collection does not contain value for 'xxx'
原因: "Mapper的结果不包含 ‘xxx’ ",因为xxx在全局配置文件中没有加载该类的mapper.xml
解决办法: 重命名其中一个方法名即可。
问题六 (不存在的属性)
There is no getter for property named ‘username’ in ‘com.andyqian.user.bean.User’
Error updating database. Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'username' in 'com.andyqian.user.bean.User'
Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'education' in 'class com.andyqian.user.bean.User'
原因: SQL中查询的列,在其实体对象中不存在对应的属性。
解决办法: 在对应的实体对象上,添加上缺失的属性即可。