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>