MySql批量操作数据库
- 1)批量插入
- 批量操作主要使用的是Mybatis的foreach,遍历参数列表执行相应的操作,
- 所以批量插入/更新/删除的写法是类似的,只是SQL略有区别而已。
- mysql批量操作需要数据库连接配置allowMultiQueries=true才可以。
- <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>
- 或者
- <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>
- (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>
相关推荐
Dullonjiang 2020-07-30
sofast 2020-07-08
AngelicaA 2020-07-04
Iamready 2020-06-25
TMD咯MySQL 2020-06-16
窃破天道 2020-06-12
tanyhuan 2020-06-09
debugjoker 2020-06-07
nan00zzu 2020-06-07
BiPerler 2020-06-03
勇往直前 2020-06-01
huangyx 2020-05-29
阿亮 2020-05-28
Justagreenonion 2020-05-14
sunnyJam 2020-04-03
数据库之扑朔迷离 2020-04-26
gamestart0 2020-04-10
李轮清 2020-05-11
imacoder 2020-05-10