亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于MyBatisSystemException異常產(chǎn)生的原因及解決過(guò)程

 更新時(shí)間:2025年01月23日 17:14:57   作者:宣布無(wú)人罪  
文章講述了在使用MyBatis進(jìn)行數(shù)據(jù)庫(kù)操作時(shí)遇到的異常及其解決過(guò)程,首先考慮了事務(wù)問(wèn)題,但未解決,接著懷疑是MyBatis的一級(jí)緩存問(wèn)題,關(guān)閉緩存后問(wèn)題依舊存在,最終發(fā)現(xiàn)是SQL映射文件中的參數(shù)傳遞錯(cuò)誤,使用了錯(cuò)誤的標(biāo)簽導(dǎo)致循環(huán)插入

MyBatisSystemException異常產(chǎn)生原因及解決

異常發(fā)生場(chǎng)景

  • 當(dāng)我使用mybatis對(duì)數(shù)據(jù)庫(kù)操作時(shí)報(bào)的錯(cuò)誤
<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>

嘗試解決問(wèn)題的過(guò)程

1.事務(wù)問(wèn)題

  • 一開(kāi)始,我以為是事務(wù)問(wèn)題,于是在service層加上了@Transactional開(kāi)啟事務(wù)
@Override
@Transactional
public GetData postOrders(List<Long> shoppingCartIds, Long userId) {
    //1.判斷用戶(hù)是否存在
    if (msUserMapper.FindUser(userId) == null) {
        GetData getData=new GetData(500,"無(wú)此賬號(hào)",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.查詢(xún)購(gòu)物車(chē)商品數(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)在想想,真是沒(méi)抓住重點(diǎn),看錯(cuò)了代碼的報(bào)錯(cuò)信息

2.MyBatis一級(jí)緩存問(wèn)題

  • 接著排查發(fā)現(xiàn)發(fā)現(xiàn)查詢(xún)出的數(shù)據(jù)與同樣的代碼在數(shù)據(jù)庫(kù)里不一樣,人當(dāng)場(chǎng)傻了
  • 面向百度編程后認(rèn)為是出現(xiàn)了MyBatis一級(jí)緩存問(wèn)題
  • 于是在yml文件中關(guān)閉關(guān)閉了MyBatis一級(jí)緩存
# 配置mybatis
mybatis:
  # mapper配置文件
  mapper-locations: classpath:mapper/*.xml
  # resultType別名,沒(méi)有這個(gè)配置resultType包名要寫(xiě)全,配置后只要寫(xiě)類(lèi)名
  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è)置一級(jí)緩存關(guān)閉,mybatis默認(rèn)開(kāi)啟
  • 當(dāng)然還是不對(duì),無(wú)可奈何之下,我只能回歸最初的報(bào)錯(cuò)信息,意思大概是文件映射有問(wèn)題

問(wèn)題的產(chǎn)生及其原因

  • 沒(méi)辦法,如果真是映射出了錯(cuò),那就只能一個(gè)一個(gè)排查過(guò)去了
  • 終于在結(jié)合了springboot的報(bào)錯(cuò)日志
  • 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í)行的語(yǔ)句中,三個(gè)問(wèn)號(hào)插入的值是固定的0,1,2
  • 也就是循環(huán)插入出了問(wèn)題
  • 果然,我錯(cuò)誤的使用了標(biāo)簽index

解決方式

  • 只要認(rèn)真檢查mybatis的映射文件,我的話(huà)是把標(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é)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

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

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

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

    Mybatis-Plus中的查詢(xún)指定字段

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

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

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

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

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

    springboot用thymeleaf模板的paginate分頁(yè)完整代碼

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

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

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

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

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

    Spring Boot Admin實(shí)踐詳解

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

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

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

最新評(píng)論