解决MyBatis @param注解参数类型错误异常的问题
作者:wangjinnan16 时间:2023-12-01 06:41:45
问题现象
今天使用mybatis遇到个很奇怪的问题,我使用一个参数@param("threshold"),类型是java的double,但是很奇怪,一直告诉我参数转换错误,int不能转double,我就奇怪了,哪里来的int。
解决办法
我感觉可能使用用到了mybatis的关键字,所以就把threshold换了个名字,果然好了。
问题原因
某些关键词,mybatis会认为是某种类型,下面列出来一些,后面发现再补充。
size, threshold, modCount是int类型
loadFactor是float类型
补充:mybatis使用@Param的坑
在mybatis中@Param注解的作用是为参数指定一个名称,在mapper文件中使用,而不是使用mybatis的arguments[0,1…]代替。但是在非动态的mapper——mybatis根据mapper接口创建实现类,@param注解是不起作用的。
阅读mybatis的源码得知,检查@param注解的方法:
private boolean hasNamedParams(Method method) {
boolean hasNamedParams = false;
final Object[][] paramAnnos = method.getParameterAnnotations();
for (Object[] paramAnno : paramAnnos) {
for (Object aParamAnno : paramAnno) {
if (aParamAnno instanceof Param) {
hasNamedParams = true;
break;
}
}
}
return hasNamedParams;
}
此方法位于 org.apache.ibatis.binding.MapperMethod 的内部类 MethodSignature 中。
根据源码一步一步的递归:
MethodSignature的构造方法——>MapperMethod的构造方法——>MapperProxy的cachedMapperMethod 和invoke方法 ——> MapperProxyFactory中的 newInstance 方法。
@SuppressWarnings("unchecked")
protected T newInstance(MapperProxy<T> mapperProxy) {
//jdk的 * 生成mapper对象
return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy);
}
public T newInstance(SqlSession sqlSession) {
//MapperProxy实现了Invocation接口
final MapperProxy<T> mapperProxy = new MapperProxy<T>(sqlSession, mapperInterface, methodCache);
return newInstance(mapperProxy);
}
在整合spring与mybatis,使用spring提供的sqlSessionTemplate进行查询时,没有从MapperRegistry的mapper缓存集合中取mapper,而是直接使用配置的sqlSessionTemplate。
所以这种清况下,@param注解是无效的。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/wangjinnan16/article/details/75284394
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java 自动生成略缩图示例代码
200行java代码实现2048小游戏
![](https://img.aspxhome.com/file/2023/3/60693_0s.gif)
Spring学习教程之AOP模块的概述
![](https://img.aspxhome.com/file/2023/7/91747_0s.jpg)
详解关于Windows10 Java环境变量配置问题的解决办法
![](https://img.aspxhome.com/file/2023/7/101917_0s.png)
java原装代码完成pdf在线预览和pdf打印及下载
![](https://img.aspxhome.com/file/2023/7/72617_0s.png)
Java对List进行排序的两种实现方法
![](https://img.aspxhome.com/file/2023/8/65758_0s.png)
SpringBoot整合Jackson超详细用法(附Jackson工具类)
![](https://img.aspxhome.com/file/2023/8/70588_0s.png)
微信跳一跳辅助Java代码实现
Java编写多功能万年历程序的实例分享
Android TabHost如何实现顶部选项卡
![](https://img.aspxhome.com/file/2023/2/126732_0s.gif)
Android Intent实现页面跳转的方法示例
![](https://img.aspxhome.com/file/2023/9/93289_0s.png)
Spring Cloud Ribbon客户端详细介绍
![](https://img.aspxhome.com/file/2023/7/89947_0s.png)
java poi解析word的方法
Android gradle打包并自动上传的方法
![](https://img.aspxhome.com/file/2023/9/138909_0s.jpg)
在C#中 webbrowser的使用心得
mybatis 查询返回Map<String,Object>类型
![](https://img.aspxhome.com/file/2023/0/58760_0s.png)
Java程序员容易犯的10大低级错误
C语言实现生日贺卡
![](https://img.aspxhome.com/file/2023/8/127698_0s.jpg)