mybatis批量插入和更新
<!--无效的列类型: 1111 jdbcTypeForNull XXX 免去JDBCTYPE -->
<settings>
<setting name="jdbcTypeForNull" value="NULL" />
</settings>
1:批量插入
<insert id="insertBatch" parameterType="Java.util.List" >
insert into RECIPEDETAIL (RDID, ROID, TYPE,
NAME, MEDIWEIGHT, MEDINUM,
MONEY, OPERATETIME, CHARGENUMBER,
REMARK, INSTRUCTION, AMOUNTEVERY,
MEDIUNIT, STATUS)
<!--序列Oracle自增 -->
select SEQ_recipeDetail.NEXTVAL,A.* from(
<foreach collection="list" item="item" index="index" separator="union all">
select #{item.roid,jdbcType=DECIMAL}, #{item.type,jdbcType=DECIMAL},
#{item.name,jdbcType=NVARCHAR}, #{item.mediweight,jdbcType=DECIMAL}, #{item.medinum,jdbcType=DECIMAL},
#{item.money,jdbcType=DECIMAL}, #{item.operatetime,jdbcType=TIMESTAMP}, #{item.chargenumber,jdbcType=NVARCHAR},
#{item.remark,jdbcType=NVARCHAR}, #{item.instruction,jdbcType=NVARCHAR}, #{item.amountevery,jdbcType=DECIMAL},
#{item.mediunit,jdbcType=NVARCHAR}, #{item.status,jdbcType=DECIMAL}
FROM DUAL
</foreach>
) A
</insert>
2:批量更新
<update id="updateBatch" parameterType="java.util.List" >
<foreach collection="list" item="item" index="index" open="begin" close=";end;" separator=";">
update RECIPEDETAIL set
ROID = #{item.roid,jdbcType=DECIMAL},
TYPE = #{item.type,jdbcType=DECIMAL},
NAME = #{item.name,jdbcType=NVARCHAR},
MEDIWEIGHT = #{item.mediweight,jdbcType=DECIMAL},
MEDINUM = #{item.medinum,jdbcType=DECIMAL},
MONEY = #{item.money,jdbcType=DECIMAL},
OPERATETIME = #{item.operatetime,jdbcType=TIMESTAMP},
CHARGENUMBER = #{item.chargenumber,jdbcType=NVARCHAR},
REMARK = #{item.remark,jdbcType=NVARCHAR},
INSTRUCTION = #{item.instruction,jdbcType=NVARCHAR},
AMOUNTEVERY = #{item.amountevery,jdbcType=DECIMAL},
MEDIUNIT = #{item.mediunit,jdbcType=NVARCHAR},
STATUS = #{item.status,jdbcType=DECIMAL}
where RDID = #{item.rdid,jdbcType=DECIMAL}
</foreach>
</update>
二:MySQL数据库
批量操作主要使用的是Mybatis的foreach,遍历参数列表执行相应的操作,所以批量插入/更新/删除的写法是类似的,只是SQL略有区别而已。MySql批量操作需要数据库连接配置allowMultiQueries=true才可以。
(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>