现在我们有一个dao层接口方法,
public Class findAllByName(String name);
那么对应Mapper.xml文件
<select id="getAllByName" parameterType="string" resultType="Class">
select * from class
<where>
<if test="name != null">
f_name like #{name}"%"
</if>
</where>
</select>
一般来说,我们这样写对于其他类型来说是没有错的。但是如果传入参数为String的话就会抛出异常:
There is no getter for property named 'name' in 'class java.lang.Sting'
因为MyBatis要求如果参数为String的话,不管接口方法的形参是什么,在Mapper.xml中引用时需要改变为_parameter才能被识别:
<select id="getAllByName" parameterType="string" resultType="Class">
select * from class
<where>
<if test="_parameter != null">
f_name like #{_parameter}"%"
</if>
</where>
</select>
记住:这里的传入参数的两个都要修改为_paramter。 且传入参数有且仅有一个String的参数时才会有这种情况。好比说下面的这种情况你传入的是两个Sring参数,那么也就没有这种情况发生。
<select id="getAllByName" resultType="Class">
select * from class
<where>
<if test="param1 != null">
f_name like #{param1}"%"
</if>
<if test="param2 != null">
f_teacherName like #{param2}"%"
</if>
</where>
</select>