改造iBatis,使其支持自动生成sql语句(2)

阅读更多

改造iBatis,使其支持自动生成sql语句(2)

  上篇讲到基本思路和代码修改的切入点。这篇继续讲实现的细节的demo。

  在生成iql语句时,generate前面和后面的iql语句给予保留,充分保留最大的灵活性。

  在生成iql语句时候后,在调用insert和update时,转入的parametarObject必须是parametermap规定的Object,在select和delete时,如果parameter只有1个,则会把iql变量替换成#value#。

 sql-map 代码

  1. xml version="1.0" encoding="UTF-8"?>  
  2.     "http://ibatis.apache.org/dtd/sql-map-2.dtd">  
  3.   
  4. <sqlMap namespace="sort">  
  5.   <typeAlias alias="Sort" type="com.cpcw.product.domain.Sort"/>  
  6.   
  7.   <parameterMap id="SortParameterMap" class="Sort">  
  8.     <parameter property="name" column="name" jdbcType="VC"/>  
  9.     <parameter property="fid" column="fid" jdbcType="INTEGER"/>  
  10.     <parameter property="status" column="status" jdbcType="INTEGER"/>  
  11.     <parameter property="notes" column="notes" jdbcType="VC"/>  
  12.     <parameter property="created" column="created" jdbcType="TIMESTAMP"/>  
  13.   parameterMap>  
  14.   
  15.   <parameterMap id="SortParameterFulMap" class="Sort" extends="SortParameterMap">  
  16.     <parameter property="id" column="id" jdbcType="INTEGER"/>  
  17.   
  18.   parameterMap>  
  19.   
  20.   <parameterMap id="SortParameterDeleteMap" class="Sort">  
  21.     <parameter property="id" column="id"/>  
  22.   parameterMap>  
  23.   
  24.   <select id="selectSort" parameterMap="sort.SortParameterFulMap" resultClass="Sort">  
  25.     select * from (   
  26.     <generate table="sort" where="id"/>  
  27.     ) a order by id desc limit 1   
  28.   select>  
  29.   
  30.   <insert id="createSort" parameterMap="sort.SortParameterMap">  
  31.     <selectKey keyProperty="id" resultClass="int" type="post">  
  32.       select last_insert_id() as value   
  33.     selectKey>  
  34.     <generate table="sort"/>  
  35.   insert>  
  36.   <update id="updateSort" parameterMap="sort.SortParameterFulMap">  
  37.     <generate where="id" table="sort" excludes="{status,created}"/>  
  38.   update>  
  39.   
  40.   <delete id="deleteSort" parameterMap="sort.SortParameterDeleteMap">  
  41.     <generate table="sort" where="id"/>  
  42.   delete>  
  43.   
  44.   
  45. sqlMap>  

 

java 代码
  1. public Sort getSortById(int id) {   
  2.   return (Sort) queryForObject("sort.selectSort"new Integer(id));   
  3. }   
  4.   
  5. public int createSort(Sort sort) {   
  6.   insert("sort.createSort", sort);   
  7.   return sort.getId();   
  8. }   
  9.   
  10. public int updateSort(Sort sort) {   
  11.   return update("sort.updateSort", sort);   
  12. }   
  13.   
  14. public int deleteSort(int id) {   
  15.   return delete("sort.deleteSort"new Integer(id));   
  16. }  

 

生成的IQL 代码
  1.   select:
  2.   select * from (   
  3.     select name as name, fid as fid, status as status, notes as notes, created as created, id as id from sort where id = #value#   
  4.     ) a order by id desc limit 1   
  5.      
  6. inesrt:       
  7.     insert into sort (name, fid, status, notes, created) values (#name:VC#, #fid:INTEGER#, #status:INTEGER#, #notes:VC#, #created:TIMESTAMP#)   
  8.      
  9. update:
  10.     update sort set name = #name:VC#, fid = #fid:INTEGER#, notes = #notes:VC#, id = #id:INTEGERwhere id = #id:INTEGER#   
  11. delete
  12.     delete from sort where id = #value#  

 原创文章,如果要转载请注明出处、原始地址和作者信息。 

  • ibatis-2.3.0.677.hack.zip (333.9 KB)
  • 描述: hack过的ibatis jar文件,包含dtd文件
  • 下载次数: 139

你可能感兴趣的:(iBATIS,SQL,VC++,Apache,XML)