MyBatis学习笔记(三)——parameterType为基本类型时的使用方法

当mapper中的parametType为基本类型(如int,string等)时,是怎样使用的

最简单的使用方法:

	<select id="list" parameterType="string"  resultMap="ClassroomResultMap">
		select id, name
		from bc
		where name = #{name}
	</select>

这里的参数#{}中写什么变量名都可以,mybatis会自动给赋值。而当使用if语句时,比如

	<select id="list" parameterType="string"  resultMap="ClassroomResultMap">
		select id, name
		from bc
		<where>
			<if test="name != null and <span style="font-family: Arial, Helvetica, sans-serif;">name</span> != ''">
				name like CONCAT('%','${name}','%')
			</if>
		</where>
	</select>

会报错

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'name' in 'class java.lang.String'

原因:

mybatis自动调用OGNL寻找String的name属性

解决办法:

1、使用_parameter

<select id="list" parameterType="string"  resultMap="ClassroomResultMap">
   select id, name
   from bc
   <where>
      <if test="_parameter != null and _parameter != ''">
         name like CONCAT('%','${name}','%')
      </if>
   </where>
</select>
2、使用mybatis默认的对象名:value

<select id="list" parameterType="string"  resultMap="ClassroomResultMap">
   select id, name
   from bc
   <where>
      <if test="value != null and value != ''">
         name like CONCAT('%','${value}','%')
      </if>
   </where>
</select>








  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值