java - Mybatis查询数据库时出现查询getInt()的错误
问题描述
我数据库表的id是varchar类型,而已这条数据也是项目插入到数据库的,但是在后面查询的时候就出现了一下错误。
严重: Servlet.service() for servlet [SpringMVC] in context with path [/cims-ssm] threw exception [Request processing failed; nested exception is org.springframework.dao.TransientDataAccessResourceException: Error attempting to get column ’id’ from result set. Cause: java.sql.SQLException: Invalid value for getInt() - ’x41D6z93’; SQL []; Invalid value for getInt() - ’x41D6z93’; nested exception is java.sql.SQLException: Invalid value for getInt() - ’x41D6z93’] with root causejava.sql.SQLException: Invalid value for getInt() - ’x41D6z93’
这是错误信息,以下是相关代码

这是查询映射代码
我代码在数据库里就能查询出来

望各位看看对了 model中 club中的id属性是string的
问题解答
回答1:mapping的问题:
<resultMap type='com.uiyllong.cims.model.ClubType'> <id column='id' jdbcType='INTEGER' property='id' /> <!-- 省略 --></resultMap><resultMap type='com.uiyllong.cims.model.Club'> <id column='id' jdbcType='VARCHAR' property='id' /> <!-- 省略 --> <association column='club_type_id' jdbcType='INTEGER'property='clubType' javaType='com.uiyllong.cims.model.ClubType'resultMap='resultClubType' /></resultMap>
resultClub和resultClubType的id属性对应的列名都是id,而其中一个的类型是int。
sql的问题(记得以后贴问题把代码贴上来,贴截图让人家怎么拿你代码试,手敲吗?):
select club_t.id, ...club_type_t.id....
这个查询结果有两列ID。
解决办法很简单,把 club_type_t 列设置别名,比如添加前缀:
select club_t.id, ...club_type_t.id ct_id, club_type_t.typeName ct_type_name
利用mybatis的columnPrefix:
<association property='clubType' columnPrefix='ct_' notNullColumn='ct_id' resultMap='resultClubType' />
相关文章:
1. javascript - weex和node,js到底是怎样一个关系呢?2. dockerfile - 为什么docker容器启动不了?3. docker gitlab 如何git clone?4. docker - 各位电脑上有多少个容器啊?容器一多,自己都搞混了,咋办呢?5. nignx - docker内nginx 80端口被占用6. golang - 用IDE看docker源码时的小问题7. javascript - 修改表单多选项时和后台同事配合的问题。8. 我在centos容器里安装docker,也就是在容器里安装容器,报错了?9. redis - 究竟是选择微信小程序自带的统计工具还是自己开发一个数据统计的代码?10. docker安装后出现Cannot connect to the Docker daemon.

网公网安备