【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 ...
随机推荐
- Vue相关开源项目库汇总(史上最全)
偶尔在网上看到的,先保存着,以备不时之需 https://www.jianshu.com/p/e37f4dde49a2 better-scroll:是一款重点解决移动端(未来可能会考虑 PC 端)各种 ...
- 【组合数】[NOIP2011]选择客栈[c++]
题目描述 丽江河边有n 家很有特色的客栈,客栈按照其位置顺序从 1 到n 编号.每家客栈都按照某一种色调进行装饰(总共 k 种,用整数 0 ~ k-1 表示),且每家客栈都设有一家咖啡店,每家咖啡店均 ...
- pygame-KidsCanCode系列jumpy-part13-改进跳跃
这节研究下跳跃如何做得更自然,先看看之前的跳跃有什么问题,我们把settings.py里的初始化参数调整下: # starting platform # PLATFORM_LIST = [(5, HE ...
- Lua模块的加载与内存释放
今天早上听说一件事情让我觉得很诡异的事情:公司线上的一款游戏,加载一份配置资源后,内存涨了几十M,然后内存再也下不来了.因为好奇,所以要来了最大的一个配置文件(4.5M,去除空格与换行后的大小),进行 ...
- Why validation set ?
Let's assume that you are training a model whose performance depends on a set of hyperparameters. In ...
- SpringBoot(九):多模块下mapper分散后无法启动SpringBoot解决方法
问题描述: SpringBoot项目之前是一个项目*-web,运行没问题,后来将项目拆分为多个项目,就出现启动失败问题. SpringBoot项目结构: 项目被分为多块:*-mapper(mybati ...
- Initialize a vector in C++ (5 different ways)
https://www.geeksforgeeks.org/initialize-a-vector-in-cpp-different-ways/ Following are different way ...
- [Vuex] Perform Async Updates using Vuex Actions with TypeScript
Mutations perform synchronous modifications to the state, but when it comes to make an asynchronous ...
- eclipse项目名称后面括号里的名称和项目名称不一样
解决方案: 1:项目右键-属性(Properties)-Web Project Setting, 改名称注意:这个名字将成为你在浏览器访问的路径 2:打开项目目录的.setting文件夹,随便一个文本 ...
- Teigha.NET开发入门1- Teigha介绍
对于CAD开发,无疑较强大的方式是Lisp.AutoCAD二次开发,且学习资源丰富,依靠强大的AutoCAD的环境可以干很多事,省很多力.但若要脱离AutoCAD环境,那就当属Teigha了. 名称问 ...