DAO:YQDaoImpl
@Override
public int insertYQinfo2DB(List<YQBean> list) {
return insert("YQDao.insertYQinfo2DB",list);
}
YQMapper.xml
<insert id="insertYQinfo2DB" parameterType="java.util.List" useGeneratedKeys="false">
INSERT INTO
yq_info
(id, title, url, source, time, type, create_time)
(SELECT A.*
FROM(
<foreach collection="list" item="item" index="index" separator="UNION ALL">
SELECT
#{item.id, jdbcType=VARCHAR} id,
#{item.title, jdbcType=VARCHAR} title,
#{item.url, jdbcType=VARCHAR} url,
#{item.source, jdbcType=VARCHAR} source,
#{item.time, jdbcType=VARCHAR} time,
#{item.type, jdbcType=VARCHAR} type,
#{item.time, jdbcType=VARCHAR} create_time
FROM dual
</foreach>
) A)
</insert>
以上为Oracle的写法,需要注意的是如果不使用Oracle的序列,需要明文指定 useGeneratedKeys="false"。
如果使用Oracle的序列xml需要写成:
<insert id="insertYQinfo2DB" parameterType="java.util.List">
INSERT INTO
yq_info
(id, title, url, source, time, type, create_time)
(SELECT YQ_INFO.SEQ.NEXTVAL, A.*
FROM(
<foreach collection="list" item="item" index="index" separator="UNION ALL">
SELECT
#{item.title, jdbcType=VARCHAR} title,
#{item.url, jdbcType=VARCHAR} url,
#{item.source, jdbcType=VARCHAR} source,
#{item.time, jdbcType=VARCHAR} time,
#{item.type, jdbcType=VARCHAR} type,
#{item.time, jdbcType=VARCHAR} create_time
FROM dual
</foreach>
) A)
</insert>
补充:如若使用Mysql:
<insert id="insertYQinfo2DB" parameterType="java.util.List">
INSERT INTO
yq_info(
id,
title,
url,
source,
time,
type,
create_time)
VALUES
<foreach collection="list" item="item" index="index" separator="," >
(#{item.id},#{item.title},#{item.url},#{item.source},#{item.time},#{item.type},#{item.time})
</foreach>
</insert>