H2數據庫用來作單測數據庫,能夠自定義初始化數據,不用擔憂數據庫內容更改形成單測跑不過問題,不過H2數據庫跟實際使用的Mysql仍是有必定區別。java
--批量更新(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>
<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.sqlsql
-- 表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 ;