mybatis 批量操做

mysql 批量插入java

第一种:mysql

<insert id="insertBatch" parameterType="java.util.List" >
insert into student ( <include refid="Base_Column_List"/> ) 
values 
<foreach collection="list" item="item" index="index" separator=",">
(null,#{item.name},#{item.sex},#{item.address},#{item.telephone},#{item.tId})
</foreach>
</insert>

第二种:sql

<insert id="batchInsertB2B" parameterType="ArrayList">
insert into xxxxtable(hkgs,hkgsjsda,office,asdf,ddd,ffff,supfullName,classtype,agent_type,remark)
<foreach collection="list" item="item" index="index" separator="union all">
select #{item.hkgs,jdbcType=VARCHAR},
#{item.hkgsjsda,jdbcType=VARCHAR},
#{item.office,jdbcType=VARCHAR},
#{item.asdf,jdbcType=VARCHAR},
#{item.ddd,jdbcType=VARCHAR},
#{item.ffff,jdbcType=VARCHAR},
#{item.supfullName,jdbcType=VARCHAR},0,0,
#{item.remark,jdbcType=VARCHAR} from dual
</foreach>
</insert>

注意: 若是为数组 collection=“array”  若是为集合 collection=“list”数据库

 

批量删除:数组

<delete id="batchRemoveUserByPks" parameterType="java.util.List">
 
DELETE FROM LD_USER WHERE ID in 
 
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
 
#{item}
 
</foreach>
 
</delete>

批量更新spa

<update id="batchUpdate"  parameterType="java.util.List">
	 
	      <foreach collection="list" item="item" index="index" open="" close="" separator=";">
				update test 
				<set>
				  test=${item.test}+1
				</set>
				where id = ${item.id}
		 </foreach>
		 
    </update>

注意:数据库链接必须配置:&allowMultiQueries=true.net

一对多的关系对应的查询: http://blog.csdn.net/mamba10/article/details/20927225code

mysql 插入获取主键:xml

方法1:
    <insert id="insert" parameterType="Person" useGeneratedKeys="true" keyProperty="id">
        insert into person(name,pswd) values(#{name},#{pswd})
    </insert>
 
方法2:
    <insert id="insert" parameterType="Person">
        <selectKey keyProperty="id" resultType="long">
            select LAST_INSERT_ID()
        </selectKey>
        insert into person(name,pswd) values(#{name},#{pswd})
    </insert>
相关文章
相关标签/搜索