關(guān)于MyBatisSystemException異常產(chǎn)生的原因及解決過程
更新時間:2025年01月23日 17:14:57 作者:宣布無人罪
文章講述了在使用MyBatis進(jìn)行數(shù)據(jù)庫操作時遇到的異常及其解決過程,首先考慮了事務(wù)問題,但未解決,接著懷疑是MyBatis的一級緩存問題,關(guān)閉緩存后問題依舊存在,最終發(fā)現(xiàn)是SQL映射文件中的參數(shù)傳遞錯誤,使用了錯誤的標(biāo)簽導(dǎo)致循環(huán)插入
MyBatisSystemException異常產(chǎn)生原因及解決
異常發(fā)生場景
- 當(dāng)我使用mybatis對數(shù)據(jù)庫操作時報(bào)的錯誤
<resultMap id="shoppingCartProduct" type="shoppingCartProductVo"> <id property="shoppingCartId" column="shopping_cart_id"></id> <result property="productId" column="product_id"></result> <result property="num" column="num"></result> <result property="productName" column="product_name"></result> <result property="productTitle" column="product_title"></result> <result property="productIntro" column="product_intro"></result> <result property="productPicture" column="product_picture"></result> <result property="productPrice" column="product_price"></result> <result property="productSellingPrice" column="product_selling_price"></result> </resultMap> <select id="selectShoppingCartByIds" resultMap="shoppingCartProduct"> select s.shopping_cart_id, s.num, s.product_id, p.product_name, p.product_title, p.product_intro, p.product_picture, p.product_price, p.product_selling_price FROM shopping_cart AS s left JOIN product AS p ON s.product_id = p.product_id WHERE s.shopping_cart_id in (<foreach collection="list" index="id" separator=","> #{id} </foreach>) </select>
嘗試解決問題的過程
1.事務(wù)問題
- 一開始,我以為是事務(wù)問題,于是在service層加上了@Transactional開啟事務(wù)
@Override @Transactional public GetData postOrders(List<Long> shoppingCartIds, Long userId) { //1.判斷用戶是否存在 if (msUserMapper.FindUser(userId) == null) { GetData getData=new GetData(500,"無此賬號",null); return getData; } //2.生成訂單 Orders orders=new Orders(); orders.setOrderNum(UUID.randomUUID().toString()); orders.setUserId(userId); orders.setOrderTime(new Date()); orderProductMapper.addOrders(orders); System.out.println(orders.getOrderId()); System.out.println(orders.getOrderId().getClass().getTypeName()); System.out.println(shoppingCartIds); //3.查詢購物車商品數(shù)據(jù) List<ShoppingCartProductVo> shoppingCartProductVos = productMapper.selectShoppingCartByIds(shoppingCartIds); System.out.println(shoppingCartProductVos.size()); System.out.println(shoppingCartProductVos); List<OrdersDtl> ordersDtls = new ArrayList<>(); for (ShoppingCartProductVo vo : shoppingCartProductVos) { OrdersDtl ordersDtl = new OrdersDtl(); ordersDtl.setOrderId(orders.getOrderId()); ordersDtl.setProductId(vo.getProductId()); ordersDtl.setProductIntro(vo.getProductIntro()); ordersDtl.setProductName(vo.getProductName()); ordersDtl.setProductPicture(vo.getProductPicture()); ordersDtl.setProductPrice(vo.getProductPrice()); ordersDtl.setProductSellingPrice(vo.getProductSellingPrice()); ordersDtl.setProductTitle(vo.getProductTitle()); ordersDtl.setNum(vo.getNum()); ordersDtls.add(ordersDtl); } System.out.println(ordersDtls); orderProductMapper.addBatchOrderDtlsInt(ordersDtls); System.out.println(1); int rs =shoppingCartMapper.deleteShoppingCarts(shoppingCartIds); if (rs>0){ GetData getData=new GetData(200,"操作成功",null); return getData; }else { GetData getData=new GetData(500,"操作成功",rs); return getData; } }
- 現(xiàn)在想想,真是沒抓住重點(diǎn),看錯了代碼的報(bào)錯信息
2.MyBatis一級緩存問題
- 接著排查發(fā)現(xiàn)發(fā)現(xiàn)查詢出的數(shù)據(jù)與同樣的代碼在數(shù)據(jù)庫里不一樣,人當(dāng)場傻了
- 面向百度編程后認(rèn)為是出現(xiàn)了MyBatis一級緩存問題
- 于是在yml文件中關(guān)閉關(guān)閉了MyBatis一級緩存
# 配置mybatis mybatis: # mapper配置文件 mapper-locations: classpath:mapper/*.xml # resultType別名,沒有這個配置resultType包名要寫全,配置后只要寫類名 type-aliases-package: com.example.demo.com.mashang.dao configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl map-underscore-to-camel-case: true local-cache-scope: statement # 設(shè)置一級緩存關(guān)閉,mybatis默認(rèn)開啟
- 當(dāng)然還是不對,無可奈何之下,我只能回歸最初的報(bào)錯信息,意思大概是文件映射有問題
問題的產(chǎn)生及其原因
- 沒辦法,如果真是映射出了錯,那就只能一個一個排查過去了
- 終于在結(jié)合了springboot的報(bào)錯日志
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
select s.num, s.product_id, p.product_name, p.product_title, p.product_intro, p.product_picture, p.product_price, p.product_selling_price FROM shopping_cart AS s INNER JOIN product AS p ON s.product_id = p.product_id WHERE s.shopping_cart_id in ( ? , ? , ? )
- 終于,我發(fā)現(xiàn)mybatis執(zhí)行的語句中,三個問號插入的值是固定的0,1,2
- 也就是循環(huán)插入出了問題
- 果然,我錯誤的使用了標(biāo)簽index
解決方式
- 只要認(rèn)真檢查mybatis的映射文件,我的話是把標(biāo)簽換成即可,以下是修改后的mybatis映射文件
<resultMap id="shoppingCartProduct" type="shoppingCartProductVo"> <id property="shoppingCartId" column="shopping_cart_id"></id> <result property="productId" column="product_id"></result> <result property="num" column="num"></result> <result property="productName" column="product_name"></result> <result property="productTitle" column="product_title"></result> <result property="productIntro" column="product_intro"></result> <result property="productPicture" column="product_picture"></result> <result property="productPrice" column="product_price"></result> <result property="productSellingPrice" column="product_selling_price"></result> </resultMap> <select id="selectShoppingCartByIds" resultMap="shoppingCartProduct"> select s.shopping_cart_id, s.num, s.product_id, p.product_name, p.product_title, p.product_intro, p.product_picture, p.product_price, p.product_selling_price FROM shopping_cart AS s left JOIN product AS p ON s.product_id = p.product_id WHERE s.shopping_cart_id in (<foreach collection="list" item="id" separator=","> #{id} </foreach>) </select>
總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 解決Mybatis報(bào)錯:org.apache.ibatis.reflection.ReflectionException: There is no getter for property named問題
- 解決mybatis generator MySQL自增ID出現(xiàn)重復(fù)問題MySQLIntegrityConstraintViolationException
- MyBatis嵌套查詢collection報(bào)錯:org.apache.ibatis.exceptions.TooManyResultsException
- 解決springboot3:mybatis-plus依賴錯誤:org.springframework.beans.factory.UnsatisfiedDependencyException
- 解決Mybatis出現(xiàn)報(bào)錯Error querying database.Cause: java.lang.IndexOutOfBoundsException: Index 9 out of
- 解決mybatis plus報(bào)錯com.microsoft.sqlserver.jdbc.SQLServerException:必須執(zhí)行該語句才能獲得結(jié)果
相關(guān)文章
play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面
這篇文章主要介紹了play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面的相關(guān)資料,需要的朋友可以參考下2016-11-11java代碼實(shí)現(xiàn)斗地主發(fā)牌功能
這篇文章主要介紹了java實(shí)現(xiàn)斗地主發(fā)牌功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-11-11springboot用thymeleaf模板的paginate分頁完整代碼
本文根據(jù)一個簡單的user表為例,展示 springboot集成mybatis,再到前端分頁完整代碼,需要的朋友可以參考下2017-07-07SpringBoot配置文件中密碼屬性加密的實(shí)現(xiàn)
本文主要介紹了SpringBoot配置文件中密碼屬性加密的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式
這篇文章主要介紹了SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11