最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

異常發(fā)生場景

  • 當(dāng)我使用mybatis對數(shù)據(jù)庫操作時(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>

嘗試解決問題的過程

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),看錯(cuò)了代碼的報(bào)錯(cuò)信息

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別名,沒有這個(gè)配置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)錯(cuò)信息,意思大概是文件映射有問題

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

  • 沒辦法,如果真是映射出了錯(cuò),那就只能一個(gè)一個(gè)排查過去了
  • 終于在結(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í)行的語句中,三個(gè)問號插入的值是固定的0,1,2
  • 也就是循環(huán)插入出了問題
  • 果然,我錯(cuò)誤的使用了標(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é)

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

相關(guān)文章

  • Java HashMap底層實(shí)現(xiàn)原理

    Java HashMap底層實(shí)現(xiàn)原理

    HashMap在不同的JDK版本下的實(shí)現(xiàn)是不同的,在JDK1.7時(shí),HashMap 底層是通過數(shù)組+鏈表實(shí)現(xiàn)的;而在JDK1.8時(shí),HashMap底層是通過數(shù)組+鏈表或紅黑樹實(shí)現(xiàn)的,本詳細(xì)介紹了HashMap底層是如何實(shí)現(xiàn)的,需要的朋友可以參考下
    2023-05-05
  • mybatis中注解與xml配置的對應(yīng)關(guān)系和對比分析

    mybatis中注解與xml配置的對應(yīng)關(guān)系和對比分析

    這篇文章主要介紹了mybatis中注解與xml配置的對應(yīng)關(guān)系和對比分析,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • java安全編碼指南之:Mutability可變性詳解

    java安全編碼指南之:Mutability可變性詳解

    這篇文章主要介紹了java安全編碼指南之:Mutability可變性詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • 一文讀懂Java多線程背后的故事

    一文讀懂Java多線程背后的故事

    Java是一種廣泛使用的編程語言,而多線程是Java程序員必不可少的一部分,Java的多線程支持具有確保數(shù)據(jù)同步、最大化利用CPU資源、并行處理任務(wù)等眾多優(yōu)點(diǎn),本文從實(shí)際應(yīng)用場景出發(fā),為您詳細(xì)介紹 Java 多線程的各個(gè)方面的實(shí)際應(yīng)用及背景
    2023-06-06
  • 一文教你搞懂SpringBoot自定義攔截器的思路

    一文教你搞懂SpringBoot自定義攔截器的思路

    在開發(fā)中,都離不開攔截器的使用,比如說在開發(fā)登錄功能時(shí),實(shí)現(xiàn)權(quán)限管理功能時(shí)等,這篇文章主要帶大家使用SpringBoot梳理自定義攔截器的思路,需要的可以參考一下
    2023-08-08
  • Java中Comparable與Comparator的區(qū)別解析

    Java中Comparable與Comparator的區(qū)別解析

    這篇文章主要介紹了Java中Comparable與Comparator的區(qū)別解析,實(shí)現(xiàn)Comparable接口,重寫compareTo方法,一般在實(shí)體類定義的時(shí)候就可以選擇實(shí)現(xiàn)該接口,提供一個(gè)默認(rèn)的排序方式,供Arrays.sort和Collections.sort使用,需要的朋友可以參考下
    2024-01-01
  • MyBatis-Plus執(zhí)行SQL分析打印過程

    MyBatis-Plus執(zhí)行SQL分析打印過程

    這篇文章主要介紹了MyBatis-Plus執(zhí)行SQL分析打印過程,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • 使用mybatisPlus生成oracle自增序列遇到的坑及解決

    使用mybatisPlus生成oracle自增序列遇到的坑及解決

    這篇文章主要介紹了使用mybatisPlus生成oracle自增序列遇到的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • SpringBoot+Mybatis項(xiàng)目使用Redis做Mybatis的二級緩存的方法

    SpringBoot+Mybatis項(xiàng)目使用Redis做Mybatis的二級緩存的方法

    本篇文章主要介紹了SpringBoot+Mybatis項(xiàng)目使用Redis做Mybatis的二級緩存的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • 你的Idea還有BUG嗎不妨試試另一個(gè)開發(fā)神器

    你的Idea還有BUG嗎不妨試試另一個(gè)開發(fā)神器

    Spring Tool Suite(STS)就是一個(gè)基于Eclipse的開發(fā)環(huán)境, 用于開發(fā)Spring應(yīng)用程序。本文給大家給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2020-12-12

最新評論

司法| 桑日县| 海南省| 闵行区| 金昌市| 绿春县| 普安县| 屏东县| 都兰县| 鸡泽县| 岢岚县| 磐安县| 渭南市| 武安市| 临漳县| 方正县| 佳木斯市| 曲松县| 扶风县| 武清区| 望奎县| 郓城县| 清新县| 潜江市| 钦州市| 揭阳市| 繁昌县| 万全县| 吕梁市| 改则县| 红河县| 合川市| 江阴市| 哈巴河县| 石棉县| 水富县| 昭觉县| 张家港市| 南平市| 如东县| 盱眙县|