這個錯是sqlserver拋出來的。 進過驗證,上述錯誤中的2100爲插入的總字段數。
好比下面這種插入方式,values後面的一個括號裏的字段爲30個,那麼後面最多隻能加70條,即這種批量插入方式一次性最多隻能插入70條。java
insert table() values(),(),()....
若是超過71條,就會出現這個錯誤。這是使用了mybatis這個持久層框架以後sqlserver自身的限制。sql
1.批量插入改成循環單條插入,這個不太合適,批量就是爲了提升插入效率,這樣改只是解決了表面問題,卻丟了咱們想要達到的目的。
2.使用JDBC的的方式執行批量插入。這個方法雖然實現麻煩了點,可是解決了這個報錯的問題,也達到了咱們的目的。
這裏給成第二種解決方式的java代碼數據庫
public void insert(List<Person> personList) throws SQLException { final String sql = "INSERT INTO MT_EXP_SUB(NAME,AGE,SEX) VALUES(?,?,?)"; Connection conn = null; PreparedStatement ps = null; try { // 獲取數據庫鏈接 conn = ds.getConnection(); if (conn == null) { throw(new RuntimeException("獲取數據庫鏈接失敗")); } // 預編譯SQL ps = conn.prepareStatement(sql); // 關閉自動提交事務 conn.setAutoCommit(false); for (Person person : personList) { ps.setString(1, person.getName()); ps.setInt(2, person.getAge()); ps.setString(3, person.getSex()); ps.addBatch(); } // 執行批量入庫 ps.executeBatch(); // 手動提交事務 conn.commit(); }catch (Exception e) { // 批量入庫異常,回滾 conn.rollback(); }finally { if(conn != null) { conn.close(); } if(ps != null) { ps.close(); } } }
參考連接1:https://blog.csdn.net/qq_35457078/article/details/85259789
參考連接2:https://bbs.csdn.net/topics/391933671mybatis