问题:Ibatis中,由于parameterMap无法将传入的java属性转换为数据库的字段。查了Ibatis的文档,针对Ado.Net是存在column字段的,而针对java没有。搞不明白为什么针对java没有?

Ado.net的配置大致如下,参考:http://developer.51cto.com/art/200907/138508.htm

 
  
  1. ﹤parameterMap class="Employee" id="Employee_SelectParameterMap"﹥   
  2.   
  3.     ﹤parameter column="EmployeeID" property="EmployeeID" dbType="int" type="int" direction="Input"/﹥   
  4.   
  5.     ﹤parameter column="LastName" property="LastName" dbType="nvarchar" type="string" direction="Input"/﹥   
  6.   
  7.     ﹤parameter column="Country" property="Country" dbType="nvarchar" type="string" direction="Input"/﹥   
  8.   
  9. ﹤/parameterMap﹥  

Java的parameterMap 配置大致如下:【注意】其中的 $sort$ $dir$,防止ibatis添加"'"

 
  
  1. <parameterMap class="java.util.Map" id="paramQueryMap"> 
  2.     <parameter property="appId" /> 
  3.     <parameter property="listValue" /> 
  4.     <parameter property="sort" /> 
  5.     <parameter property="dir" /> 
  6.     <parameter property="begin" /> 
  7.     <parameter property="increment" /> 
  8. </parameterMap> 
  9.  
  10. <select id="getBlackList" resultMap="HummockBlackListResult" 
  11.     parameterMap="paramQueryMap"> 
  12.     SELECT 
  13.     <include refid="columns" /> 
  14.     <![CDATA[ 
  15.         FROM black_list  
  16.     ]]> 
  17.     <dynamic prepend="where"> 
  18.         <isNotEmpty prepend="and" property="appId"> 
  19.             app_Id = #appId# 
  20.         </isNotEmpty> 
  21.         <isNotEmpty prepend="and" property="listValue"> 
  22.             list_value = 
  23.             #listValue# 
  24.         </isNotEmpty> 
  25.         <isNotEmpty prepend="order by" property="sort"> 
  26.             $sort$ $dir$ 
  27.         </isNotEmpty> 
  28.     </dynamic> 
  29.     limit #begin#,#increment# 
  30. </select> 

自己写的转换操作的方法如下:

 
  
  1. public class PropertyColumnWrapper { 
  2.     private static Logger logger = LoggerFactory.getLogger(PropertyColumnWrapper.class); 
  3.  
  4.     public static String getColumn(String property) { 
  5.         if (StringUtils.isBlank(property)) { 
  6.             logger.warn("property should not null"); 
  7.             return null
  8.         } 
  9.          
  10.         StringBuffer buffer = new StringBuffer(property); 
  11.         for (int i = 0; i < buffer.length(); i++) { 
  12.             char c = buffer.charAt(i); 
  13.             if (c >= 'A' && c <= 'Z') { 
  14.                 //插入'_'之后,位置+1 
  15.                 buffer.insert(i++, '_'); 
  16.             } 
  17.         } 
  18.         return buffer.toString(); 
  19.     } 
  20.  
  21.     public static String getProperty(String column) { 
  22.         if (StringUtils.isBlank(column)) { 
  23.             logger.warn("column should not null"); 
  24.             return null
  25.         } 
  26.         return column.replaceAll("_"""); 
  27.     }