mybatis对传入基本类型参数的判断方式有哪些

发布时间:2022-03-14 09:16:58 作者:小新
来源:亿速云 阅读:347

这篇文章主要介绍mybatis对传入基本类型参数的判断方式有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

对传入基本类型参数的判断

mybatis的xml文件的sql语句中parameterType为基本类型,如:

<select id="getCustomer" parameterType="Integer" resultType="Customer">
    select * from customer
    where
    <if test="id != null">id=#{id}</if>
<select>

会报错:There is no getter for property named 'id' in 'class java.lang.Integer'

这是因为Integer对象中没有id属性

解决办法

<select id="getCustomer" parameterType="Integer" resultType="Customer">
    select * from Customer
    where
    <if test="_parameter != null">id=#{_parameter}</if>
<select>

即将接收参数的参数名改为_parameter,注意改成其他参数名没用。

传入基本类型参数时test判断报错

在使用mybatis的时候出现了这样的问题:

//Dao层的接口中的代码
List<Map<String,Object>> getName(String username);
//对应的mapper中的代码
<select id="getName" resultType="java.util.Map">
    select name,client_id
    from table1
    <where> 
        <if test=" username!= null and username!='' ">
            and username= #{id}
        </if>
    </where> 
</select>

//报的错误
org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: 
There is no getter for property named 'username' in 'class java.lang.String'

分析

There is no getter for property named &lsquo;username&rsquo; in &lsquo;class java.lang.String&rsquo;,这句话打大概意思是:在“class java.lang.String”中没有名为“username”的属性的getter方法。因为mybatis默认采用ONGL解析参数,所以会自动采用对象树的形式取string.num值,引起错误。

解决办法

if test中的id用_parameter替换,而实际的语句不需要修改and a.id =#{id},因为Mybatis当只传入一个参数时#{ } 中的内容没有要求。

在Mapper中给出入参设置名称,例:public &hellip; getName(@Param(“username”) String username);这样修改后我们前面的写法就不会报错了。

以上是“mybatis对传入基本类型参数的判断方式有哪些”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. Mybatis如何传入多个参数
  2. Mybatis如何传入多个参数的实现代码

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis

上一篇:怎么使用IDEA查看java文件编译后的字节码内容

下一篇:SpringBoot JPA出现No identifier specified for en错误怎么办

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》