一、Mybatis批量更新
mapper文件中的sql语句
<!--批量更新表数据-->
<update id="batchUpdate" parameterType="java.util.List" >
<foreach collection="list" item="item" index="index" separator=";">
update 表名
<set>
<if test="item.与entiy属性对应!= null">`表字段名` = #{item.与entiy属性对应}, </if>
</set>
WHERE '表字段名' = ${item.与entiy属性对应}
</foreach>
</update>
在mapper文件里面写上这个不一定会执行成功,还要在配置数据库的地方加上?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
这句话可能在单独的数据库配置文件db.properties中,也可能是在Mybatis的总配置文件中,看具体用到的框架是什么!
##对应的Dao层应该怎么写
int batchUpdate(@Param("list") List<实体类或Map> list);
二、批量删除
mapper映射
<delete id="batchDelete">
DELETE FROM 表名 WHERE id IN
<foreach item="id" collection="array" open="(" separator="," close=")">
#{pkid}
</foreach>
</delete>
Dao层
int batchDelete(@Param("array")Map<String, Object> array);
三、批量添加
<insert id="batchInsert" parameterType="java.util.List">
insert into url_import
(字段1, 字段2, 字段3, 字段4)
values
<foreach collection="list" item="item" index="index" separator=",">
(#{item.字段1-数字类型,jdbcType=INTEGER}
#{item.字段2-日期类型,jdbcType=TIMESTAMP}
#{item.字段3-String,jdbcType=VARCHAR},
#{item.字段4-String,jdbcType=LONGVARCHAR (对应数据库的text类型)}
)
</foreach>
</insert>
/**
* 批量添加对象
* @param list
* @return
*/
int batchInsert(@Param("list") List<实体对象> list);