最近我使用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);
}
测试成功
分享到:
相关推荐
Java实现使用Mybatis将数据批量插入到Oracle数据库,并且可以使用Oracle序列来生成主键
Mybatis框架(批量添加)
springboot +mybatis+oracle 配置多个数据源,配置两个数据库信息
Mybatis Plus 自定义批量插入或批量更新(根据唯一索引)
这是一个用mybatis框架连接oracle数据库的例子,可在eclipse上运行,需要在oral建test表包涵(id,age,username,password)字段,
SpringBoot+mybatis+Oracle整合代码,测试通过,可以学习下载。
Java开发中集成mybatis+orcal做批量操作时会遇到的问题。就想记录一下问题,这里的方法我的是完全可以的
实现了oracle多条数据的插入,并通过优化,防止内存不足和缩短运行时间,另外通过mybatis的generator工具自动生成dao层,vo层,xml层文件.
Java实现Mybatis将数据批量插入到Oracle中
mybatis批量添加数据的方法1
主要介绍了mybatis使用char类型字段查询oracle数据库时结果返回null问题的解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
mybatis 整合oracle (含增删改查)
idea配置maven+Springboot+mybatis+oracle 整合,idea
spring 结合mybatis操作oracle数据
Mybatis 处理 CLOB、BLOB 类型数据
MyBatis防止批量更新1
maven+spring+springmvc+mybatis+oracle的整合,使用idea。
mybatis + oracle demo 比较简单的例子、入门很试用哦~
springboot+mybatis+oracle源码,事务管理,
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现...