日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

IllegalArgumentException異常產(chǎn)生原因及解決方案

作者:宣布無人罪 更新時間: 2023-12-18 編程語言

IllegalArgumentException異常產(chǎn)生原因及解決方案

01 異常發(fā)生場景

  • 當(dāng)我在使用mybatis執(zhí)行MySQL語法時出現(xiàn)的bug
<!--mybatis的映射-->
<resultMap id="OrdersVoResult" type="OrdersVo">
    <!-- id配置的是主鍵,就是表的主鍵 -->
    <!-- property是實體類的屬性名 -->
    <!-- cloumn是sql查詢出來的字段名 -->
    <id property="orderId" column="order_id"></id>
    <!-- result是其他的字段 -->
    <result property="orderNum" column="order_num"></result>
    <result property="orderTime" column="order_time"></result>
    <collection property="products"  javaType="ordersDtlVo">
        <id property="orderDtlId" column="order_dtl_id"></id>
        <result property="productId" column="product_id"></result>
        <result property="productName" column="product_name"></result>
        <result property="productSellingPrice" column="product_selling_price"></result>
        <result property="num" column="num"></result>
        <result property="productPicture" column="product_picture"></result>
    </collection>
</resultMap>
<select id="selectOrders" resultMap="OrdersVoResult">
SELECT
    t1.order_id,
    t1.create_time,
    t1.order_num,
    t2.order_dtl_id,
    t2.product_id,
    t2.product_name,
    t2.product_selling_price,
    t2.num,
    t2.product_price
    FROM
    orders AS t1
    left JOIN
    orders_dtl AS t2
    ON t1.order_id = t2.order_id
    WHERE
    t1.user_id = #{userId}
</select>

02 異常的產(chǎn)生原因

  • 在數(shù)據(jù)庫里SQL語句的執(zhí)行是沒有問題的

  • 經(jīng)過查詢百度,發(fā)現(xiàn)是非法傳參異常,也就是參數(shù)傳的類型沖突,但是我前前后后對了好幾次參數(shù),確定了實體類參數(shù)對應(yīng)沒有問題

  • 那么問題出在哪里呢?經(jīng)過查詢mybatis3的官方文檔,我找到了問題的所在

  • javaType 一個 Java 類的全限定名,或一個類型別名(關(guān)于內(nèi)置的類型別名,可以參考上面的表格)。 如果你映射到一個 JavaBean,MyBatis 通常可以推斷類型
    ofType 也是一個 Java 類的全限定名,但映射對應(yīng)的參數(shù)是數(shù)組
  • 我的實體類

    public class OrdersVo {
        private Long orderId;
        private String orderNum;
        private Date orderTime;
        private List<OrdersDtlVo> products;
    }
    
  • 很明顯可以看出其中一個參數(shù)是list集合,但我簡單的使用javaType設(shè)置路徑,所以才造成了IllegalArgumentException(參數(shù)映射問題)

03 解決方式

  • 很簡單,在映射集合時,使用ofType映射對應(yīng)的參數(shù)即可

  • 修改后的mybtais映射

  • <resultMap id="OrdersVoResult" type="OrdersVo">
        <!-- id配置的是主鍵,就是表的主鍵 -->
        <!-- property是實體類的屬性名 -->
        <!-- cloumn是sql查詢出來的字段名 -->
        <id property="orderId" column="order_id"></id>
        <!-- result是其他的字段 -->
        <result property="orderNum" column="order_num"></result>
        <result property="orderTime" column="order_time"></result>
        <collection property="products"  ofType="ordersDtlVo">
            <id property="orderDtlId" column="order_dtl_id"></id>
            <result property="productId" column="product_id"></result>
            <result property="productName" column="product_name"></result>
            <result property="productSellingPrice" column="product_selling_price"></result>
            <result property="num" column="num"></result>
            <result property="productPicture" column="product_picture"></result>
        </collection>
    </resultMap>
    <select id="selectOrders" resultMap="OrdersVoResult">
    SELECT
        t1.order_id,
        t1.create_time,
        t1.order_num,
        t2.order_dtl_id,
        t2.product_id,
        t2.product_name,
        t2.product_selling_price,
        t2.num,
        t2.product_price
        FROM
        orders AS t1
        left JOIN
        orders_dtl AS t2
        ON t1.order_id = t2.order_id
        WHERE
        t1.user_id = #{userId}
    </select>
    

原文鏈接:https://blog.csdn.net/2302_77182979/article/details/134484538

  • 上一篇:沒有了
  • 下一篇:沒有了
欄目分類
最近更新