我们知道在mybatis的映射中传参数,只能传入一个。通过#{参数名} 即可获取传入的值。

Mapper接口文件:

public int delete(int id) throws Exception;
1
MapperL配置文件:

<delete id="delete" parameterType="int">
delete from user where id=#{id}
</delete>
1
2
3
接口中我们定义了delete(int id),形参的名称为id。顺理成章的在sql段里就用#{id}去获取。
其实这里的”参数名”可以是任意的。
因为JAVA反射只能获取方法参数的类型,但无从得知方法参数的名字的
上面这个例子把#{id}换成#{di},一样运行。当然为了便于阅读代码,还是用#{id}。
_parameter则是java对通过反射获取参数后,给参数取的别名。所以用#{_parameter}也行。

但有几种情况你必须得用_parameter:

第一种情况:拼接字符${}。咱这里先不去考虑SQL注入这些问题。

public List<User> findByName(String searchkey) throws Exception;
1
<select id="findByName" parameterType="String" resultType="twm.mybatisdemo.pojo.User">
select * from user where username like '%${searchkey}%'
</select>
1
2
3
因为要使用模糊查询。要在入参前后加上%,所以不能用占位符#{},而要用拼接字符串。因此我们理所当然的接入。因此我们理所当然的接入{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>
1
2
3
OK,一切又正常了。
可是你没有觉得这种写法太不赏必悦目了?

mybatis考虑到这点,你只需在接口的方法参数前加上param注解就好了。
public List<User> findByName(@Param(value="searchkey") String searchkey) throws Exception;

第二种情况:动态SQL中的条件判断语句中

public List<User> findByName(String searchkey) throws Exception;
1
<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>
1
2
3
4
5
6
7
8
9
在if和bind元素中涉及到参数searchkey。直接使用searchkey也会报上面一样的错误
解决办法就是用_parameter替换searchkey

或者在接口的方法参数前加上param注解。

public List<User> findByName(@Param(value="searchkey") String searchkey) throws Exception;
---------------------

原文:https://blog.csdn.net/soonfly/article/details/63385018

_parameter:解决There is no getter for property named in class java.lang.String的更多相关文章

  1. 【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String

    我们知道在mybatis的映射中传参数,只能传入一个.通过#{参数名} 即可获取传入的值. Mapper接口文件: public int delete(int id) throws Exception ...

  2. mybatis 错误: There is no getter for property named '*' in 'class java.lang.String解决

    现象: mybatis mapper.xml 的sql里如果直接使用了想要传入的变量,比如: <select id="selectXx" resultType="i ...

  3. 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 ...

  4. Mybatis 报错 There is no getter for property named '***' in 'class java.lang.String'

    在mapper.xml中 , 如果单参数是String类型 , 且在sql语句中对参数进行了判断 , 如下 when 中的判断 , 如果出现 if 判断也是一样的.都需要把判断中的参数用 _param ...

  5. 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 ...

  6. spring mvc出现 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'endtime'

    在使用spring mvc中,绑定页面传递时间字符串数据给Date类型是出错: Failed to convert property value of type [java.lang.String] ...

  7. 【spring mvc】后台API查询接口,get请求,后台Date字段接收前台String类型的时间,报错default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'createDate';

    后台API查询接口,get请求,后台Date字段接收前台String类型的时间筛选条件 后台接口接收 使用的实体 而createDate字段在后台实体中是Date类型 报错信息: org.spring ...

  8. Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'

    查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss 服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日 ...

  9. message [Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property

    springmvc前台字符串,后台Date类型字段.时间强转失败 数值:18年12月31日 15:43:21 解决方法,给时间字段加注释 @DateTimeFormat(pattern = " ...

随机推荐

  1. expdp使用

    原文:https://blog.csdn.net/zftang/article/details/6387325 ORACLE EXPDP命令使用详细相关参数以及导出示例: 1. DIRECTORY指定 ...

  2. [转载]C++STL概述

    来源:https://www.cnblogs.com/dyllove98/p/3214898.html 什么是容器 首先,我们必须理解一下什么是容器,在 C++ 中容器被定义为:在数据存储上,有一种对 ...

  3. MySQL两种内核对比

    MySQL内核 https://blog.csdn.net/baichoufei90/article/details/83504446 关键字:全文索引 索引外置 两种内核:MyISAM 和InnoD ...

  4. 使用shell脚本自动打包上传 fir.im

    http://blog.csdn.net/wang631106979/article/details/52299083

  5. kubesphere集群节点扩容

    原有的节点是 : master[123] , node[1234] 新加的节点node5 一.修改配置文件hosts.ini [root@master0 ~]# /conf/hosts.ini [al ...

  6. busybox介绍

    BusyBox 是一个集成了一百多个最常用linux命令和工具的软件.BusyBox 将许多具有共性的小版本的UNIX工具结合到一个单一的可执行文件.这样的集合可以替代大部分常用工具比如的GNU fi ...

  7. PAT Basic 1006 换个格式输出整数 (15 分)

    让我们用字母 B 来表示“百”.字母 S 表示“十”,用 12...n 来表示不为零的个位数字 n(<),换个格式来输出任一个不超过 3 位的正整数.例如 234 应该被输出为 BBSSS123 ...

  8. php和redis实现消息队列

    php+redis消息队列是php+mysql性能不足时的一个中间间处理方案.通过这个中间的处理,保证的数据的可用性和准确性.用于服务器瞬间请求大,数据库压力大的情况.如并发量大导致的超卖.并发量大导 ...

  9. eclipse springboot 官网demo启动 SpringApplication类找不到

    网上有很多类似的,我这种情况是:maven的问题,,, 我自己下载了maven并集成到了eclipse上,导致java.lang.NoClassDefFoundError: org/springfra ...

  10. python file对象测试数据的读写操作及OS模块介绍(四)

    import   from....import 引入模块 引入类 ①import 如果文件在lib下而且是python模块 :import 模块名. ②from....import from 包名.包 ...