废话连篇

最近做的一个项目,自己搭建ORM层采用的是Ibatis框架,使用中遇到些理解出错之处。
记录自己使用的一些心得。 


    1 用MAP向ibatis配置文件传参问题

    通常我们通过DAO调用配置文件时,加如下参数parameterClass="java.util.Map",在Dao中,我们调用map.put(key,value)方法,如下: 
  
Map parms = new HashMap();   
parms.put("startDate", example.getDatatime());   
parms.put("endDate", getLastDayOfMonth(example.getDatatime()));   
  
<select id="selectCountStatusByDate" resultClass="int" parameterClass="java.util.Map">  
 select count(*) from s_upload_log l where  l.datatime  between #startDate:VARCHAR# and #endDate:VARCHAR#   
</select>  
Map parms = new HashMap();
parms.put("startDate", example.getDatatime());
parms.put("endDate", getLastDayOfMonth(example.getDatatime()));

<select id="selectCountStatusByDate" resultClass="int" parameterClass="java.util.Map">
 select count(*) from s_upload_log l where  l.datatime  between #startDate:VARCHAR# and #endDate:VARCHAR#
</select>

  



   或者也可以用parameterClass="某个对象类型",Ibatis配置文件直接可以获取对象get方法的各个字段。

   但如果我们不用parameterClass="某个对象类型"的话,将该对象put到Map中,那对应的Ibatis配置文件用到的对象中的各个值,可以获取的到吗?答案是肯定的,用法是将map的key作为配置文件的前缀,同时不指名parameterClass,就OK了,代码如下:

SUploadLog record = new SUploadLog();    
Map map = new HashMap();   
map.put("upload", record);   
  
<update id="updateLogByTemplateId">  
    update S_UPLOAD_LOG   
    <dynamic prepend="set">  
      <isNotNull prepend="," property="upload.sname">  
        SNAME = #upload.sname:VARCHAR#   
      </isNotNull>  
      <isNotNull prepend="," property="upload.status">  
        STATUS = #upload.status:CHAR#   
      </isNotNull>  
    </dynamic>  
     where TEMPLATEID = #upload.templateid# and DATATIME=#upload.datatime#     
 </update>  
SUploadLog record = new SUploadLog(); 
Map map = new HashMap();
map.put("upload", record);

<update id="updateLogByTemplateId">
    update S_UPLOAD_LOG
    <dynamic prepend="set">
      <isNotNull prepend="," property="upload.sname">
        SNAME = #upload.sname:VARCHAR#
      </isNotNull>
      <isNotNull prepend="," property="upload.status">
        STATUS = #upload.status:CHAR#
      </isNotNull>
    </dynamic>
     where TEMPLATEID = #upload.templateid# and DATATIME=#upload.datatime#  
 </update>



  2 ##参数和$$参数的区别

  Ibatis的配置XML中,#parm#和$parm$ 是有区别的,例如:

<select id="SELECT-MENU" parameterClass="java.util.Map" resultMap="Result4Menu">  
 select * from S_FUNCTION where SPARENT=#id#      
</select>  
  
<select id="SELECT-MENU" parameterClass="java.util.Map" resultMap="Result4Menu">  
 select * from S_FUNCTION where SPARENT=$id$   
</select>  
<select id="SELECT-MENU" parameterClass="java.util.Map" resultMap="Result4Menu">
 select * from S_FUNCTION where SPARENT=#id#   
</select>

<select id="SELECT-MENU" parameterClass="java.util.Map" resultMap="Result4Menu">
 select * from S_FUNCTION where SPARENT=$id$
</select>



前者会做类型检查,而后者不会。


你可能感兴趣的:(java,DAO,框架,ibatis,orm)