源碼舉例:
package com.gxr.imybatisplus.utils; import com.gxr.imybatisplus.entity.TSampleE; import java.math.BigDecimal; import java.sql.Date; import java.sql.Timestamp; import java.util.ArrayList; import java.util.List; import java.util.Random; import java.util.UUID; import java.util.logging.Logger; public class GenObject { private static final Logger logger = Logger.getLogger(GenObject.class.getName()); public static final String HANZI = "趙錢孫李周吳鄭王馮陳褚衛蔣沈韓楊朱秦尤許何呂施張孔曹嚴華金魏陶姜戚謝鄒喻柏水竇章雲蘇潘葛奚范彭郎魯韋昌馬苗鳳花方"; public static final String GENDEL = "男女"; public static final String[] SCHOOLS = {"清華大學", "北京大學", "南京大學", "上海交通大學", "東南大學", "華中科技大學", "武漢大學"}; public static final String[] JOBS = {"商人", "學生", "文職人員", "程序員", "科學家", "醫生", "律師", "司機"}; public static final String REMARK = "隨着公司項目的持續增多、實施工作持續推進,同時也暴露出很多數據從哪出、數據如何用等問題," + "各項目都在按需定制功能和對接數據,這就給公司帶來很大的支出成本"; public static TSampleE genSampleObj(int id, String tableName) { TSampleE sample = new TSampleE(); sample.setTableName(tableName); sample.setId(id); sample.setName(getRandomString(HANZI, 3)); sample.setGender(getRandomString(GENDEL, 1)); // sample.setHeight(175.56F); sample.setHeight(getRandomFloat(150F, 200F)); // sample.setWeight(67.57); sample.setWeight(getRandomDouble(50, 120, 2)); sample.setAge(getRandomInt(16, 100)); int ageTime = sample.getAge() * 365 * 24 * 360; // int類型的數據范圍有限 Date date = new Date((System.currentTimeMillis() / 1000 / 10 - ageTime) * 1000 * 10); sample.setBrithday(date); // sample.setSchool("清華大學"); sample.setSchool(getRandomItem(SCHOOLS)); // sample.setJob("商人"); sample.setJob(getRandomItem(JOBS)); sample.setRemarks(getUUID()); sample.setCreateTime(new Timestamp(System.currentTimeMillis())); System.out.println("-- 生成數據: " + sample.toString()); return sample; } public static List<TSampleE> getSampleList(int startNum, int num, String tableName) { List<TSampleE> list = new ArrayList<>(); for (int i = 0; i < num; i++) { TSampleE sample = genSampleObj(startNum + i, tableName); list.add(sample); } return list; } /** * 根據字符串隨機獲取定長字符串 */ public static String getRandomString(String str, int length) { // String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; // String str = "趙錢孫李周吳鄭王馮陳褚衛蔣沈韓楊朱秦尤許何呂施張孔曹嚴華金魏陶姜戚謝鄒喻柏水竇章雲蘇潘葛奚范彭郎魯韋昌馬苗鳳花方"; Random random = new Random(); StringBuffer sb = new StringBuffer(); for (int i = 0; i < length; i++) { int number = random.nextInt(str.length()); sb.append(str.charAt(number)); } return sb.toString(); } /** * 在字符串數組中隨機獲取一個值 */ public static String getRandomItem(String[] strs) { Random random = new Random(); int number = 0; number = random.nextInt(strs.length); return strs[number]; } /** * 隨機生成一個范圍內的整數 */ public static int getRandomInt(int min, int max) { if (max < min) { logger.fine("max < min"); return max; } else if (min == max) { return min; } return (int) (min + Math.random() * (max - min)); } /** * 在指定范圍中,隨機獲取一個double數 * * @param scale :小數點位數 */ public static double getRandomDouble(final double min, final double max, int scale) { if (max < min) { logger.fine("max < min"); return max; } else if (min == max) { return min; } double d = min + ((max - min) * new Random().nextDouble()); d = new BigDecimal(d).setScale(scale, BigDecimal.ROUND_HALF_UP).doubleValue(); return d; } /** * 在指定范圍中,隨機獲取一個float數 */ public static float getRandomFloat(final float min, final float max) { if (max < min) { logger.fine("max < min"); return max; } else if (min == max) { return min; } return min + ((max - min) * new Random().nextFloat()); } /** * 獲取一個不重復的隨機字符串 */ public static String getUUID() { UUID uuid = UUID.randomUUID(); String str = uuid.toString(); // System.out.println("原始UUID: " + str); // 去掉"-"符號 String temp = str.substring(0, 8) + str.substring(9, 13) + str.substring(14, 18) + str.substring(19, 23) + str.substring(24); // System.out.println("去掉\"-\"符號: " + temp); return temp; } }