1. 程式人生 > >H2數據庫做單測數據庫時踩到的坑

H2數據庫做單測數據庫時踩到的坑

tid odi for 定義 reac mar incr repl ble

H2數據庫用來做單測數據庫,可以自定義初始化數據,不用擔心數據庫內容更改造成單測跑不過問題,不過H2數據庫跟實際使用的Mysql還是有一定區別。

1. H2數據庫不支持Mysql的批量更新功能,支持批量插入

--批量更新(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>

2. H2數據庫不支持Mysql的replace into 語法

3. H2數據庫初始化時不允許出現相同的UK

    <jdbc:embedded-database id="dataSource" type="H2">
        <jdbc:script location="classpath:H2_TYPE.sql"/>
        <jdbc:script location="classpath:INIT_TABLE.sql"/>
        <jdbc:script location="classpath:INIT_DATA.sql"/>
    </jdbc:embedded-database>

INIT_TABLE.sql

-- 表A
CREATE TABLE `table_A` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 自增主鍵,
  `bill_no` varchar(45) DEFAULT NULL COMMENT 單號,
  `bill_type` tinyint(4) unsigned NOT NULL DEFAULT 0 COMMENT 單據類別,
  `created_time` datetime NOT NULL DEFAULT 1000-01-01 00:00:00 COMMENT 創建時間,
  `modify_time` datetime NOT NULL DEFAULT 1000-01-01 00:00:00 COMMENT 修改時間,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_bill_type_bill_no` (`bill_type`,`bill_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

--表A
CREATE TABLE `table_B` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 自增主鍵,
  `bill_no` varchar(45) DEFAULT NULL COMMENT 單號,
  `bill_type` tinyint(4) unsigned NOT NULL DEFAULT 0 COMMENT 單據類別,
  `created_time` datetime NOT NULL DEFAULT 1000-01-01 00:00:00 COMMENT 創建時間,
  `modify_time` datetime NOT NULL DEFAULT 1000-01-01 00:00:00 COMMENT 修改時間,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_bill_type_bill_no` (`bill_type`,`bill_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

運行結果:

Caused by: org.h2.jdbc.JdbcSQLException: Constraint "uk_bill_type_bill_no" already exists; 
SQL statement:CREATE TABLE `table_B` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT ‘自增主鍵‘,
  `bill_no` varchar(45) DEFAULT NULL COMMENT ‘單號‘,
  `bill_type` tinyint(4) unsigned NOT NULL DEFAULT ‘0‘ COMMENT ‘單據類別‘,
  `created_time` datetime NOT NULL DEFAULT ‘1000-01-01 00:00:00‘ COMMENT ‘創建時間‘,
  `modify_time` datetime NOT NULL DEFAULT ‘1000-01-01 00:00:00‘ COMMENT ‘修改時間‘,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uk_bill_type_bill_no` (`bill_type`,`bill_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;

H2數據庫做單測數據庫時踩到的坑