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 閱讀(3380) 評論(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 | 團派家園

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


          網站導航:
           
          主站蜘蛛池模板: 博白县| 普兰店市| 澎湖县| 钦州市| 湖口县| 赤水市| 玉溪市| 杭锦旗| 敦化市| 太湖县| 太谷县| 平山县| 墨脱县| 云浮市| 高密市| 宝丰县| 林西县| 微博| 郓城县| 皋兰县| 原阳县| 千阳县| 绥化市| 嘉峪关市| 哈尔滨市| 临汾市| 舞阳县| 张家川| 嘉定区| 临桂县| 马关县| 神池县| 怀安县| 万安县| 青州市| 颍上县| 济阳县| 外汇| 五峰| 梓潼县| 襄城县|