<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    badqiu

    XPer
    隨筆 - 46, 文章 - 3, 評論 - 195, 引用 - 0
    數據加載中……

    ibatis3 實例代碼下載兼ibatis3優劣分析

    作為rapid-framework路線圖的一部分,集成ibatis3也是以后要更新的內容之一.

    現編寫了ibatis3的代碼例子. 

     

    一.首先我們來看現在的xml mapper關于增刪改查的編寫

     

     

    Xml代碼 
    1. <?xml version="1.0" encoding="UTF-8" ?>  
    2. <!DOCTYPE mapper  
    3. PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"  
    4. "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">  
    5.   
    6. <!--   
    7.     該文件通過代碼生成器自動生成,只需編寫模板,可以生成任何代碼  
    8.      具請查看: http://code.google.com/p/rapid-framework/  
    9. -->  
    10. <mapper namespace="UserInfo">  
    11.   
    12.     <resultMap id="UserInfoResult" type="com.company.project.model.UserInfo">  
    13.     </resultMap>  
    14.       
    15.     <!-- 用于select查詢公用抽取的列 -->  
    16.     <sql id="commonColumns">  
    17.         <![CDATA[ 
    18.             user_id as userId, 
    19.             username as username, 
    20.             password as password, 
    21.             birth_date as birthDate, 
    22.             sex as sex, 
    23.             age as age 
    24.         ]]>  
    25.     </sql>  
    26.   
    27.     <!-- useGeneratedKeys="true" keyProperty="xxx" for sqlserver and mysql -->  
    28.     <insert id="insert" parameterType="com.company.project.model.UserInfo"   
    29.         useGeneratedKeys="true" keyProperty="userId"   
    30.     >  
    31.     <![CDATA[ 
    32.         INSERT INTO 
    33.         user_info ( 
    34.             user_id , 
    35.             username , 
    36.             password , 
    37.             birth_date , 
    38.             sex , 
    39.             age  
    40.         ) VALUES ( 
    41.             #{userId,jdbcType=BIGINT} , 
    42.             #{username,jdbcType=VARCHAR} , 
    43.             #{password,jdbcType=VARCHAR} , 
    44.             #{birthDate,jdbcType=DATE} , 
    45.             #{sex,jdbcType=TINYINT} , 
    46.             #{age,jdbcType=INTEGER}  
    47.         ) 
    48.     ]]>  
    49.         <!--   
    50.             oracle: order="BEFORE" SELECT sequenceName.nextval AS ID FROM DUAL   
    51.             DB2: order="BEFORE"" values nextval for sequenceName  
    52.         <selectKey resultType="java.lang.Long" order="BEFORE" keyProperty="userId">  
    53.             SELECT sequenceName.nextval AS ID FROM DUAL   
    54.         </selectKey>  
    55.         -->  
    56.     </insert>  
    57.       
    58.     <update id="update" parameterType="com.company.project.model.UserInfo">  
    59.     <![CDATA[ 
    60.         UPDATE user_info SET 
    61.             username = #{username,jdbcType=VARCHAR} , 
    62.             password = #{password,jdbcType=VARCHAR} , 
    63.             birth_date = #{birthDate,jdbcType=DATE} , 
    64.             sex = #{sex,jdbcType=TINYINT} , 
    65.             age = #{age,jdbcType=INTEGER}  
    66.         WHERE  
    67.             user_id = #{userId}  
    68.     ]]>  
    69.     </update>  
    70.   
    71.     <delete id="delete" parameterType="java.lang.Long">  
    72.     <![CDATA[ 
    73.         delete from user_info where 
    74.         user_id = #{id}  
    75.     ]]>  
    76.     </delete>  
    77.       
    78.     <select id="getById" parameterType="java.lang.Long" resultMap="UserInfoResult">  
    79.         select <include refid="commonColumns" />  
    80.         <![CDATA[ 
    81.             from user_info  
    82.             where  
    83.                 user_id = #{id}  
    84.         ]]>  
    85.     </select>  
    86.       
    87.     <sql id="dynamicWhere">  
    88.         <where>  
    89.            <if test="userId != null">  
    90.                 and user_id = #{userId}  
    91.             </if>  
    92.            <if test="username != null">  
    93.                 and username = #{username}  
    94.             </if>  
    95.         </where>  
    96.     </sql>  
    97.           
    98.     <select id="count" resultType="long">  
    99.         select count(*) from user_info   
    100.         <include refid="dynamicWhere"/>      
    101.     </select>  
    102.       
    103.     <select id="pageSelect" resultMap="UserInfoResult">  
    104.         select <include refid="commonColumns" />  
    105.         from user_info   
    106.         <include refid="dynamicWhere"/>  
    107.         <if test="sortColumns != null and sortColumns.length() != 0">  
    108.             ORDER BY ${sortColumns}  
    109.         </if>  
    110.     </select>  
    111.   
    112.       
    113. </mapper>  

     

     

    與ibatis2 sqlmap的主要異同:

    1. insert節點現在可以直接指定mysql auto_increment(或是sqlserver identity)的主鍵生成策略

      useGeneratedKeys="true" keyProperty="userId" 

     

    2. insert及update語句對于null字段,beta3現肯定要指定jdbcType,而且現在是只能在表達式里面指定,不知道ibatis3的大佬是如何想的,如下面

      #{userId,jdbcType=BIGINT}  

     還有其它可以配置,如: #{age,javaType=int,jdbcType=NUMERIC,typeHandler=typeHandler}

     

    3.動態構造sql部分,test部分采用的是struts2 ognl表達式,還有choose,foreach語句等,跟struts2 tag是否很像呢?

      不過讓人郁悶是的, 判斷一個字符串非空竟然要寫這么長的語句,那位同學知道精簡的麻煩告訴我一下,懷念ibatis2的<isNotEmpty>: 

    sortColumns != null and sortColumns.length() != 0

     

    二.構造SqlSessionFactory,以前的SqlMapClient

     

    Java代碼 
    1. Reader reader = Resources.getResourceAsReader("Configuration.xml");  
    2. SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);  
    3. SqlSession session = sessionFactory.openSession();  

     

    三. 配置文件Configuration.xml

     

    Java代碼 
    1. <?xml version="1.0" encoding="UTF-8"?>  
    2. <!DOCTYPE configuration  
    3. PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  
    4. "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">  
    5. <configuration>  
    6.     <environments default="development">  
    7.         <environment id="development">  
    8.             <transactionManager type="JDBC" />  
    9.             <dataSource type="POOLED">  
    10.                 <property name="driver" value="com.mysql.jdbc.Driver" />  
    11.                 <property name="url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8" />  
    12.                 <property name="username" value="root" />  
    13.                 <property name="password" value="123456" />  
    14.             </dataSource>  
    15.         </environment>  
    16.     </environments>  
    17.     <mappers>  
    18.         <mapper resource="com/company/project/model/mapper/UserInfoMapper.xml" />  
    19.     </mappers>  
    20. </configuration>  

     

    以上就是完整的示例, 具體demo代碼下載:  http://rapid-framework.googlecode.com/files/ibatis3_demo.zip

     

     

    ibatis3 annotation評價:

    難聽點,根本是個腦殘方案,如果用annotation寫,我還不如使用類似jdbc的java代碼. 不知道自己優勢是在xml文件,瞎跟風.

     

    而spring現在還沒有對ibatis3集成,不過以后rapid會先與spring發布ibatis3的插件, 只提供生成器模板,現不自己開發集成,等待spring. 當然以后對提供類似ibatis2的基于方言Dialect的分頁還是會提供的.

     

     

    最后仍然做下廣告: rapid-framework, 現最好的項目腳手架

    http://code.google.com/p/rapid-framework/

     

    posted on 2009-09-27 11:17 badqiu 閱讀(3378) 評論(2)  編輯  收藏

    評論

    # re: ibatis3 實例代碼下載兼ibatis3優劣分析  回復  更多評論   

    俺也在學習這個,感覺比2好用多了,越來越像hibernate了
    請教lz一個問題:在insert的時候,主鍵總是null,(我用的oracle)
    <insert id="insertTest" parameterType="user" >
    <selectKey keyProperty="userId" resultType="java.lang.Long" order="BEFORE">
    select seq_data.nextval as userId from dual
    </selectKey>
    <![CDATA[ insert into usertest (user_id,user_name)values(#{userId,jdbcType=BIGINT},#{name,jdbcType=VARCHAR})]]>

    </insert>

    能幫忙解決一下嗎?以下是打印的sql,主鍵類型為null
    329 main DEBUG java.sql.ResultSet <== Columns: [USERID]
    329 main DEBUG java.sql.ResultSet <== Columns: [USERID]
    329 main DEBUG java.sql.ResultSet <== Row: [8772]
    329 main DEBUG java.sql.ResultSet <== Row: [8772]
    329 main DEBUG java.sql.PreparedStatement ==> Executing: insert into usertest (user_id,user_name)values(?,?)
    329 main DEBUG java.sql.PreparedStatement ==> Executing: insert into usertest (user_id,user_name)values(?,?)
    329 main DEBUG java.sql.PreparedStatement ==> Parameter Types: [null, java.lang.String]
    329 main DEBUG java.sql.PreparedStatement ==> Parameter Types: [null, java.lang.String]
    329 main DEBUG java.sql.PreparedStatement ==> Parameters: [null, adbcdddddddddddd]
    329 main DEBUG java.sql.PreparedStatement ==> Parameters: [null, adbcdddddddddddd]
    2009-09-27 12:36 | suo

    # re: ibatis3 實例代碼下載兼ibatis3優劣分析  回復  更多評論   

    也在學習這個,感覺比2好用多了,越來越像hibernate了
    2009-12-17 13:57 | 團派家園

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 久久久久久久岛国免费播放| 亚洲最大福利视频网站| 国产成人免费午夜在线观看| 一级毛片试看60分钟免费播放| 亚洲伦理一二三四| 久久精品国产亚洲av麻豆| 免费国产成人午夜电影| 丁香花免费完整高清观看| 特级做A爰片毛片免费看无码| 香港一级毛片免费看| 亚洲人成网站999久久久综合| 久久久久亚洲av无码专区导航| 亚洲VA中文字幕无码一二三区| 亚洲国产高清在线一区二区三区| 免费黄色app网站| 亚洲免费福利在线视频| 1000部啪啪未满十八勿入免费| 成在线人视频免费视频| yy一级毛片免费视频| 美女被爆羞羞网站免费| 精品韩国亚洲av无码不卡区| 亚洲中文字幕乱码一区| 亚洲国产精品久久网午夜| 亚洲精品美女在线观看| 久久久无码精品亚洲日韩蜜臀浪潮| 亚洲av无码专区国产乱码在线观看 | 国产精品亚洲lv粉色| 亚洲精品无播放器在线播放| 亚洲人成色777777老人头| 亚洲精品伊人久久久久| 亚洲一级免费视频| 67194在线午夜亚洲| 99999久久久久久亚洲| 亚洲看片无码在线视频| 亚洲色少妇熟女11p| 亚洲影院天堂中文av色| 亚洲国产成人综合精品| 青青草97国产精品免费观看| 久久久久女教师免费一区| a级在线观看免费| 一区二区免费视频|