MyBatis_動態sql_foreach_mysql下foreach批量插入的兩種方式


方法1:

筆記要點
出錯分析與總結
工程組織
數據庫組織
0.重新修改Bean類    修改
1.定義接口

//批量插入
    public void addEmps(@Param("emps")List<Employee> emps);

2.定義XML映射文件

<!--//批量插入-->
    <!--public void addEmps(@Param("emps")List<Employee> emps);-->
    <insert id="addEmps">
        INSERT INTO tbl_employee(`last_name`,`email`,`gender`,`d_id`)
        VALUES
        <foreach collection="emps" item="emp" separator=",">
            (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
        </foreach>

    </insert>

3.編寫測試代碼

public SqlSessionFactory getSqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

 @Test
    public void test12() throws Exception {
        SqlSession openSession = getSqlSessionFactory().openSession();
        try {
            System.out.println("++++++++++---- 2.測試動態SQL: mysql下的foreach的批量插入");
            EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class);
            List<Employee> emps=new ArrayList<>();
            emps.add(new Employee(null,"smith" ,"smith@qq.com" , "1",
                    new Department(1)));
            emps.add(new Employee(null,"aliex" ,"aliex@qq.com" , "0",
                    new Department(1)));
            mapper.addEmps(emps);

            openSession.commit();
        } finally {
            openSession.close();
        }
    }

測試結果

++++++++++---- 2.測試動態SQL: mysql下的foreach的批量插入
DEBUG 12-05 16:43:52,848 ==>  Preparing: INSERT INTO tbl_employee(`last_name`,`email`,`gender`,`d_id`) VALUES (?,?,?,?) , (?,?,?,?)   (BaseJdbcLogger.java:145) 
DEBUG 12-05 16:43:52,873 ==> Parameters: smith(String), smith@qq.com(String), 1(String), 1(Integer), aliex(String), aliex@qq.com(String), 0(String), 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 12-05 16:43:52,875 <==    Updates: 2  (BaseJdbcLogger.java:145) 

方法2:     使用多條mysql語句

1.開啟allowMultiQueries=true屬性--支持一次查詢多條語句, 進入全局配置文件中

2.定義XML映射文件

   <insert id="addEmps">

        <foreach collection="emps" item="emp" separator=";">
            INSERT INTO tbl_employee(last_name,email,gender,d_id)
            VALUES (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id});
        </foreach>
    </insert>

 

3.編寫測試代碼

@Test
    public void test12() throws Exception {
        SqlSession openSession = getSqlSessionFactory().openSession();
        try {
            System.out.println("++++++++++---- 2-2.測試動態SQL: mysql下的foreach的批量插入");
            EmployeeMapper_DynamicSQL mapper = openSession.getMapper(EmployeeMapper_DynamicSQL.class);
            List<Employee> emps=new ArrayList<>();
            emps.add(new Employee(null,"smith2" ,"smith@qq.com" , "1",
                    new Department(1)));
            emps.add(new Employee(null,"aliex2" ,"aliex@qq.com" , "0",
                    new Department(1)));
            mapper.addEmps(emps);

            openSession.commit();
        } finally {
            openSession.close();
        }
    }

 


測試結果

  idea環境下, 測試失敗!原因如下:

++++++++++---- 2-2.測試動態SQL: mysql下的foreach的批量插入
DEBUG 12-05 17:14:10,399 ==>  Preparing: INSERT INTO tbl_employee(last_name,email,gender,d_id) VALUES (?,?,?,?); ; INSERT INTO tbl_employee(last_name,email,gender,d_id) VALUES (?,?,?,?);   (BaseJdbcLogger.java:145) 
DEBUG 12-05 17:14:10,424 ==> Parameters: smith2(String), smith@qq.com(String), 1(String), 1(Integer), aliex2(String), aliex@qq.com(String), 0(String), 1(Integer)  (BaseJdbcLogger.java:145) 

org.apache.ibatis.exceptions.PersistenceException: 
### Error updating database.  Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; 
            INSERT INTO tbl_employee(last_name,email,gender,d_id)
           ' at line 3
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: INSERT INTO tbl_employee(last_name,email,gender,d_id)             VALUES (?,?,?,?);          ;              INSERT INTO tbl_employee(last_name,email,gender,d_id)             VALUES (?,?,?,?);
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; 
            INSERT INTO tbl_employee(last_name,email,gender,d_id)
           ' at line 3
........
....

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM