【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 ...
随机推荐
- Helm: Error: no available release name found
如题,helm报这个错误 Helm: Error: no available release name found 错误的原因大概是因为 tiller没有正确的角色权限. 执行以下命令可解决这个问题. ...
- 3、css初识
前端内容就分三部分html.css.javascript(js),对一个网页来说html相当于是一个裸体的人,css相当于给这个人穿上了衣服,javascript相当于给这个人赋予动作行为,今天我们要 ...
- C#实战技能之WebApi+Task+WebSocket
一.背景介绍 环境的局限性: 用户在使用XX客户端的时候,必须每台电脑都安装打印组件,同时由于XX客户端使用的是 websocket进行通讯,这就必须限制用户的电脑浏览器必须是IE10.0+以上版本, ...
- 获取gcc和clang的内置宏定义
下面是对Gcc的内置宏定义的解释: https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html https://github.co ...
- iOS10 11跳转系统设置等的URL收集
Settings App-Prefs:root Settings -> About App-Prefs:root=General&path=About Settings -> Ac ...
- ios之animateWithDuration的坑
[UIView animateWithDuration:<#(NSTimeInterval)#> delay:<#(NSTimeInterval)#> options:< ...
- Error-MVC: “/”应用程序中的服务器错误。
ylbtech-Error-MVC: “/”应用程序中的服务器错误. 1.返回顶部 1. “/”应用程序中的服务器错误. 运行时错误 说明: 服务器上出现应用程序错误.此应用程序的当前自定义错误设置禁 ...
- Reactor反应器模式 (epoll)
1. 背景 最近在看redis源码,主体流程看完了. 在网上看到了reactor模式,看了一下,其实我们经常使用这种模式. 2. 什么是reactor模式 反应器设计模式(Reactor patter ...
- 命令 上传项目到git中
点击Clone or dowload会出现一个地址,copy这个地址备用. 接下来就到本地操作了,首先右键你的项目,如果你之前安装git成功的话,右键会出现两个新选项,分别为Git Gui Here, ...
- IDEA攻略合辑
AS使用lombok注解报错:Annotation processors must be explicitly declared now. The following dependencies on ...