出现错误的代码:
想测试的方法:
public List findUsersByName(String name);
在xml文件中编写的代码
<resultMap type="user3" id="userMap">
<id property="id" column="id" />
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap>
<sql id="userCols">
id, name, age
</sql>
<select id="findUsersByName" resultMap="userMap">
select <include refid="userCols"/> from user
<where>
<if test="name != null and name != ''">
and name like concat(#{name}, '%')
</if>
</where>
</select>
本想借此测试一下mybatis中的动态SQL,结果出现这种错误,先给出两种解决方案
1.在接口中给方法加上一个注解(推荐使用)
public List findUsersByName(@Param(“name”) String name);
2.在if语句的test中把name改成_parameter(仅传入一个类型为String的参数,那么在 xml文件中应该使用_parameter来代替参数名)
<where>
<if test="_parameter != null and _parameter != ''">
and name like concat(#{name}, '%')
</if>
</where>
以上就是我在网上收集的两种解决方法。