1.单个参数(除集合数组)
xml中参数名可以任意。
2.多个参数
- 可以使用
- 对Mapper类中方法的参数添加注解
JavaBean情况:
3.应用:项目中遇到的一个错误
注意:Mybatis默认采用OGNL解析参数,会造成如下代码出现错误:
List<Test> selectByName(String name);
<select id="selectByName" resultType="com.zzy.learn.po.Test">
select id, test_name testName, age, sex, other
from test
<where>
<if test="name != null and name != ''">
and test_name=#{name}
</if>
</where>
</select>
org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'name' in 'class java.lang.String'
原因:Mybatis默认采用OGNL解析参数,所以会自动采用对象树的形式取 string.xxx 值,如果没在在方法中定义,则会抛异常报错。
修改代码:
List<Test> selectByName(@Param("name") String name);
即可