背景:测试的时候,没有数据,自己一个一个造,很是麻烦,再搞个java接口,似乎也不太方便,这里就可以尝试一下下面这种做法,利用mysql的存储过程来造数据。
示例代码:
delimiter $$ drop procedure if exists generate; CREATE DEFINER=`root`@`localhost` PROCEDURE `generate`( IN num INT ) BEGIN DECLARE chars VARCHAR ( 100 ) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE name VARCHAR ( 10 ) DEFAULT ''; DECLARE hobby VARCHAR ( 25 ) DEFAULT ''; DECLARE id INT UNSIGNED; DECLARE len INT; SET id = 1; DELETE FROM person; WHILE id <= num DO SET len = FLOOR( 1 + RAND()* 10 ); SET fname = ''; WHILE len > 0 DO SET name = CONCAT(name,substring( chars, FLOOR( 1 + RAND()* 62 ), 1 )); SET len = len - 1; END WHILE; SET len = FLOOR( 1+RAND ()* 25 ); SET hobby = ''; WHILE len > 0 DO SET hobby = CONCAT(hobby,SUBSTR( chars, FLOOR( 1 + RAND()* 62 ), 1 )); SET len = len - 1; END WHILE; INSERT INTO person VALUES (id,name,hobby,FLOOR( RAND()* 100 ),FLOOR( RAND()* 2 )); SET id = id + 1; END WHILE; END $$ -- 停掉事务 set autocommit = 0; -- 调用存储过程 call generate(1000000); -- 重启事务 set autocommit = 1;