原博客网址:
http://blog.csdn.net/qq_28885149/article/details/52211999
当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 >