MySQL數據庫
批量操做主要使用的是Mybatis的foreach,遍歷參數列表執行相應的操做,因此批量插入/更新/刪除的寫法是相似的,只是SQL略有區別而已。MySql批量操做須要數據庫鏈接配置allowMultiQueries=true才能夠。 java
(0)批量查詢:mysql
<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)批量插入 sql
<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)批量更新 mybatis
<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>
2、模糊查詢 測試
<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,其它數據庫使用相應的函數便可。
3、多條件查詢
多條件查詢經常使用到Mybatis的if判斷,這樣只有條件知足時,才生成對應的SQL。 spa
<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>
4、聯表查詢
聯表查詢在返回結果集爲多張表的數據時,能夠經過繼承resultMap,簡化寫法。例以下面的示例,結果集在User表字段的基礎上添加了Dept的部門名稱.net
<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>