1. 程式人生 > 其它 >easyExcel+mybatis批量插入時Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax;

easyExcel+mybatis批量插入時Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax;

技術標籤:筆記mybatis批量插入

error:
Cause: java.sql.SQLSyntaxErrorException: 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 '' at line 1

### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: 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 '' at line 1 ### The error may exist in file [D:\Software\工作\study\easy-excel\target\classes\mapper\GoodsDao.xml] ### The error may involve defaultParameterMap ### The error occurred while setting parameters ### SQL:
insert into goods (goods_name, price) values ### Cause: java.sql.SQLSyntaxErrorException: 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 '' at line 1 ; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException:
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 '' at line 1] with root cause java.sql.SQLSyntaxErrorException: 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 '' at line 1

解決的過程:

  • xml中的sql語句如下:
<insert id="save" parameterType="java.util.List">
    insert into goods (goods_name, price) values
    <foreach collection="list" item="dataList" index="index" separator=",">
        (
        #{dataList.goods_name},
        #{dataList.price}
        )
    </foreach>
</insert>
  • dao介面
public interface GoodsDao {
    void save(@Param("list") List<Goods> list);
}
  • easyExcel讀取excel監聽器
public class GoodsExcelReadListener extends AnalysisEventListener<Goods> {
    private GoodsDao goodsDao;
    //如果此處需要使用Spring管理的類,需要通過建構函式的方式將spring管理的類傳進來
    public GoodsExcelReadListener(GoodsDao goodsDao){
        this.goodsDao =goodsDao;
    }

    private static final int  BATCH_COUNT = 5;//實際可以用3000,方便回收記憶體
    private List<Goods>  list = new ArrayList<>();
    @Override
    public void invoke(Goods goods, AnalysisContext analysisContext) {
        list.add(goods);
        if(list.size()>=BATCH_COUNT){
            goodsDao.save(list);
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        goodsDao.save(list);
    }
  1. 首先想到的是mapper.xml中的sql語句有問題,因為這個提示不是很明顯,所以想到的是可能sql語句寫的有問題,但是仔細檢查了發現寫的並沒有毛病。
  2. 又試了試不從excel讀取資料,模擬一個list集合然後批量插入,發現可以正常插入。
  3. 定位就是easyExcel讀取那塊插入出現問題。
  4. 翻了一下監聽器那塊,找到了問題 問題就出在了doAfterAllAnalysed這個方法。
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
    goodsDao.save(list);
    }
    初學easyExcel這個工具,之前測試的時候沒有涉及資料庫操作,一直都是模擬列印。然後這個方法會在excel當前sheet所有行的資料讀取完後執行,我的本意是插入剩餘的資料。但是我每次批量儲存的資料是5條,我excel裡面一共放了20條資料,所以剛好4次就插入完了,所以執行到這裡的時候list集合是沒有資料的,插入的時候就造成了文初的那個報錯。所以這裡要加一個判斷。
@Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if(list.size()>0) {
            goodsDao.save(list);
        }
    }

大意了,,,

總結:

當mybatis批量插入出現 Cause: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax;這種異常時,不一定就是mapper.xml中的sql語句寫的有問題。也有可能是插入的集合為空造成的。