Ibatis parameterMap converts Property to Column

Source: Internet
Author: User

Problem: In Ibatis, parameterMap cannot convert passed java attributes to database fields. I checked the Ibatis document. For Ado. Net, the column field exists, but not for java. I cannot understand why it is not applicable to java?

Ado.net configuration is roughly as follows, see: 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﹥  

The Java parameterMap configuration is roughly as follows: Note:$ Sort $ dir $,Prevent ibatis from adding"'"

 
 
  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> 

You can use the following methods to perform a self-written conversion:

 
 
  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 shocould not null ");
  7. Return null;
  8. }
  9. StringBuffer buffer = new StringBuffer (property );
  10. For (int I = 0; I <buffer. length (); I ++ ){
  11. Char c = buffer. charAt (I );
  12. If (c> = 'A' & c <= 'Z '){
  13. // After '_' is inserted, the position is + 1
  14. Buffer. insert (I ++ ,'_');
  15. }
  16. }
  17. Return buffer. toString ();
  18. }
  19.  
  20. Public static String getProperty (String column ){
  21. If (StringUtils. isBlank (column )){
  22. Logger. warn ("column shocould not null ");
  23. Return null;
  24. }
  25. Return column. replaceAll ("_","");
  26. }
  27. }

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.