【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String
我们知道在mybatis的映射中传参数,只能传入一个。通过#{参数名} 即可获取传入的值。
Mapper接口文件:
public int delete(int id) throws Exception;
MapperL配置文件:
<delete id="delete" parameterType="int">
delete from user where id=#{id}
</delete>
接口中我们定义了delete(int id),形参的名称为id。顺理成章的在sql段里就用#{id}去获取。
其实这里的”参数名”可以是任意的。
因为JAVA反射只能获取方法参数的类型,但无从得知方法参数的名字的
上面这个例子把#{id}换成#{di},一样运行。当然为了便于阅读代码,还是用#{id}。
_parameter则是java对通过反射获取参数后,给参数取的别名。所以用#{_parameter}也行。
但有几种情况你必须得用_parameter:
第一种情况:拼接字符${}。咱这里先不去考虑SQL注入这些问题。
public List<User> findByName(String searchkey) throws Exception;
<select id="findByName" parameterType="String" resultType="twm.mybatisdemo.pojo.User">
select * from user where username like '%${searchkey}%'
</select>
因为要使用模糊查询。要在入参前后加上%,所以不能用占位符#{},而要用拼接字符串。因此我们理所当然的接入
{searchkey}
一切都似乎没有问题,运行,报错:
Exception in thread “main”
org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘searchkey’ in ‘class java.lang.String’ ### Cause:
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named ‘searchkey’ in ‘class java.lang.String’
问题就出在拼接字符串${searchkey}。和占位符#{}不同的是,java不会自动将${searchkey}对应成_parameter。因此只能自己写
<select id="findByName" parameterType="String" resultType="twm.mybatisdemo.pojo.User">
select * from user where username like '%${_parameter}%'
</select>
OK,一切又正常了。
可是你没有觉得这种写法太不赏必悦目了?
mybatis考虑到这点,你只需在接口的方法参数前加上param注解就好了。
public List<User> findByName(@Param(value="searchkey") String searchkey) throws Exception;
第二种情况:动态SQL中的条件判断语句中
public List<User> findByName(String searchkey) throws Exception;
<select id="findByName" parameterType="String" resultType="twm.mybatisdemo.pojo.User">
<bind name="likestr" value="'%'+ searchkey +'%'"></bind>
select * from user
<where>
<if test="searchkey !='' and searchkey !=null">
username like #{likestr}
</if>
</where>
</select>
在if和bind元素中涉及到参数searchkey。直接使用searchkey也会报上面一样的错误
解决办法就是用_parameter替换searchkey
或者在接口的方法参数前加上param注解。
public List<User> findByName(@Param(value="searchkey") String searchkey) throws Exception;
【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String的更多相关文章
- Mybatis查询报错:There is no getter for property named '*' in 'class java.lang.String
问题: 执行查询时报错:There is no getter for property named '*' in 'class java.lang.String 原因: 传过去的参数为识别.本例为 p ...
- _parameter:解决There is no getter for property named in class java.lang.String
我们知道在mybatis的映射中传参数,只能传入一个.通过#{参数名} 即可获取传入的值. Mapper接口文件: public int delete(int id) throws Exception ...
- mybatis 错误: There is no getter for property named '*' in 'class java.lang.String解决
现象: mybatis mapper.xml 的sql里如果直接使用了想要传入的变量,比如: <select id="selectXx" resultType="i ...
- Mybatis 报错 There is no getter for property named '***' in 'class java.lang.String'
在mapper.xml中 , 如果单参数是String类型 , 且在sql语句中对参数进行了判断 , 如下 when 中的判断 , 如果出现 if 判断也是一样的.都需要把判断中的参数用 _param ...
- mybatis There is no getter for property named '*' in 'class java.lang.String
1.原因 server层 xxxx.get("1234") map <if test="aaa != null and aaa.id != null and ...
- Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String'
Mybatis中传参包There is no getter for property named 'XXX' in 'class java.lang.String' 一.发现问题 <select ...
- mybatis parameterType报错:There is no getter for property named 'xxx' in 'class java.lang.String'
方法1: 当parameterType = "java.lang.String" 的时候,参数读取的时候必须为 _parameter 方法2: 在dao层的时候,设置一下参数,此方 ...
- Mybatis问题:There is no getter for property named 'unitId' in 'class java.lang.String'
Mybatis遇到的问题 问题: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.re ...
- (mybatis)There is no getter for property named 'isEffective' in 'class java.lang.String
原来代码: <select id="findSpecialOffer" resultType="com.lizard.back.model.SpecialOffer ...
随机推荐
- 页面嵌套iframe后,点击里面的链接,然后父窗口跳转(子窗口控制父窗口的链接跳转)
做app的时候遇到一个问题,一个页面,然后里面嵌套了一个另一个页面,想实现点击里面的链接,然后外面进行跳转,不然的话,里面的页面永远出不来, 后面想了个办法,app的页面都是打开打开,不关闭的,然后由 ...
- HTML5 学习04—— MathML数学标记
MathML 元素标签: <math>...</math> MathML 是数学标记语言,是一种基于XML(标准通用标记语言的子集)的标准,用来在互联网上书写数学符号和公式的置 ...
- Scikit-learn 概述
https://www.leiphone.com/news/201701/ZJMTak4Y8ch3Nwd0.html
- FFM及DeepFFM模型在推荐系统的探索及实践
12月20日至23日,全球人工智能与机器学习技术大会 AiCon 2018 在北京国际会议中心盛大举行,新浪微博AI Lab 的资深算法专家 张俊林@张俊林say 主持了大会的 搜索推荐与算法专题,并 ...
- [Linux] - SVN忽略文件夹更新的命令与方法
在服务器上有时需要忽略某个文件夹及内容的更新,可以使用命令: svn update --set-depth=exclude FOLDER_NAME 比如需要忽略static目录: svn update ...
- 【MAC】Mac下部分常用的小工具
Homebrew: 官方介绍:The missing package manager for OS X(OS X 不可或缺的套件管理器) /usr/bin/ruby -e "$(curl - ...
- go微服务框架go-micro深度学习(五) stream 调用过程详解
上一篇写了一下rpc调用过程的实现方式,简单来说就是服务端把实现了接口的结构体对象进行反射,抽取方法,签名,保存,客户端调用的时候go-micro封请求数据,服务端接收到请求时,找到需要调用调 ...
- tensorflow 在加载大型的embedding模型参数时,会遇到cannot be larger than 2GB
这种问题是,对于每一个变量 variable 由于是基于protobuf存在这大小限制(2G),这个时候,我们需要将embedding拆开,拆分成N等分,来使得每一个 variable都在2G以下; ...
- MATLAB 统计不同区间中元素的个数
使用 find 命令: x = :;%生成数组 k = find( x > & x < );%查找大于2小于5的元素的数组下标 size(k,) %统计的元素的个数
- Linux服务器CPU使用率较低但负载较高
CPU使用率较低但负载较高 问题描述 Linux 系统没有业务程序运行,通过 top 观察,类似如下图所示,CPU 很空闲,但是 load average 却非常高,如下图所示. 处理办法 load ...