蚂蚁森林数据库造数

基于之前的数据库建模方法,我们设计好数据库之后还需要往数据库中填充数据以供程序测试使用

建表如下:
蚂蚁森林数据库造数_第1张图片
energy
蚂蚁森林数据库造数_第2张图片
friend
在这里插入图片描述
steal
蚂蚁森林数据库造数_第3张图片
user
蚂蚁森林数据库造数_第4张图片

我们需要往该空数据库中填入适当的测试数据,虽然各个表之间有主外键关系,但是我们建库时不需要考虑外键(很多大公司也是这么做),暂时只要给到合适的数据类型和数据范围就行了。

观察得到其中需要随机产生的数据类型大概有:
整形,可变长字符串,时间日期,bool型

So,先写产生这些随机数据的方法

package com.foo.ant;

import java.util.Date;
import java.util.Random;

/**
 * 随机工具类
 * 

* 日期: 2019年8月20日 上午9:57:27 *

* * @author HEX * @version 1.0 */ public class RandomUtils { private static final String[] POOL_NICKNAME = "子赫,祺祾,朝彦,圣鹏,新哲,鼎明,楠明,裕昊,智棋,皓福,敬坤,渊荣,景尧,敬洪,朝实,善玮,朝棋,朝寒,楷林,景瑞,琪洋,捷杰,寒柏,敬易,涛光,鼎益,朝波,新明,昌震,皓翔,乔豪,敬轩,尚兴,皓清,裕明,杰宇,岩乐,乔宁,乔诚,川善,东辉,皓宁,雄杰,金锋,涛宇,楠峻,靖轩,尚欧,琪哲,皓景,昊辉,雨锋,智凯,捷旭,雨逸,宜磊,川峰,智睿,尚啸,铭晨,莱远,宝峰,涆安,腾波,星儒,玥隆,日麒,震可,远皓,宇正,铭振,蓄景,曜为,昂康,豪嘉,晟良,逸凌,珹耀,轩越,燎段,炎殿,淼瀚,昌烨,黎伟,昱名,彭奎,爵立,嘉珂,行成,曦栋,宝琳,丹梅,子婧,春霞,影梅卿蓉,亚萍,于娜,欣怡,燕晓杰文,纪颖,怡瑶,炎琳,婉婷歌玲,琬玲,彩红,妍茜,倡文佳艳,会霞,悦颖,泓茹,宇丞红英,悦婷,品怡,晓梅,桂燕安娅,韧颖,彩燕,钊莹,涟颖暖怡,筱雪,婷旭,苏娟,芷茹维娟,羽莹,太文,湘媛,曦秀惠玲,琴轩,丽莉,肖倩,薏冉林娜,仪琳,超莹,俞莹,杨梅嫦娥,昊怡,玲玉,燕齐,锦洁安娜,嘉洁,伟娜,紫玲,雅莉蓉洁,诗茹,荣霞,瑶琴,媛娜瑜莉,燕平,爱茹,志芳,若瑶,婉怡,爱萍,文英,艺萍,金文桂霞,娟娣,秀颖,翠萍,莉轩姗婷,雪娟,沁媛,章文,曼玲冬雪,悦驰,景怡,瑞艳,光文悦淇,煊悦,玲丽,媛雪,巧娜柏文,淑琳,心悦,婷秀,科翰莉娜,婧文,泽琴,宇玲,生文智琳,小妹,爱琴,崇芳,焱霞晶婧,妍彤,泽瑛,婷丽,晴芳海萍,秀芳,腊梅,琳淼,倩成依玲,欣秀,午瑶,惠琴,悦张成娥,秋艳,昕妍,睿琳,青霞艳霞,鸾瑶,莉娅,高妍,兰娟" .split(","); private static final String[] POOL_HEAD_PORTRAINT = { "photo.jpg", "xiongda.jpg" }; private static Random random = new Random(); private static int USER_ID_SEED = 1; private static int ENERGY_ID_SEED = 1; /** * 随机生成一个正整数 */ public static int randomInteger(int min, int max) { return random.nextInt(max - min + 1) + min; } /** * 随机生成一个boolean */ public static boolean randomBoolean() { return random.nextBoolean(); } /** * 随机生成一个头像 * @return */ public static String randomHeadPortraint() { int n = random.nextInt(POOL_HEAD_PORTRAINT.length); return POOL_HEAD_PORTRAINT[n]; } /** * 随机生成一个过去的时间 * * @param passDays * 表示过去几天内 * @Return date */ public static Date randomPassDate(int passDays) { // 当前时间 Date now = new Date(); // 随机生成一个1-passHours*60*60*1000范围内的整数, 表示毫秒数 long mills = randomInteger(0, passDays) * 24L * 60L * 60L * 1000L; // long mills = (long)(Math.random() * 24 *60 *60 * 100); // 得到一个过去的日期 Date passDate = new Date(now.getTime() - mills); return passDate; } /** * 随机生成一个中文的昵称 */ public static String randomNickName() { int n = random.nextInt(POOL_NICKNAME.length); return POOL_NICKNAME[n]; } /** * 随机生产一个用户 * @return */ public static User randomUser() { User user = new User(); // 用随机值给这个user填充属性 user.setUserId(USER_ID_SEED++); user.setNickName(randomNickName()); user.setHeadPortraint(randomHeadPortraint()); user.setEnergy(randomInteger(2000, 20000)); return user; } /** * 随机生产一个能量球 * @return */ public static Energy randomEnergy() { Energy energy = new Energy(); energy.setEnergyId(ENERGY_ID_SEED++); energy.setOwnerUserId(randomInteger(1, 1000)); energy.setInitValue((short)randomInteger(50, 100)); energy.setRemainValue((short)randomInteger(0, 100)); short initValue = (short)randomInteger(50, 100); energy.setInitValue(initValue); short remainValue = (short)randomInteger(0, initValue); energy.setRemainValue(remainValue); boolean useup = randomBoolean(); energy.setUseUp(useup); Date createTime = randomPassDate(2 * 365 * 24); energy.setCreateTime(createTime); return energy; } /** * 随机生产一个Friend * @return */ public static Friend randomFriend() { Friend friend = new Friend(); friend.setSrcUserId(randomInteger(1, 1000)); friend.setDestUserId(randomInteger(1, 1000)); friend.setFriendTime(randomPassDate(2 * 365)); return friend; } /** * 随机生产一个Steal记录 * @return */ public static Steal randomSteal() { Steal steal = new Steal(); steal.setStealUserId(randomInteger(1, 1000)); steal.setEnergyId(randomInteger(1, 10000)); steal.setStealTime(randomPassDate(3)); steal.setStealEnergyValue(randomInteger(1, 50)); return steal; } }

以energy表为例

首先建立Energy对象,属性对应数据库

package com.foo.ant.entity;

import java.util.Date;

public class Energy { 
	private long energyId;
	private long ownerUserId;
	private Date createTime;
	private short initValue;
	private short remainValue;
	private boolean useUp;
	

	@Override
	public String toString() {
		return "Energy [energyId=" + energyId + ", ownerUserId=" + ownerUserId + ", createTime=" + createTime
				+ ", initValue=" + initValue + ", remainValue=" + remainValue + ", useUp=" + useUp + "]";
	}

	public long getEnergyId() {
		return energyId;
	}

	public void setEnergyId(long energyId) {
		this.energyId = energyId;
	}

	public long getOwnerUserId() {
		return ownerUserId;
	}
	
	public void setOwnerUserId(long ownerUserId) {
		this.ownerUserId = ownerUserId;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public short getInitValue() {
		return initValue;
	}

	public void setInitValue(short initValue) {
		this.initValue = initValue;
	}

	public short getRemainValue() {
		return remainValue;
	}

	public void setRemainValue(short remainValue) {
		this.remainValue = remainValue;
	}

	public boolean isUseUp() {
		return useUp;
	}

	public void setUseUp(boolean useUp) {
		this.useUp = useUp;
	}

}

其他实体同理

利用jdbc把数据存入数据库内

package com.foo.ant;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
/**
 * 把随机数据存入数据库
 * 日期: 2019年8月20日 下午2:53:52
 * @author HEX
 * @version 1.0
 */
public class StorePersistante {
	public static void main(String[] args) {
		// 1000个user
		ArrayList users = new ArrayList<>();
		for (int i = 0; i < 1000; i++) {
			users.add(RandomUtils.randomUser());
		}
		// 10000个engergy
		ArrayList energies = new ArrayList<>();
		for (int i = 0; i < 10000; i++) {
			energies.add(RandomUtils.randomEnergy());
		}
		// 50000个friend, 随机过程中, 可能产生这样的情况: { 2, 3, 2018年4月1号 }, { 2, 3, 2019年3月3号 }, 
		HashMap friends = new HashMap<>();
		Friend friend;
		for (int i = 0; i < 50000; i++) {
			friend = RandomUtils.randomFriend();
			friends.put(friend.getSrcUserId() + "-" + friend.getDestUserId(), friend);
		}

		// 50000个steal
		HashMap steals = new HashMap<>();
		Steal steal;
		for (int i = 0; i < 50000; i++) {
			steal = RandomUtils.randomSteal();
			steals.put(steal.getStealUserId() + "-" + steal.getEnergyId(), steal);
		}

		String url = "jdbc:mysql://localhost:3306/ant?rewriteBatchedStatements=true";
		String user = "root";
		String password = "root";
		String sql_insert_user = "INSERT INTO `user`(user_id, nick_name, head_portrait, energy) values(?, ?, ?, ?)";
		String sql_insert_friend = "INSERT INTO friend(src_user_id, dest_user_id, friend_time) values(?, ?, ?)";
		String sql_insert_energy = "INSERT INTO energy(energy_id, owner_user_id, create_time, init_value, remain_value, use_up) values(?, ?, ?, ?, ?, ?)";
		String sql_insert_steal = "INSERT INTO steal(steal_user_id, energy_id, steal_energy_value, steal_time) VALUES(?, ?, ?, ?)";
		long start = System.currentTimeMillis();
		Connection conn = null;
		PreparedStatement stmt;
		try {
			conn = DriverManager.getConnection(url, user, password); // only 1 connection
			conn.setAutoCommit(false); // --------------------------------------- 开启事务

			// 保存users
			stmt = conn.prepareStatement(sql_insert_user); // only 1 statement
			for (User u : users) {
				stmt.setLong(1, u.getUserId());
				stmt.setString(2, u.getNickName());
				stmt.setString(3, u.getHeadPortraint());
				stmt.setInt(4, u.getEnergy());
				stmt.addBatch(); // 添加到批处理
			}
			stmt.executeBatch(); // 一次性执行
			System.out.println("--- users saved ---");

			// 保存friends
			stmt = conn.prepareStatement(sql_insert_friend);
			Friend f;
			for (Entry entry : friends.entrySet()) {
				f = entry.getValue();
				stmt.setLong(1, f.getSrcUserId());
				stmt.setLong(2, f.getDestUserId());
				stmt.setObject(3, f.getFriendTime());
				stmt.addBatch();
			}
			stmt.executeBatch();
			System.out.println("--- friends saved ---");

			// 保存energies
			stmt = conn.prepareStatement(sql_insert_energy);
			for (Energy energy : energies) {
				//energy_id, owner_user_id, create_time, init_value, remain_value, use_up
				stmt.setLong(1, energy.getEnergyId());
				stmt.setLong(2, energy.getOwnerUserId());
				stmt.setObject(3, energy.getCreateTime());
				stmt.setShort(4, energy.getInitValue());
				stmt.setShort(5, energy.getRemainValue());
				stmt.setBoolean(6, energy.isUseUp());
				stmt.addBatch();
			}
			stmt.executeBatch();
			System.out.println("--- energies saved ---");
			
			// 保存steals
			stmt = conn.prepareStatement(sql_insert_steal);
			Steal s;
			for(Entry entry : steals.entrySet()) {
				s = entry.getValue();
				//steal_user_id, energy_id, steal_energy_value, steal_time
				stmt.setLong(1, s.getStealUserId());
				stmt.setLong(2, s.getEnergyId());
				stmt.setInt(3, s.getStealEnergyValue());
				stmt.setObject(4, s.getStealTime());
				stmt.addBatch();
			}
			stmt.executeBatch();
			System.out.println("--- steals saved ---");
			
			conn.commit(); // ------------------------------------------------ 提交事务
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != conn)
				try {
					conn.close();
				} catch (SQLException e) {
					e.printStackTrace();
				}
		}
		long end = System.currentTimeMillis();
		System.out.println("ok, " + (end - start) + " milliseconds cost.");

	}
}

你可能感兴趣的:(Java,Java,数据库)