13

Mapper.xml(映射器 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="TestDAO">
<insert id="insertEmployeeList" parameterType="java.util.List">
INSERT INTO EMPLOYEE (id, name) VALUES
<foreach collection="list" item="element" index="index" open="(" separator=","  close=")">
#{element.id}, #{element.name}
</foreach>
</insert>
</mapper>

雇员.java

public class Employee {
  private List<Emp> list = new ArrayList<Emp>();
  public List<Emp> getList() {
    return list;
  }
  public void setList(List<Emp> list) {
    this.list = list;
  }
}

Emp.java

public class Emp {
  public Emp(int id, String name) {
    this.id = id;
    this.name = name;
  }
  private int id;
  private String name;
  public int getId() {
    return id;
  }
  public void setId(int id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
}

TestDAO.java

public interface TestDAO {
public Integer insertEmployeeList(List<Emp> empList) throws SQLException;
}

主.java

public class Main {
   public static void main (String args[]) {
       TestDAO tm = session.getMapper(TestDAO.class);
       Employee e = new Employee();
       Emp e11 = new Emp(123,"abc");
       Emp e12 = new Emp(456,"def");
       e.getList().add(e11);
       e.getList().add(e12);
       tm.insertEmployeeList(e.getList());
   }
}

我得到的例外是:

Error updating database.  Cause: java.sql.SQLSyntaxErrorException: ORA-00913: too many values
The error may involve com.XXXX.sample.test.dao.TestDAO.insertEmployeeList-Inline
The error occurred while setting parameters
Cause: java.sql.SQLSyntaxErrorException: ORA-00913: too many values
4

5 回答 5

21

映射器.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="com.xxxx.sample.test.dao.TestDAO">
  <insert id="insertEmployeeList" parameterType="java.util.List">
   INSERT ALL  
    <foreach collection="list" item="element" index="index" >
     INTO EMPLOYEE (id,name) values (#{element.id},#{element.name})
    </foreach>
   SELECT * FROM dual
 </insert>
</mapper>

这就是 Mapper xml 中的查询方式

于 2013-07-12T06:55:49.010 回答
21

当我使用 MySQL 作为数据库时,经过几次尝试,这就是它对我的工作方式。

<insert id="insert" parameterType="java.util.List">
        INSERT INTO games (
        id,
        game_id,
        game_vendor,
        game_code,
        game_name,
        game_type)
        VALUES
        <foreach collection="list" item="element" index="index" open="(" separator="),("  close=")">
            #{element.id},
            #{element.gameId},
            #{element.gameVendor},
            #{element.gameCode},
            #{element.gameName},
            #{element.gameType},
       </foreach>
    </insert>
</mapper>
于 2015-06-30T08:31:36.823 回答
11

您可以使用注释 ( @org.apache.ibatis.annotations.Insert ) 为整个列表执行单个插入。

请记住: 您不需要任何 sql 提供程序类。

public interface SomethingMapper {

    @Insert({
        "<script>",
        "INSERT INTO your_database_name.your_table_name",
            "(column1_int, column2_str, column3_date, column4_time)",
        "VALUES" +  
            "<foreach item='each_item_name' collection='theCollection' open='' separator=',' close=''>" +
                "(" +
                    "#{each_item_name.column1,jdbcType=INTEGER},",
                    "#{each_item_name.column2,jdbcType=VARCHAR},",
                    "(SELECT SOME_DB_FUNCTION(#{each_item_name.column3,jdbcType=DATE})),",
                    "#{each_item_name.period.start,jdbcType=TIME}" +
                ")" +
            "</foreach>",
    "</script>"})
    void insertBatchSomething(@Param("theCollection") List<Something> theCollection);

}

如果您有 2 项,则输出 SQL:

SQL: INSERT INTO your_database_name.your_table_name (column1_int, column2_str, column3_date, column4_time) VALUES (?, ?, (SELECT SOME_DB_FUNCTION(?)), ?), (?, ?, (SELECT SOME_DB_FUNCTION(?)), ?)

附言

@Insert接收一个字符串 [],因此对于每个值,它将在字符串之间添加一个空格。

于 2017-08-15T13:08:42.267 回答
2

配置您的日志系统以打印生成的查询,然后尝试将它们直接执行到您的数据库。

在您的情况下,预期的查询应该是

INSERT INTO EMPLOYEE (id, name) VALUES
(123, "abc")(456, "def")

据我所知,这是无效的。INSERT INTO只需要一组VALUES. 我查看了文档,那里也看起来像这样。

有关如何在 oracle 中处理多行的信息,请参阅此答案。INSERT

于 2013-07-10T07:36:32.323 回答
2

[tistory] ​​http://woniperstory.tistory.com/194 :此链接显示正确答案!

public  someClass {

    public void someMethod() {

        Map<String,Object> map =  new HashMap<String,Object>() ;
        List<Emp> list =  new ArrayList<Emp>() ;

        ::::  add Emp Objects to list

        map.put("list", list)

        public Integer insertEmployeeList(map) ;
    }
}

public interface TestDAO {
    public Integer insertEmployeeList(Map<String,Object> map) throws SQLException;
}

<?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="TestDAO">  
<insert id="insertEmployeeList" parameterType="java.util.Map">  
INSERT INTO EMPLOYEE (id, name) VALUES  
<foreach collection="list" item="element" index="index" separator=",">  
    ( #{element.id}, #{element.name} )  
</foreach>  
</insert>  
</mapper>  
于 2014-11-12T08:40:39.027 回答