_parameter:解决There is no getter for property named in class java.lang.String
我们知道在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的更多相关文章
- 【MyBatis学习06】_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
问题: 执行查询时报错:There is no getter for property named '*' in 'class java.lang.String 原因: 传过去的参数为识别.本例为 p ...
- 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 ...
- 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] ...
- 【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 ...
- Java 异常 Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date'
查询时发送给服务器的日期的字符串格式:yyyy-MM-dd HH:mm:ss 服务器接收到日期的字符串之后,向 MySQL 数据库发起查询时,因为没有指定日期时间格式,导致字符串数据不能正确地转换为日 ...
- 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 = " ...
随机推荐
- 服务器部署Java Web及微信开发调试
参考摘抄: 阿里云部署Java网站和微信开发调试心得技巧(上):https://www.imooc.com/article/20583 阿里云部署Java网站和微信开发调试心得技巧(下):https: ...
- AWS In Action
Core Services of AWS Elastic Cloud Compute(EC2) Simple Storage Service(S3) Relational Database Servi ...
- const与constexpr
关于const型数据,谭浩强老爷子这么总结道: Time const t; //t是常对象,其值在任何情况下都不能改变 void Time::fun()const; //fun是Time类中的常成员函 ...
- Hdu第八场 树形dp+基环树
Card Game 每个牌背面的数字朝正面的数字连一条有向边 则题目变为问你最少翻转多少次 能使得每个数字的入度不超过1 首先判断图中每个连通块是不是树或者基环树 因为只有树或者基环树能使得每个点的入 ...
- java读写大文件
java读写2G以上的大文件(推荐使用以下方法) static String sourceFilePath = "H:\\DataSource-ready\\question.json&qu ...
- Python核心技术与实战——二十|assert的合理利用
我们平时在看代码的时候,或多或少会看到过assert的存在,并且在有些code review也可以通过增加assert来使代码更加健壮.但是即便如此,assert还是很容易被人忽略,可是这个很不起眼的 ...
- 自定义前端框架(VUE+magicbox响应式风格)
1.用脚手架初始化一个vue项目swain $ vue create swain 2.安装几个常用插件
- 使用Vue自定义组件时,报did you register the component correctly? For recursive components, make sure to provide the "name" option.(未注册组件)的原因之一
错误信息: [Vue warn]: Unknown custom element: <list> - did you register the component correctly? F ...
- Redis的部署使用文档
Redis的部署使用文档 简述: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符 串).list(链表).set( ...
- linux下使用 TC 对服务器进行流量控制
tc 介绍 在linux中,tc 有二种控制方法 CBQ 和 HTB.HTB 是设计用来替换 CBQ 的.HTB比CBQ更加灵活,但是CPU 开销也更大,通常高速的链路会使用CBQ,一般而言HTB使用 ...