欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > IT业 > h2数据库模拟mysql进行单元测试遇到的问题

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

2024/10/26 5:22:52 来源:https://blog.csdn.net/politeboy/article/details/142975879  浏览:    关键词: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数据库中也是没有的,需要自己定义。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com