mybatis 使用参数

 

Parameter
 
1.   传入简单类型java


    JAVA代码:apache

Java代码   收藏代码
  1. public User get(Long id) {    
  2.     return (User) getSqlSession().selectOne("com.liulanghan.get" , id);  
  3. }  

 
 MAPPER :数组

 

Xml代码   收藏代码
  1. <select id="findUserListByIdList" parameterType="java.lang.Long" resultType="User">  
  2.         select * from user where  id = #{id};  
  3. </select>  

  
2.   传入List安全

 

    JAVA代码:less

 

Java代码   收藏代码
  1. public List<Area> findUserListByIdList(List<Long> idList) {  
  2.         return getSqlSession().selectList("com.liulanghan.findUserListByIdList", idList);  
  3.     }  

 

MAPPER :spa

 

 

Xml代码   收藏代码
  1.    <select id="findUserListByIdList" parameterType="java.util.ArrayList" resultType="User">  
  2.     select * from user user  
  3.     <where>  
  4.         user.ID in (  
  5.         <foreach item="guard" index="index" collection="list"  
  6.             separator=","> #{guard} </foreach>  
  7.         )  
  8.     </where>  
  9. </select>   

   
 
 单独传入list时,foreach中的collection必须是list,不无论变量的具体名称是什么。好比这里变量名为idList,
 collection倒是是list。
 
3.  传入数组
code


  JAVA代码:xml

 

Java代码   收藏代码
  1. public List<Area> findUserListByIdList(int[] ids) {  
  2.         return getSqlSession().selectList("com.liulanghan.findUserListByIdList", ids);  
  3.     }  

 

 MAPPER :对象

 

 

Xml代码   收藏代码
  1. <select id="findUserListByIdList" parameterType="java.util.HashList" resultType="User">  
  2.     select * from user user  
  3.     <where>  
  4.         user.ID in (  
  5.         <foreach item="guard" index="index" collection="array"  
  6.             separator=","> #{guard} </foreach>  
  7.         )  
  8.     </where>  
  9. </select>     

  
 
 单独传入数组时,foreach中的collection必须是array,不无论变量的具体名称是什么。好比这里变量名为ids,
 collection倒是是array
字符串

 

4.  传入map
 
 JAVA代码:

Java代码   收藏代码
  1. public boolean exists(Map<String, Object> map){  
  2.         Object count = getSqlSession().selectOne("com.liulanghan.exists", map);  
  3.         int totalCount = Integer.parseInt(count.toString());  
  4.         return totalCount > 0 ? true : false;  
  5.     }  

  
 MAPPER :

Xml代码   收藏代码
  1. <select id="exists" parameterType="java.util.HashMap" resultType="java.lang.Integer">  
  2.         SELECT COUNT(*) FROM USER user  
  3.         <where>  
  4.             <if test="code != null">   
  5.                 and user.CODE = #{code}   
  6.             </if>  
  7.             <if test="id != null">   
  8.                 and user.ID = #{id}   
  9.             </if>  
  10.             <if test="idList !=null ">  
  11.                 and user.ID in (  
  12.                 <foreach item="guard" index="index" collection="idList"  
  13.                     separator=","> #{guard} </foreach>  
  14.                 )  
  15.             </if>  
  16.         </where>  
  17.     </select>  

 

 MAP中有list或array时,foreach中的collection必须是具体list或array的变量名。好比这里MAP含有一个
    名为idList的list,因此MAP中用idList取值,这点和单独传list或array时不太同样。
 
 
5. 传入JAVA对象
 
 JAVA代码:

Java代码   收藏代码
  1. public boolean findUserListByDTO(UserDTO userDTO){  
  2.         Object count = getSqlSession().selectOne("com.liulanghan.exists", userDTO);  
  3.         int totalCount = Integer.parseInt(count.toString());  
  4.         return totalCount > 0 ? true : false;  
  5.     }  

  
 MAPPER :

Xml代码   收藏代码
  1. <select id="findUserListByDTO" parameterType="UserDTO" resultType="java.lang.Integer">  
  2.         SELECT COUNT(*) FROM USER user  
  3.         <where>  
  4.             <if test="code != null">   
  5.                 and user.CODE = #{code}   
  6.             </if>  
  7.             <if test="id != null">   
  8.                 and user.ID = #{id}   
  9.             </if>  
  10.             <if test="idList !=null ">  
  11.                 and user.ID in (  
  12.                 <foreach item="guard" index="index" collection="idList"  
  13.                     separator=","> #{guard} </foreach>  
  14.                 )  
  15.             </if>  
  16.         </where>  
  17.     </select>  

 

    JAVA对象中有list或array时,foreach中的collection必须是具体list或array的变量名。好比这里UserDTO含有一个
    名为idList的list,因此UserDTO中用idList取值,这点和单独传list或array时不太同样。

 

6.取值


 由上面能够看出,取值的时候用的是#{}。它具体的意思是告诉MyBatis建立一个预处理语句参数。
 使用JDBC,这样的一个参数在SQL中会由一个“?”来标识,并被传递到一个新的预处理语句中,就像这样:

 

Java代码   收藏代码
  1. // Similar JDBC code, NOT MyBatis…  
  2. String selectPerson = “SELECT * FROM PERSON WHERE ID=?”;  
  3. PreparedStatement ps = conn.prepareStatement(selectPerson);  
  4. ps.setInt(1,id);  

  
    能够看到这个写法比较简单,MyBatis为咱们作了不少默认的事情,具体的写法应该以下:

 

Xml代码   收藏代码
  1. #{property,javaType=int,jdbcType=NUMERIC,typeHandler=MyTypeHandler,mode=OUT,resultMap=User}  

 

 property:属性名,即代码传入的变量名。
 javaType:该字段在JAVA中的类型,好比int。
 jdbcType:该字段在JDBC中的类型,好比NUMERIC。
 typeHandler:类型处理器
 mode:参数类型为IN,OUT或INOUT参数
 resultMap:结果。
 
 还好,MyBatis比较体谅咱们,通常咱们只需写一个属性名便可,如#{id},其余的如javaType和typeHandlerMybatis
 会自动帮咱们填好。但是这样有时也会出问题,好比出现CLOB字段时。
 
 因为JAVA代码中的String类型对应的默认typeHandler为StringTypeHandler,当用String类型处理时,若是String长度超过必定长度,就会报以下错误:


 setString can only process strings of less than 32766 chararacters

 

 解决办法是指定该属性的typeHandler,以下:


 #{message,typeHandler=org.apache.ibatis.type.ClobTypeHandler}

 

 咱们也能够自定义typeHandler来处理须要的数据,具体这里详述。
 
 JDBC类型是仅仅须要对插入,更新和删除操做可能为空的列进行处理。这是JDBC的须要,而不是MyBatis的。通常不须要配置
 
 mode、resultMap通常不须要,在写存储过程时会用到,这里不详述。
 
7.字符串替换

 

 通常状况下,咱们采用#{}取值,产生预处理语句,可是有时咱们可能不但愿Mybatis来帮咱们预处理,好比ORDER BY时,能够
 采用以下写法:
 
 ORDER BY ${columnName}
 
 这里MyBatis不会修改或转义字符串。而是直接拼接到SQL字符串后面。
   重要:接受从用户输出的内容并提供给语句中不变的字符串,这样作是不安全的。这会致使潜在的SQL注入攻击,所以你  不该该容许用户输入这些字段,或者一般自行转义并检查。

相关文章
相关标签/搜索