It show you that the sequence table can manage all tables.
// These are Schema Desgins
CREATE TABLE zz_app
(
id BIGINT UNSIGNED NOT NULL DEFAULT '0',
app_id VARCHAR(45) NOT NULL,
app_aaa VARCHAR(45) NULL,
app_bbb VARCHAR(45) NULL,
app_status_flag CHAR(1) NULL,
insert_time TIMESTAMP NOT NULL,
update_time TIMESTAMP NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE INDEX zz_app_idx1 ON zz_app(app_id);
CREATE INDEX zz_app_idx2 ON zz_app(insert_time);
CREATE TABLE zz_sequence
(
seq_name VARCHAR(30) NOT NULL,
id BIGINT UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (seq_name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
----------------------------------------------------------------------
// Update sequence number into the Mysql
<insert id="updateSequece" parameterType="map">
UPDATE
zz_sequence
SET
id=LAST_INSERT_ID(id+1)
WHERE
seq_name = #{seqName}
<selectKey resultType="Long" order="AFTER">
SELECT
LAST_INSERT_ID()
</selectKey>
</insert>
// Insert a data into the Mysql
<insert id="insertApp" parameterType="map">
<selectKey keyProperty="id" resultType="Long" order="BEFORE">
SELECT
id
FROM
zz_sequence
WHERE
seq_name = #{seqName};
</selectKey>
INSERT INTO zz_app (
id,
app_id,
app_aaa,
app_bbb,
app_status_flag,
insert_time,
update_time
) VALUES (
#{id},
#{appId},
#{appAaa},
#{appBbb},
#{appStatusFlag},
now(),
now()
)
</insert>
No comments:
Post a Comment