当前位置:首页 > 开发 > 编程语言 > mybatis,Ibatis > 正文

mybatis基础操作之批量操作

发表于: 2013-12-26   作者:53873039oycg   来源:转载   浏览:
摘要:     Mybatis批量操作,包括批量新增、修改、删除等,本次使用Oracle数据库进行操作。     首先新建表     -- Create table create table TEST_INSERT ( ID NUMBER not null, NAME V

    Mybatis批量操作,包括批量新增、修改、删除等,本次使用Oracle数据库进行操作。

    首先新建表

   

-- Create table
create table TEST_INSERT
(
  ID        NUMBER not null,
  NAME      VARCHAR2(12),
  AGE       NUMBER(3),
  STARTDATE DATE
)
-- Create/Recreate primary, unique and foreign key constraints 
alter table TEST_INSERT
  add primary key (ID)

 

   2新建JavaBean

   

package bean;

import java.util.Date;

public class TestInsert {
	private long id;
	
	public String toString() {
		return "t [id=" + id + ", age=" + age + ", startDate="
				+ startDate.toLocaleString()+ ", name=" + name + "]";
	}

	private int age;
	private Date startDate;
	private String name;

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public Date getStartDate() {
		return startDate;
	}

	public void setStartDate(Date startDate) {
		this.startDate = startDate;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public TestInsert(long id, int age, Date startDate, String name) {
		super();
		this.id = id;
		this.age = age;
		this.startDate = startDate;
		this.name = name;
	}

	public TestInsert() {
		super();
	}

	public TestInsert(int age, Date startDate, String name) {
		super();
		this.age = age;
		this.startDate = startDate;
		this.name = name;
	}

}

   首先是Mybatis使用注解方式批量新增

  

public interface TestInsertMapper {

	@Insert("insert into test_insert (id, name, age, startDate) values (#{id}, #{name}, #{age}, #{startDate}) ")
	public void insertTest(TestInsert test);
}

   mybatis.xml配置为

  

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
	 <settings>
		 <setting name="cacheEnabled" value="true" /> 
		<setting name="lazyLoadingEnabled" value="true" /> 
		<setting name="multipleResultSetsEnabled" value="true" />
		<setting name="useColumnLabel" value="true" />
		<!-- <setting name="useGeneratedKeys" value="true" /> -->
		<setting name="defaultExecutorType" value="SIMPLE" />
		<setting name="defaultStatementTimeout" value="25000" />
	</settings> 
	
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
				<property name="url"
					value="jdbc:oracle:thin:@localhost:1521:xe" />
				<property name="username" value="admin" />
				<property name="password" value="123" />
			</dataSource>
		</environment>
	</environments>

	<mappers>
		<!-- <mapper resource="config/score.xml" /> -->
		<mapper resource="config/testinsert-mapper.xml"/>
		<mapper class="mapper.TestInsertMapper"/>  
	</mappers>
</configuration>

    对应的例子为:

  

public void batchInsert1() throws Exception {
		SqlSession sqlSession = ssf.openSession(ExecutorType.BATCH);
		TestInsertMapper pmapper = sqlSession.getMapper(TestInsertMapper.class);
		long start = System.currentTimeMillis();
		try {
			for (int i = 0; i < 16; i++) {
				pmapper.insertTest(testList.get(i));			}
			sqlSession.commit();
		} finally {
			sqlSession.close();
			System.out.println((System.currentTimeMillis() - start) + " ms---");
		}
	}

     下面是for-each进行插入,对应的配置为:

    

<insert id="OracleBatchInsert" parameterType="java.util.List">
		insert into test_insert(id, name, age, startDate)(
		<foreach collection="list" item="test" index="index"
			separator="union all">
			select
			#{test.id}, #{test.name}, #{test.age},
			#{test.startDate}
			from dual
		</foreach>
		)
	</insert>

    测试例子和上面的差不多

  

public void batchInsert2() throws Exception {
		SqlSession sqlSession = ssf.openSession();
		long start = System.currentTimeMillis();
		try {
			sqlSession.insert("test.OracleBatchInsert", testList);
			sqlSession.commit();
		} finally {
			sqlSession.close();
			System.out.println((System.currentTimeMillis() - start) + " ms---");
		}
	}

     上面例子是主键自己指定的,接下来是使用Oracle的序列作为主键,对应的配置为

   

<insert id="OracleBatchSeqInsert" parameterType="java.util.List">
		insert into test_insert(id, name, age, startDate)(
		select
		STUDENT_ID_SEQUENCE.NEXTVAL,A.* from(
		<foreach collection="list" item="test" index="index"
			separator="union all">
			select #{test.name}, #{test.age},
			#{test.startDate}
			from dual
		</foreach>
		)A)
	</insert>

    测试例子和上面一样

    接下来是批量更新,对应配置为

  

<update id="updateByIds" parameterType="map">
		update test_insert
		set startDate = #{startDate,jdbcType=TIMESTAMP} where id in
		<foreach collection="list" item="item" index="index" open="("
			close=")" separator=",">
			#{item}
		</foreach>

	</update>

   测试例子为:

   

public void batchUpdate() {
		SqlSession sqlSession = ssf.openSession();
		long start = System.currentTimeMillis();
		try {
			Map map = new HashMap();
			map.put("startDate", new Date());
			map.put("list", idlist);
			sqlSession.update("test.updateByIds", map);
			sqlSession.commit();
		} finally {
			sqlSession.close();
			//
			System.out.println((System.currentTimeMillis() - start) + " ms---");
		}

	}

   最后是批量删除,配置如下:

   

<delete id="deleteBatch" parameterType="list">
		delete from test_insert where id in
		<foreach collection="list" item="item" index="index" open="("
			close=")" separator=",">
			#{item}
		</foreach>
	</delete>

    测试例子如下:

   

public void batchDelete() {
		SqlSession sqlSession = ssf.openSession();
		long start = System.currentTimeMillis();
		try {
			sqlSession.delete("test.deleteBatch", idlist);
			sqlSession.commit();
		} finally {
			sqlSession.close();
			//
			System.out.println((System.currentTimeMillis() - start) + " ms---");
		}
	}

    全文完,源码就不打包了,上面都有,写的不好的地方,请多包涵,谢谢!

     --------------------------------------------------------------------------------------------

     Ibatis2 批量插入配置为:

    

<insert id="oraclebatchinsert" parameterClass="java.util.List">
		insert into test_insert(id, name, age, startDate)(
		<iterate conjunction="union all">
			select
			#idsList[].id#, #idsList[].name#, #idsList[].age#,
			#idsList[].startDate#
			from dual
		</iterate>
		)
	</insert>

	<insert id="oracleseqbatchinsert" parameterClass="java.util.List">
		insert into test_insert(id, name, age, startDate)(
		select
		STUDENT_ID_SEQUENCE.NEXTVAL,A.* from(
		<iterate conjunction="union all">
			select
			#idsList[].name#, #idsList[].age#,
			#idsList[].startDate#
			from dual
		</iterate>
		)A)
	</insert>

 

   

     

mybatis基础操作之批量操作

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Mybatis前身是Ibatis,Ibatis是apache的一个顶级开源项目。2010年迁移到Google code,更名为Mybatis
MyBatis的前身就是著名的Ibatis,不知何故脱离了Apache改名为MyBatis。 MyBatis所说是轻量级的ORM框
MyBatis的前身就是著名的Ibatis,不知何故脱离了Apache改名为MyBatis。 MyBatis所说是轻量级的ORM框
MyBatis的前身就是著名的Ibatis,不知何故脱离了Apache改名为MyBatis。 MyBatis所说是轻量级的ORM框
一、mybatis中的批量操作 批量操作核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中
一、mybatis中的批量操作 批量操作核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中
一、mybatis中的批量操作 批量操作核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中
一、mybatis中的批量操作 批量操作核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中
一、mybatis中的批量操作 批量操作核心就是一次传入多个数据然后进行相关操作,增删改查中掌握其中
MyBatis的前身就是著名的Ibatis,不知何故脱离了Apache改名为MyBatis。 MyBatis所说是轻量级的ORM框
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号