MyBatis——parameterType为基本类型时的使用方法

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

最简单的使用方法:

[html]  view plain  copy
  1. <select id="list" parameterType="string"  resultMap="ClassroomResultMap">  
  2.     select id, name  
  3.     from bc  
  4.     where name = #{name}  
  5. select>  

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

[html]  view plain  copy
  1. <select id="list" parameterType="string"  resultMap="ClassroomResultMap">  
  2.     select id, name  
  3.     from bc  
  4.     <where>  
  5.         <if test="name != null and <span style="font-family: Arial, Helvetica, sans-serif;">namespan> != ''">  
  6.             name like CONCAT('%','${name}','%')  
  7.         if>  
  8.     where>  
  9. select>  

会报错

[plain]  view plain  copy
  1. 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

[html]  view plain  copy
  1. <select id="list" parameterType="string"  resultMap="ClassroomResultMap">  
  2.    select id, name  
  3.    from bc  
  4.    <where>  
  5.       <if test="_parameter != null and _parameter != ''">  
  6.          name like CONCAT('%','${name}','%')  
  7.       if>  
  8.    where>  
  9. select>  
2、使用mybatis默认的对象名:value

[html]  view plain  copy
  1. <select id="list" parameterType="string"  resultMap="ClassroomResultMap">  
  2.    select id, name  
  3.    from bc  
  4.    <where>  
  5.       <if test="value != null and value != ''">  
  6.          name like CONCAT('%','${value}','%')  
  7.       if>  
  8.    where>  
  9. select>  

你可能感兴趣的:(Mybatis)