MyBatis的mapper配置文件的语句(在Oracle数据中,多条数据之间用union all 连接,MySQL数据库用,):
<insert id="submitItem" parameterType="java.util.List">
insert into ITEM (
ITEM_CODE,
ITEM_NAME,
ITEM_VALUE,
ITEM_CATAGORY
)
select item.* from
(
<foreach collection="list" item="item" index="index" separator="UNION ALL" >
select
#{item.itemCode,jdbcType=VARCHAR},
#{item.itemName,jdbcType=VARCHAR},
#{item.itemValue,jdbcType=VARCHAR},
#{item.itemCategory,jdbcType=VARCHAR}
from dual
</foreach>
) item
</insert>
<!--MySql写法-->
<insert id="submitItem" parameterType="java.util.List">
insert into ITEM (
ITEM_CODE,
ITEM_NAME,
ITEM_VALUE,
ITEM_CATAGORY
)
values
<foreach collection="list" item="item" index="index" separator="," >
(
#{item.itemCode,jdbcType=VARCHAR},
#{item.itemName,jdbcType=VARCHAR},
#{item.itemValue,jdbcType=VARCHAR},
#{item.itemCategory,jdbcType=VARCHAR}
)
</foreach>
</insert>
foreach元素解析:
foreach元素是一个遍历集合的循环语句,它支持遍历数组,List和Set接口的集合。
foreach元素中,collection是传进来的参数名称,可以是一个数组或者List、Set等集合;
item是循环中当前的元素(配置的item的名字随意取,类似于iterator);
index是当前元素在集合中的位置下标;
seperator是各个元素的间隔符;
()分别是open和close元素,表示用什么符号将这些集合元素包装起来。
注意:由于一些数据库的SQL对执行的SQL长度有限制,所以使用foreach元素的时候需要预估collection对象的长度;foreach除了用于本示例的循环插入,亦可用于构建in条件中(可自行尝试)。
或者:
package com.oracle.mapper;
import java.util.List;
import com.oracle.entity.AccountInfo;
public interface AccountInfoMapper {
/**
* 查询所有的数据
* @return
*/
List<AccountInfo> queryAllAccountInfo();
/**
* 批量插入数据
*
* @param accountInfoList
* @return
*/
int batchInsertAccountInfo(List<AccountInfo> accountInfoList);
/**
* 批量插入数据,使用Oracle的序列获取唯一键
*
* @param accountInfoList
* @return
*/
int batchInsertAccountInfoUseSeq(List<AccountInfo> accountInfoList);
/**
* 插入数据,使用Oracle的序列获取唯一键
*
* @param accountInfoList
* @return
*/
int insertOne(AccountInfo accountInfo);
}
<?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="com.oracle.mapper.AccountInfoMapper"><!-- 接口的全类名 -->
<!-- type:实体类的全类名 -->
<resultMap id="BaseResultMap" type="com.oracle.entity.AccountInfo">
<id column="ID" property="id" jdbcType="DECIMAL" />
<result column="USERNAME" property="userName" jdbcType="VARCHAR" />
<result column="PASSWORD" property="password" jdbcType="VARCHAR" />
<result column="GENDER" property="gender" jdbcType="CHAR" />
<result column="EMAIL" property="email" jdbcType="VARCHAR" />
<result column="CREATE_DATE" property="createDate" jdbcType="DATE" />
</resultMap>
<!-- id 跟接口中的方法名称保持一致 -->
<select id="queryAllAccountInfo" resultMap="BaseResultMap">
select ID,
USERNAME,PASSWORD,
GENDER, EMAIL, CREATE_DATE from ACCOUNT_INFO
</select>
<insert id="batchInsertAccountInfo" parameterType="java.util.List">
INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE)
(
<foreach collection="list" index="" item="accountInfo"
separator="union all">
select
#{accountInfo.id},
#{accountInfo.userName},
#{accountInfo.password},
#{accountInfo.gender},
#{accountInfo.email},
#{accountInfo.createDate}
from dual
</foreach>
)
</insert>
<insert id="batchInsertAccountInfoUseSeq" parameterType="java.util.List">
<selectKey resultType="long" keyProperty="id" order="BEFORE">
SELECT ACCOUNT_SEQ.NEXTVAL FROM dual
</selectKey>
INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE)
SELECT ACCOUNT_SEQ.NEXTVAL, m.* FROM(
<foreach collection="list" index="" item="accountInfo"
separator="union all">
select
#{accountInfo.userName},
#{accountInfo.password},
#{accountInfo.gender},
#{accountInfo.email},
sysdate
from dual
</foreach>
) m
</insert>
<insert id="insertOne" parameterType="com.oracle.entity.AccountInfo">
<selectKey resultType="long" keyProperty="id" order="BEFORE">
SELECT ACCOUNT_SEQ.NEXTVAL FROM dual
</selectKey>
INSERT INTO ACCOUNT_INFO(ID, USERNAME,PASSWORD,GENDER, EMAIL,CREATE_DATE)
values(
#{id},
#{userName},
#{password},
#{gender},
#{email},
sysdate
)
</insert>
</mapper>