基于Springboot和Mybatis-Plus,需要往数据库里插入数据,由于一条一条插入太慢了,所以写了个简单的脚本,生成随机Json数据并发送到Springboot,保存到后台。
@RequestMapping({"/facilities1"})
public class FacilitiesOneController {
@Autowired
private FacilitiesOneService facilitiesOneService;
public FacilitiesOneController() {
}
@GetMapping({"/"})
public List<FacilitiesOne> findAll() {
return this.facilitiesOneService.list();
}
@PostMapping//数据库插入方法
public boolean save(@RequestBody FacilitiesOne facilitiesOne) {
return this.facilitiesOneService.saveFacilitiesOne(facilitiesOne);
}
可以看到,SpringBoot构造的可以接收请求链接为:http://localhost:9090/facilities1,一般使用第三方工具发送Post请求,如postman,但是无法实现大规模的数据请求,所以用Python可以连续快速发送请求。
这是生成json数据的Python代码,命名为setdata.py(json构造根据自己需要),这里我生成的数据格式为:
{
'id': '220709100000',
'carId': '川A5G6XL',
'carType': '小型车',
'carSpeed': 74.7,
'carLength': 4.2,
'arriveTime': '2022-07-09 04:42:40',
'carState': 1,
'carSituation': '正常'
}
可以根据自己的需求更改json内容
import random
import time
import datetime
def genrndchar(metachar):
return metachar[int(random.random() * len(metachar))]
def license_plate():
head = ["渝","川","渝","川","渝","川","渝","渝","贵","陕","桂"]
s = genrndchar(head)
s = s + genrndchar(['A', 'B', 'C', 'D', 'E', 'H'])
#s = s + '-'
s = s + genrndchar(['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'])
for i in range(4):
s = s + genrndchar(
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])
return s
def random_speed():
return random.randint(300,1200)/10
def random_type():
type_list = ["小型车","大型车","小型车","小型车","小型车","大型车","超大型车"]
length_list = [4.2,5.6,4.3,4.1,4.0,5.8,8.7]
i = random.randint(0,len(type_list)-1)
return type_list[i],length_list[i]
def time_factory(start_time=(2022, 7, 1, 0, 0, 0, 0, 0, 0), end_time=(2022, 7, 30, 23, 59, 59, 0, 0, 0)):
start = time.mktime(start_time) # 生成开始时间戳
end = time.mktime(end_time) # 生成结束时间戳
t = random.randint(start, end) # 在开始和结束时间戳中随机取出一个
date_touple = time.localtime(t) # 将时间戳生成时间元组
date = time.strftime("%Y-%m-%d %H:%M:%S", date_touple) # 将时间元组转成格式化字符串
return date
def random_state():
state_list = ["正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","正常",
"正常","正常","正常",
"正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","正常","超速","遮挡号牌","无牌","逆行","违规变道"]
code_list = [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,3,4,5,6]
i = random.randint(0,len(code_list)-1)
#print(len(state_list)==len(code_list))
return state_list[i],code_list[i]
def code_state(i):
if i > 10000:
res = str(i)
elif i > 1000 & i<10000:
res = "0" + str(i)
elif i > 100 & i < 1000:
res = "00" + str(i)
elif i > 10 & i < 100:
res = "000" + str(i)
elif i < 10:
res = str("0000") + str(i)
return res
def random_id(i):
date = time_factory()
res = date[2:4] + date[5:7] + date[8:10] + str(1) +"%05d"%(i)
return date,res
def concat_to_json(i):
car_type,car_length = random_type()
time_res,id_res = random_id(i)
state_res,code_res = random_state()
res = {
"id": id_res,
"carId": license_plate(),
"carType": car_type,
"carSpeed": random_speed(),
"carLength": car_length,
"arriveTime": time_res,
#"carImage": None,
"carState": code_res,
"carSituation": state_res
}
return res
这是发送json数据的Python代码,命名为senddata.py
import setdata
import json
import requests
#jsondata = json.dumps(jsontext,indent = 4,ensure_ascii = False,separators=(',', ': '))
headers = {
'User-Agent': 'ozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.102 Safari/537.36'
}
for i in range(500):
jsontext = setdata.concat_to_json(i)
r = requests.post("http://localhost:9090/facilities3/",headers=headers,json=jsontext)
#print(r.text)
print("正在发送第%d条请求"%(i))
提示:请求发送成功之后会返回True,测试的时候可以
print(r.text)
,如果为true就可以大规模发送数据。