Ibatis传递多个参数

1) 用String代替

Java代码

  1. String sql = "uid = '" + username + "' and pwd='" + password + "'";  
  2. Integer r = (Integer) sqlMap.queryForObject("checkLogin", sql);  

 

Xml代码

  1.   
  2.      SELECT count(*) AS value FROM userinfo WHERE $sql$  
  3.   

 

2)用 Map

Java代码

  1. Map map=new HashMap();  
  2. map.put("uid", username);  
  3. map.put("pwd", password);  
  4. Integer r = (Integer) sqlMap.queryForObject("checkLogin2", map);  

  一种写法:

Xml代码  

  1.   
  2.        SELECT count(*) AS value FROM userinfo WHERE uid=#uid# and pwd=#pwd#  
  3.   

  另一种写法

Xml代码  

  1.         
  2.             
  3.        
  4.       
  5.         
  6.      SELECT count(*) AS value FROM userinfo WHERE uid=? and pwd=?  
  7.    

3)用bean

Java代码  

  1. public Student queryStudentsByNameAndId(HashMap hashMap)      {           
  2.   
  3.  Student s=null;            
  4. try {               
  5.       s=(Student)sqlMapClient.queryForObject("selectStudentByIdAndName",hashMap);                         
  6.  } catch (SQLException e) {                 
  7.       e.printStackTrace();           
  8.  }           
  9.       return s;       
  10.  }     

 

Xml代码  

  1.         
  2.               
  3.          
  4.        
  5.   
  6.         
  7.        select * from student where  sid=? and sname=?    
  8.    

你可能感兴趣的:(mybatis)