mybatis結合mysql批量操作及查詢sql


MySQL數據庫 
批量操作主要使用的是Mybatis的foreach,遍歷參數列表執行相應的操作,所以批量插入/更新/刪除的寫法是類似的,只是SQL略有區別而已。MySql批量操作需要數據庫連接配置allowMultiQueries=true才可以。 

(0)批量查詢:

 <select id="selectUserDataList" parameterType="list" resultType="String">
        select userData from tbl_hbb_user_info where mobile in (
        <foreach collection="list" item="item" index="index"
                 separator=",">
            #{item.mobile}
        </foreach>
        )
    </select>


(1)批量插入  

<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  
        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  
            insert into user (name, age,dept_code) values  
            (#{item.name,jdbcType=VARCHAR},  
            #{item.age,jdbcType=INTEGER},  
             #{item.deptCode,jdbcType=VARCHAR}  
            )  
        </foreach>  
    </insert>  



上面演示的是MySql的寫法(表主鍵自增的寫法),因為MySql支持主鍵自增,所以直接設置useGeneratedKeys=true,即可在插入數據時自動實現主鍵自增;不需要自增時就不需要設置useGeneratedKeys,而且插入SQL包含所有字段即可。實際Mysql還有另外一種寫法,就是拼接values的寫法,這種方法我測試過比多條insert語句執行的效率會高些。不過需要注意一次批量操作的數量做一定的限制。具體寫法如下:  

<insert id="batchInsert" parameterType="java.util.List" useGeneratedKeys="true">  
        insert into user (name, age,dept_code) values  
        <foreach collection="list" index="index" item="item" open="" close="" separator=",">  
            (#{item.name,jdbcType=VARCHAR},  
            #{item.age,jdbcType=INTEGER},  
             #{item.deptCode,jdbcType=VARCHAR}  
            )  
        </foreach>  
    </insert>  


對於Oracle不支持主鍵自增,需要序列替換,所以在SQL寫法上略有不同,需要在insert語句前加個 <selectKey>...</selectKey>告知Mybatis主鍵如何生成(selectKey中間的內容有省略,實際是生成主鍵的SQL)。 

(2)批量更新  

<update id="batchUpdate" parameterType="java.util.List">  
        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  
            update user set name=#{item.name,jdbcType=VARCHAR},age=#{item.age,jdbcType=INTEGER}  
            where id=#{item.id,jdbcType=INTEGER}  
        </foreach>  
    </update>  



(3)批量刪除  

<delete id="batchDelete" parameterType="java.util.List">  
        <foreach close="" collection="list" index="index" item="item" open="" separator=";">  
            delete from user  
            where id=#{item.id,jdbcType=INTEGER}  
        </foreach>  
    </delete>  



二、模糊查詢  

<select id="selectLikeName" parameterType="java.lang.String" resultMap="BaseResultMap">  
        select  
        <include refid="Base_Column_List" />  
        from user  
        where name like CONCAT('%',#{name},'%' )   
    </select>  


上面的模糊查詢語句是Mysql數據庫的寫法示例,用到了Mysql的字符串拼接函數CONCAT,其它數據庫使用相應的函數即可。 

三、多條件查詢 

多條件查詢常用到Mybatis的if判斷,這樣只有條件滿足時,才生成對應的SQL。  

<select id="selectUser" parameterType="map" resultMap="BaseResultMap">  
        select  
        <include refid="Base_Column_List" />  
        from user  
        <where>  
            <if test="name != null">  
                name = #{name,jdbcType=VARCHAR}  
            </if>  
            <if test="age != null">  
                and age = #{age,jdbcType=INTEGER}  
            </if>  
        </where>  
    </select>  



四、聯表查詢 
聯表查詢在返回結果集為多張表的數據時,可以通過繼承resultMap,簡化寫法。例如下面的示例,結果集在User表字段的基礎上添加了Dept的部門名稱

<resultMap id="ExtResultMap" type="com.research.mybatis.generator.model.UserExt" extends="BaseResultMap">  
     <result column="name" jdbcType="VARCHAR" property="deptName" />  
  </resultMap>  
      
    <select id="selectUserExt" parameterType="map" resultMap="ExtResultMap">  
        select  
            u.*, d.name  
        from user u inner join dept d on u.dept_code = d.code  
        <where>  
            <if test="name != null">  
                u.name = #{name,jdbcType=VARCHAR}  
            </if>  
            <if test="age != null">  
                and u.age = #{age,jdbcType=INTEGER}  
            </if>  
        </where>  
    </select>  

 

復制代碼
<update id="stockDetailOkBatchUpdate" parameterType="map">
      <foreach collection="items" index="index" item="item" open="begin" close=";end;" separator=";">
          update T_MM_ADD_STOCK_DETAIL t
          set
          t.REMARK=#{item.remark}, t.modify_time=sysdate, t.modify_user_code=#{currentUser} where t.id=#{item.id} <if test="index==items.size-1"> ; update T_MM_ADD_STOCK t set t.modify_time=sysdate, t.modify_user_code=#{currentUser}, t.remark=#{remark}, t.STORAGE_STATE='待錄價' where t.id=#{mainId} </if> </foreach> </update>
復制代碼

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM