黑马传智健康项目|day2|mybatis自动分页传参问题|变量名为value?

该项目day2中,检查项分页的映射文件设置如下:

<select id="selectByCondition" parameterType="string" 
        resultType="com.itheima.pojo.CheckItem">
  select * from t_checkitem
  <if test="value != null and value.length > 0">
    where code = #{value} or name = #{value}
  </if>
</select>

为什么parameterType="string"时,下面的变量名为value?

解释:

一句话解释:这是mybatis的规定,传入String类型时,参数写string,下面变量名写value。

对于对象类型的变量,mybatis会去传入的参数类型的类里面去找相应的getter,也就是说传入参数为string时,mybatis会拿着程序员设定的变量名去String类里面去找相应的getter——如果设定的变量名为ok,mybatis就会去String里找ok的getter,这显然是没有的。

除此之外,还有两种方法将string的参数传入,一是使用_parameter作为参数名,二是在接口参数里加上mybatis中的@param注解。注意,方法二中导入的是mybatis的包(不过路径中写作ibatis)。

方法一的例:

<select id="selectByCondition" parameterType="string" 
        resultType="com.itheima.pojo.CheckItem">
  select * from t_checkitem
  <if test="_parameter != null and _parameter.length > 0">
    where code = #{_parameter} or name = #{_parameter}
  </if>
</select>

方法二的例:

public Page<CheckItem> selectByCondition(@Param("ok")String queryString);
<select id="selectByCondition" parameterType="string"
        resultType="com.itheima.pojo.CheckItem">
    select * from t_checkitem
    <if test="ok != null and ok.length > 0">
        where code = #{ok} or name = #{ok}
    </if>
</select>

题外话:

在mybatis3.5.9(写作时最新版)的文档中说明,

鉴于参数类型(parameterType)会被自动设置为 int,这个参数可以随意命名。原始类型或简单数据类型(比如 IntegerString)因为没有其它属性,会用它们的值来作为参数。

也就是说传入参数为int时,可以随意命名,而简单数据类型没有提到命名规则。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值