自学内容网 自学内容网

h2数据库模拟mysql进行单元测试遇到的问题

使用h2数据库进行springboot的单元测试的时候出现的几个问题

1.h2数据库插入json数据的时候,默认是json String的形式(josn数据入库的时候有转义字符),导致查询出来的json数据在进行处理的时候无法解析成jsonNode

操作:

在单元测试插入数据的时候,在json对应的字段最后加上FORMAT JSON

即 insert into tabelA(json字段,string字段) values(“{‘A":“B”}’ FORMAT JSON, ”string数据段“)即可正确插入json数据(入库的时候无转义符)

2.h2数据库不支持mysql的批量更新操作,也不支持mysql的replace into语句。

--批量更新(H2不支持)
  <update id="increaseBatch"  parameterType="java.util.List">
        <foreach collection="list" item="item" index="index" open="" close="" separator=";">
            update product
            <set>
                quantity = quantity + #{item.updateQuantity}, modify_time = #{item.modifyTime}
            </set>
            where id = #{item.productId}
        </foreach>
    </update>

--批量插入(H2支持)
<insert id="insertItems" keyProperty="id" parameterType="java.util.List" useGeneratedKeys="true">
        <selectKey keyProperty="id" order="AFTER" resultType="long">
            SELECT LAST_INSERT_ID()
        </selectKey>
        INSERT INTO bill_item(
        bill_id,product_id,product_name,product_quantity,product_quantity_after,product_price,product_amount)
        VALUES
        <foreach close="" collection="list" index="index" item="item" open="" separator=",">
            (
            #{item.billId},#{item.productId},#{item.productName},#{item.productQuantity},
            #{item.productQuantityAfter},#{item.productPrice},#{item.productAmount})
        </foreach>
    </insert>

3.mysql中的json_contain等sql函数,在h2数据库中也是没有的,需要自己定义。


原文地址:https://blog.csdn.net/politeboy/article/details/142975879

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!