乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      MyBatis的幾種批量操作

       instl 2015-09-08

      MyBatis中批量插入  

      方法一:

      <insert id="insertbatch" parameterType="java.util.List">

        <selectKey keyProperty="fetchTime" order="BEFORE"

        resultType="java.lang.String">

        SELECT CURRENT_TIMESTAMP()

        </selectKey>

        insert into kangaiduoyaodian ( depart1, depart2, product_name,

        generic_name, img, product_specification, unit,

        approval_certificate, manufacturer, marketPrice, vipPrice,

        website, fetch_time, productdesc ) values

        <foreach collection="list" item="item" index="index"

        separator=",">

        ( #{item.depart1}, #{item.depart2}, #{item.productName},

        #{item.genericName}, #{item.img},

        #{item.productSpecification}, #{item.unit},

        #{item.approvalCertificate}, #{item.manufacturer},

        #{item.marketprice}, #{item.vipprice}, #{item.website},

        #{fetchTime}, #{item.productdesc} )

        </foreach>

        </insert>

      方法二:

      <insert id="batchInsertB2B" parameterType="ArrayList">
      insert into xxxxtable(hkgs,hkgsjsda,office,asdf,ddd,ffff,supfullName,classtype,agent_type,remark)
      <foreach collection="list" item="item" index="index" separator="union all">
      select #{item.hkgs,jdbcType=VARCHAR},
      #{item.hkgsjsda,jdbcType=VARCHAR},
      #{item.office,jdbcType=VARCHAR},
      #{item.asdf,jdbcType=VARCHAR},
      #{item.ddd,jdbcType=VARCHAR},
      #{item.ffff,jdbcType=VARCHAR},
      #{item.supfullName,jdbcType=VARCHAR},0,0,
      #{item.remark,jdbcType=VARCHAR} from dual
      </foreach>
      </insert>

       

      可以考慮用union all來實現批量插入。
      例如:
      insert into XX_TABLE(XX,XX,XX)select 'xx','xx','xx' union all select 'xx','xx','xx' union all select 'xx','xx','xx' ...
      先拼裝好語句再動態(tài)傳入insert into XX_TABLE(XX,XX,XX)后面部分

       

      MyBatis中批量刪除

       

       

      <!-- 通過主鍵集合批量刪除記錄 -->

       

      <delete id="batchRemoveUserByPks" parameterType="java.util.List">

       

      DELETE FROM LD_USER WHERE ID in 

       

      <foreach item="item" index="index" collection="list" open="(" separator="," close=")">

       

      #{item}

       

      </foreach>

       

      </delete>

       

       

       

       

       

      MyBatis中in子句

      mybatis in 參數 使用方法

       

      1.只有一個參數

       

      參數的類型要聲明為List或Array

       

      Sql配置如下:

       

      <select id="selectProduct" resultMap="Map">

       

      SELECT *

       

      FROM PRODUCT

       

      WHERE PRODUCTNO IN

       

           <foreach item="productNo" index="index" collection="參數的類型List或array">

       

                  #{productNo}

       

          </foreach>

       

      </select>

       

      2.多個參數

       

      首先要將多個參數寫入同一個map,將map作為一個參數傳入mapper

       

      Sql配置如下:

       

      <select id="selectProduct" resultMap="Map">

       

      SELECT *

       

      FROM PRODUCT

       

      WHERE PRODUCTNO IN

       

           <foreach item="productNo" index="index" collection="map中集合參數的名稱">

       

                  #{productNo}

       

          </foreach>

       

      </select>

       

       MyBatis批量修改

       

       

       

       <update id="updateOrders" parameterType="java.util.List">
       update orders set state = '0' where no in
       <foreach collection="list" item="nos" open="(" separator="," close=")">
         #{nos}
       </foreach>
       </update>

       

       

       

       

       

      MyBatis的關于批量數據操作的體會

      1.  MyBatis的前身就是著名的Ibatis,不知何故脫離了Apache改名為MyBatis。
         MyBatis所說是輕量級的ORM框架,在網上看過一個測試報告,感覺相比于Hibernate來說,優(yōu)勢并不明顯。

      下面說一下比較有趣的現象,根據MyBatis的官方文檔,在獲得sqlSession時,它有為批量更新而專門準備的:

      session = sessionFactory.openSession();//用于普通update
      session = sessionFactory.openSession(ExecutorType.BATCH, true);//用于批量update

       一般來說,對MYSQL數據庫批量操作時速度取決于,是為每一個處理分別建立一個連接,還是為這一批處理一共建立一個連接。按MyBatis的手冊說明,選擇ExecutorType.BATCH意味著,獲得的sqlSession會批量執(zhí)行所有更新語句。不過我測試了一下,批量插入1000條數據,發(fā)覺ExecutorType.BATCH方式的效率居然比普通的方式差很多。我測試用的Mapper中的insert配置如下,再用for循環(huán)插入1000條記錄:

      復制代碼
      1 <insert id="insert" parameterType="sdc.mybatis.test.Student">
      2 <!-- WARNING - @mbggenerated This element is automatically generated by
      3 MyBatis Generator, do not modify. This element was generated on Mon May 09
      4 11:09:37 CST 2011. -->
      5 insert into student (id, name, sex,
      6 address, telephone, t_id
      7 )
      8 values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
      9 #{sex,jdbcType=VARCHAR},
      10 #{address,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, #{tId,jdbcType=INTEGER}
      11 )
      12 </insert>
      復制代碼

        

      1.  我不清楚原因在哪里, 就配置了MyBatis的log4j,想查看下日志。下載了log4j.jar和commons-logging.jar并配置到項目的類路徑,然后在代碼路徑下新建文件log4j.properties,內容如下:
        復制代碼
        log4j.rootLogger=DEBUG, stdout

        # SqlMap logging configuration...
        log4j.logger.com.ibatis=DEBUG
        log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
        log4j.logger.com.ibatis.sqlmap.engine.cache.CacheModel=DEBUG
        log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientImpl=DEBUG
        log4j.logger.com.ibatis.sqlmap.engine.builder.xml.SqlMapParser=DEBUG
        log4j.logger.com.ibatis.common.util.StopWatch=DEBUG
        log4j.logger.java.sql.Connection=DEBUG
        log4j.logger.java.sql.Statement=DEBUG
        log4j.logger.java.sql.PreparedStatement=DEBUG
        log4j.logger.java.sql.ResultSet=DEBUG

        # Console output...
        log4j.appender.stdout=org.apache.log4j.ConsoleAppender
        log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
        log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
        復制代碼
         然后再次測試普通的sqlSession,發(fā)現日志內容中雖然插入了1000條數據,但只新建了一次連接,最后又關閉了該連接(日志如下)。也就是說MyBatis中的普通sqlSession好像已經對批量插入默認是一次連接中完成,那么還提供ExecutorType.BATCH方式干什么,況且該方式好像效率也不行,或者是我使用ExecutorType.BATCH方式不對??
        復制代碼
        DEBUG [main] - Created connection 3502256.
        DEBUG [main] - ooo Connection Opened
        DEBUG [main] - ==> Executing: insert into student ( name, sex, address, telephone, t_id ) values ( ?, ?, ?, ?, ? )
        DEBUG [main] - ==> Parameters: 新人0(String), male(String), addr0(String), dd(String),3(Integer)
        DEBUG [main] - ==> Executing: insert into student ( name, sex, address, telephone, t_id ) values ( ?, ?, ?, ?, ? )
        DEBUG [main] - ==> Parameters: 新人1(String), male(String),
        ...............
        ...............
        DEBUG [main] - xxx Connection Closed
        DEBUG [main] - Returned connection 3502256 to pool.
        復制代碼
         
      2. 最后一點是關于數據庫批量插入時sql語句級的優(yōu)化,我特意測試了兩種方式,在StudentMapper中配置了兩種insert模式。第一種對應insert value1,insert value2,,,,;第二種對應insert values (value1, value2,....)。發(fā)現后者果然比前者快很多啊。下面是兩種insert模式,及測試結果對應圖: 
        復制代碼
        <!-- 在外部for循環(huán)調用一千次 -->
        <insert id="insert" parameterType="sdc.mybatis.test.Student">
        insert into student (id, name, sex,
        address, telephone, t_id
        )
        values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},
        #{sex,jdbcType=VARCHAR},
        #{address,jdbcType=VARCHAR}, #{telephone,jdbcType=VARCHAR}, #{tId,jdbcType=INTEGER}
        )
        </insert>
        <!-- 批量 ,傳入一個長度為1000的list -->
        <insert id="insertBatch">
        insert into student (
        <include refid="Base_Column_List"/> )
        values
        <foreach collection="list" item="item" index="index" separator=",">
        (null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId})
        </foreach>
        </insert>
        復制代碼
         

      附錄:

      1. MyBatis配置文件的DTD文件(與Ibatis3不同):http:///dtd/
      2. MyBatis的中文手冊:http://mybatis./files/MyBatis%203%20User%20Guide%20Simplified%20Chinese.pdf

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多