mysql批量操作

mysql批量语句执行语法错误解决:
jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf-8 &allowMultiQueries=true

1、批量删除
Mapper.java
public void deleteCommunityRelUser(@Param("communityId") String communityId,@Param("userIdList") List<String> userIdList) throws Exception;

sql.xml
<delete id="deleteCommunityRelUser">
		DELETE FROM COMMUNITY_REL_USER 
			<where>
				<if test="communityId != null and communityId != ''">
					AND COMMUNITY_ID=#{communityId}
				</if>
				<if test="userIdList != null and userIdList != ''">
					AND USER_ID IN
					<foreach item="item" collection="userIdList" separator="," open="(" close=")" index="">  
				      #{item}  
				    </foreach>
				</if>
			</where>
	</delete>

2、批量修改
Mapper.java
public void updateCommunityRelUser(@Param("communityId") String communityId,@Param("userIdList") List<String> userIdList) throws Exception;

sql.xml
<update id="updateCommunityRelUser">
		UPDATE COMMUNITY_REL_USER SET AUDIT='1'
			<where>
				<if test="communityId != null and communityId != ''">
					AND COMMUNITY_ID=#{communityId}
				</if>
				<if test="userIdList != null and userIdList != ''">
					AND USER_ID IN
					<foreach item="item" collection="userIdList" separator="," open="(" close=")" index="">  
				      #{item}  
				    </foreach>
				</if>
			</where>
	</update>

3、批量新增
Mapper.java
public void insertInviteUser(@Param("communityMemberVOList") List<CommunityMemberVO> communityMemberVOList) throws Exception;

sql.xml
mysql
<insert id="insertInviteUser" parameterType="java.util.List">
		INSERT INTO COMMUNITY_REL_USER (USER_ID,COMMUNITY_ID,AUDIT)	
		VALUES 
		<foreach item="item" collection="communityMemberVOList" separator=","  index="index">  
	     	(#{item.userId:VARCHAR},#{item.communityId:VARCHAR},#{item.audit:VARCHAR}) 
	    </foreach>
	</insert>

oracle
<insert id="insertInviteUser" parameterType="java.util.List">
	INSERT INTO COMMUNITY_REL_USER (USER_ID,COMMUNITY_ID,AUDIT,create_date)	
	<foreach item="item" open="(" close=")" collection="communityMemberVOList" index="index" separator="union all"> 
	select
	#{item.userId,jdbcType=VARCHAR},
	#{item.communityId,jdbcType=VARCHAR},
	#{item.audit,jdbcType=VARCHAR},
	#{item.createdate:TIMESTAMP}
	from dual 
	</foreach>
</insert>

猜你喜欢

转载自java-hulu.iteye.com/blog/2191801