我的电脑操作系统版本为Win7旗舰版(ServicePack1),Oracle版本为Oracle11g
程序使用的jar包有:mybatis-3.2.2.jar、ojdbc14-10.2.0.2.0.jar
本例中使用的配置文件mybatis-config.xml、PersonInfo类以及Oracle数据库的表结构,可以参见我的另一篇Blog《一个简单的MyBatis连接Oracle数据库的例子》(http://my.oschina.net/Tsybius2014/blog/626206)
PersonInfoMapper.java代码如下:
import java.util.List;
public interface PersonInfoMapper {
//获取Sequence作为插入数据库中数据的主键
Long getIdFromSequence();
//插入一条数据
void addPersonInfo(PersonInfo personInfo);
//插入多条数据1
void addPersonInfoBatch(List<PersonInfo> list);
//插入多条数据2
void addPersonInfoBatch2(List<PersonInfo> list);
}
对应的XML文件,PersonInfoMapper.xml代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="PersonInfoMapper">
<!-- 获取Sequence作为插入数据库中数据的主键 -->
<select id="getIdFromSequence" resultType="java.lang.Long"
flushCache="true" useCache="false">
SELECT SEQ_PERSON_INFO.NEXTVAL FROM DUAL
</select>
<!-- 插入一条数据 -->
<insert id="addPersonInfo" parameterType="PersonInfo" >
INSERT INTO PERSON_INFO
(ID, NAME, GENDER, REMARK, INPUT_DATE, INPUT_TIME)
VALUES
(SEQ_PERSON_INFO.NEXTVAL,
#{name, jdbcType = VARCHAR},
#{gender, jdbcType = CHAR},
#{remark, jdbcType = VARCHAR},
#{inputDate, jdbcType = DECIMAL},
#{inputTime, jdbcType = DECIMAL})
</insert>
<!-- 插入多条数据1 -->
<insert id="addPersonInfoBatch" parameterType="java.util.List" >
INSERT INTO PERSON_INFO
(ID, NAME, GENDER, REMARK, INPUT_DATE, INPUT_TIME)
<foreach collection="list" index="index" item="item" separator="union all">
(SELECT #{item.id, jdbcType = DECIMAL} AS ID,
#{item.name, jdbcType = VARCHAR} AS NAME,
#{item.gender, jdbcType = CHAR} AS GENDER,
#{item.remark, jdbcType = VARCHAR} AS REMARK,
#{item.inputDate, jdbcType = DECIMAL} AS INPUT_DATE,
#{item.inputTime, jdbcType = DECIMAL} AS INPUT_TIME
FROM DUAL)
</foreach>
</insert>
<!-- 插入多条数据2 -->
<insert id="addPersonInfoBatch2" parameterType="java.util.List" >
INSERT INTO PERSON_INFO
(ID, NAME, GENDER, REMARK, INPUT_DATE, INPUT_TIME)
SELECT A.* FROM (
<foreach collection="list" index="index" item="item" separator="union all">
(SELECT #{item.id, jdbcType = DECIMAL} AS ID,
#{item.name, jdbcType = VARCHAR} AS NAME,
#{item.gender, jdbcType = CHAR} AS GENDER,
#{item.remark, jdbcType = VARCHAR} AS REMARK,
#{item.inputDate, jdbcType = DECIMAL} AS INPUT_DATE,
#{item.inputTime, jdbcType = DECIMAL} AS INPUT_TIME
FROM DUAL)
</foreach>
) A
</insert>
</mapper>
插入数据的Java代码如下:
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
/**
* MyBatis 插入数据测试
* @author Tsybius2014
* @date 2016年3月4日
* @time 下午6:18:43
* @remark
*/
public class MyBatisTest {
public static void main(String[] args) {
try {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = sqlSessionFactory.openSession();
try {
PersonInfoMapper mapper = session.getMapper(PersonInfoMapper.class);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
String currDateTime = simpleDateFormat.format(new Date());
//新增一条数据
PersonInfo personInfo = new PersonInfo();
personInfo.setName("Tsybius");
personInfo.setGender("m");
personInfo.setRemark("ABCDEFG");
personInfo.setInputDate(Long.parseLong(currDateTime.substring(0, 8)));
personInfo.setInputTime(Long.parseLong(currDateTime.substring(8)));
mapper.addPersonInfo(personInfo);
System.out.println("插入了1条数据");
//新增多条数据
List<PersonInfo> personInfoList = new ArrayList<PersonInfo>();
for (int i = 0; i < 5; i++) {
PersonInfo personInfoTmp = new PersonInfo();
Long id = mapper.getIdFromSequence();
personInfoTmp.setId(id);
personInfoTmp.setName("TestPerson" + i);
personInfoTmp.setGender("m");
personInfoTmp.setRemark("TestMyBatis");
personInfoTmp.setInputDate(Long.parseLong(currDateTime.substring(0, 8)));
personInfoTmp.setInputTime(Long.parseLong(currDateTime.substring(8)));
personInfoList.add(personInfoTmp);
}
mapper.addPersonInfoBatch(personInfoList);
System.out.println("插入了5条数据");
personInfoList.clear();
for (int i = 0; i < 5; i++) {
PersonInfo personInfoTmp = new PersonInfo();
Long id = mapper.getIdFromSequence();
personInfoTmp.setId(id);
personInfoTmp.setName("TestPerson" + i);
personInfoTmp.setGender("m");
personInfoTmp.setRemark("TestMyBatis");
personInfoTmp.setInputDate(Long.parseLong(currDateTime.substring(0, 8)));
personInfoTmp.setInputTime(Long.parseLong(currDateTime.substring(8)));
personInfoList.add(personInfoTmp);
}
mapper.addPersonInfoBatch2(personInfoList);
System.out.println("又插入了5条数据");
personInfoList.clear();
for (int i = 0; i < 1000; i++) {
PersonInfo personInfoTmp = new PersonInfo();
Long id = mapper.getIdFromSequence();
personInfoTmp.setId(id);
personInfoTmp.setName("TestPerson" + i);
personInfoTmp.setGender("m");
personInfoTmp.setRemark("TestMyBatis");
personInfoTmp.setInputDate(Long.parseLong(currDateTime.substring(0, 8)));
personInfoTmp.setInputTime(Long.parseLong(currDateTime.substring(8)));
personInfoList.add(personInfoTmp);
}
mapper.addPersonInfoBatch(personInfoList);
System.out.println("插入了1000条数据");
} finally {
session.commit(); //提交事务,这个步骤必须要有
session.close();
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
程序执行完毕后,在PL/SQL中可以看到插入的数据
最后总结下需要注意的点:
1、在 PersonInfoMapper.xml 中配置的 getIdFromSequence 方法,必须要指定属性 flushCache="true" 和 useCache="false",否则在取序列值时结果会出错。
2、在使用MyBatis中的foreach标签时,记住指定了item后,不要把“#{item.name, jdbcType = VARCHAR}”写成“#{name, jdbcType = VARCHAR} ”,在这个地方我吃过好多次亏了。
END
来源:oschina
链接:https://my.oschina.net/u/1425762/blog/631196