欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

關(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),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面

    play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面

    這篇文章主要介紹了play for scala 實(shí)現(xiàn)SessionFilter 過濾未登錄用戶跳轉(zhuǎn)到登錄頁面的相關(guān)資料,需要的朋友可以參考下
    2016-11-11
  • java代碼實(shí)現(xiàn)斗地主發(fā)牌功能

    java代碼實(shí)現(xiàn)斗地主發(fā)牌功能

    這篇文章主要介紹了java實(shí)現(xiàn)斗地主發(fā)牌功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • Mybatis-Plus中的查詢指定字段

    Mybatis-Plus中的查詢指定字段

    在使用Mybatis-Plus進(jìn)行數(shù)據(jù)查詢時,可以通過指定字段來優(yōu)化查詢效率,方法一和方法二分別執(zhí)行不同的SQL語句,其中方法二在執(zhí)行時通常會更高效,因?yàn)樗赡芡ㄟ^減少數(shù)據(jù)處理量和優(yōu)化查詢結(jié)構(gòu)來提升性能,比較兩種方法的SQL執(zhí)行情況
    2024-09-09
  • 零基礎(chǔ)寫Java知乎爬蟲之進(jìn)階篇

    零基礎(chǔ)寫Java知乎爬蟲之進(jìn)階篇

    前面幾篇文章,我們都是簡單的實(shí)現(xiàn)了java爬蟲抓取內(nèi)容的問題,那么如果遇到復(fù)雜情況,我們還能繼續(xù)那么做嗎?答案當(dāng)然是否定的,之前的僅僅是入門篇,都是些基礎(chǔ)知識,給大家練手用的,本文我們就來點(diǎn)高大上的東西
    2014-11-11
  • Java中的反射機(jī)制詳解

    Java中的反射機(jī)制詳解

    這篇文章主要介紹了JAVA 反射機(jī)制的相關(guān)知識,文中講解的非常細(xì)致,代碼幫助大家更好的理解學(xué)習(xí),感興趣的朋友可以了解下
    2021-09-09
  • springboot用thymeleaf模板的paginate分頁完整代碼

    springboot用thymeleaf模板的paginate分頁完整代碼

    本文根據(jù)一個簡單的user表為例,展示 springboot集成mybatis,再到前端分頁完整代碼,需要的朋友可以參考下
    2017-07-07
  • SpringBoot配置文件中密碼屬性加密的實(shí)現(xiàn)

    SpringBoot配置文件中密碼屬性加密的實(shí)現(xiàn)

    本文主要介紹了SpringBoot配置文件中密碼屬性加密的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-07-07
  • SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式

    SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式

    這篇文章主要介紹了SpringBoot環(huán)境屬性占位符解析和類型轉(zhuǎn)換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Spring Boot Admin實(shí)踐詳解

    Spring Boot Admin實(shí)踐詳解

    在本篇文章里小編給大家整理了關(guān)于Spring Boot Admin實(shí)踐的相關(guān)知識點(diǎn),有需要的朋友們可以學(xué)習(xí)下。
    2019-12-12
  • java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)

    java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了java單鏈表實(shí)現(xiàn)書籍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11

最新評論