`

Mybatis使用Oracle批量添加、更新数据的说明

阅读更多
最近我使用Mybatis插入更新数据遇到了小麻烦,在网上搜索了一大批,感觉不是很如意,最后经过高人指点,弄了出来了,具体请看如下代码
实体:
public class Author implements Serializable {
	private String author_id;
	
	private String userName;
	
	private String password;
	
	private String email;
	
	private String bio;


mapper.xml文件如下:
<update id="updateAuthorBatch" parameterType="com.deppon.cms.mybatis.domain.Author">
	begin
		<foreach collection="list" item="item" index="index"  separator=";">
		update t_zxw_author 
		<set>
			<if test="item.userName != null">
				username=#{userName,jdbcType=VARCHAR},
			</if>
			<if test="item.password != null">
				password = #{password,jdbcType=VARCHAR}
			</if>
		</set>
		where author_id in 
		#{item.author_id,jdbcType=VARCHAR}
		</foreach>
		;end;
		
	</update>

这个是弄死的,有多少数据,就有多少update语句,不知道还有什么好点的方法, 我至少现在没有找到

插入数据这样写:
<!-- 插入的时候insert或者select都行的,不解啊 -->
	<insert id="batchInsert" parameterType="java.util.List">
<!-- 		<selectKey resultType="java.lang.String" order="BEFORE" keyProperty="author_id">  -->
<!--        		SELECT SYS_GUID() FROM DUAL -->
<!-- 		</selectKey> -->
		insert into t_zxw_author(author_id,username,password,email,bio)
		<foreach collection="list" item="item" index="index" separator="union all">
			select SYS_GUID(),#{item.userName,jdbcType=VARCHAR},#{item.password,jdbcType=VARCHAR},#{item.email,jdbcType=VARCHAR},#{item.bio,jdbcType=VARCHAR} from dual
		</foreach>


当弄完这些之后,测试代码如下
	@Test
	public void testAddAuthorBatch(){
		List<Author> list = new ArrayList<Author>();
		Author author = null;
		for(int i=0;i<100;i++){
			author = new Author();
			author.setBio("gaga");
			author.setEmail("ggg");
			author.setPassword("gggg");
			author.setUserName("uyyyyy");
			list.add(author);
		}
		
		authorDao.addAuthorBatch(list);
	}
	
	@Test
	public void testUpdateAuthorBatch(){
		List<Author> list = new ArrayList<Author>();
		Author author = new Author();
		Author author1 = new Author();
		author.setUserName("潘仔");
		author.setPassword("gggg");
		author.setAuthor_id("DDC5B92630ADF245E040A8C091111E6B");
		author1.setAuthor_id("DDE78A0195BAC662E040A8C09111737F");
		author1.setUserName("hhh");
		author.setPassword("kjasdjf");
		list.add(author);
		list.add(author1);
		
		authorDao.updateAuthorBatch(list);
	}

测试成功
分享到:
评论
1 楼 xyc717 2014-07-30  
到底对不对啊,我书读得少,你别骗我,那个update的我试了跟你写的一样都没出来,

<update id="updateHidden" parameterType="java.util.List" >
          begin 
        <foreach collection="list" item="SysParam" index="index"  separator=";"> 
        update sys_param  
<!--   set  hidden=#{SysParam.hidden},order=#{SysParam.order} ,param_update_date=sysdate()  -->
<set> 
            <if test="SysParam.hidden != null"> 
                hidden=#{SysParam.hidden}, 
            </if> 
            <if test="SysParam.order != null"> 
                order = #{SysParam.order} 
            </if> 
        </set> 
        where param_id =#{SysParam.param_id}
        </foreach> 
        ;end; 
</update>

相关推荐

Global site tag (gtag.js) - Google Analytics