`

Ibatis问题总结

阅读更多

1、有关parameterMap的使用

例子

sqlmap.xml中的配置(此处用的参数是HashMap类型)如下:

 

xml 代码

 

  1. <select id="selectBySexExceptSelf" parameterMap="forSexParam" resultMap="productResult">  
  2.       
  3.         select    
  4.         id,   
  5.         proname,   
  6.         pronum,   
  7.         proclick,   
  8.         probul,   
  9.         profee,   
  10.         prosex,   
  11.         procontent,   
  12.         procolor,   
  13.         prochic,   
  14.         prochicsm,   
  15.         promot,   
  16.         propic,   
  17.         prochange,   
  18.         proback,   
  19.         prouptime,   
  20.         proispush,   
  21.         proisnew,   
  22.         proison,   
  23.         prophoto,   
  24.         protoptime,   
  25.         proisremove,   
  26.         proremovetime,   
  27.         promodifytime,   
  28.         proisdis,   
  29.         prodisfee,   
  30.         proType   
  31.         from product   
  32.         <dynamic prepend="WHERE">  
  33.             <isParameterPresent prepend="and">  
  34.                 id != #id#   
  35.             isParameterPresent>  
  36.             <isParameterPresent prepend="and">  
  37.                 prosex = #prosex#   
  38.             isParameterPresent>  
  39.         dynamic>  
  40. select>  

         
 

 

 

java调用代码:

java 代码
  1. public List  getProductListBySexExceptSelf(Integer id,String sex) {   
  2.     HashMap hmParam = new HashMap();   
  3.     hmParam.put("id", id);   
  4.     hmParam.put("prosex", sex);   
  5.     return getSqlMapClientTemplate().queryForList(Product.class.getName() + ".selectBySexExceptSelf", hmParam);   
  6. }  

请不要被很多网页中的问号的形式误导,这种写法将直接报错:The error occurred while applying a parameter map. 

  1. <dynamic prepend="WHERE">  
  2.     <isParameterPresent prepend="and">  
  3.         id != ?   
  4.     isParameterPresent>  
  5.     <isParameterPresent prepend="and">  
  6.         prosex = ?   
  7.     isParameterPresent>  
  8. dynamic>  
xml 代码

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics