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);
}

测试成功
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值