接口body传的是这种数据格式的:
{"boxNo":"300015050009","items":[{"flag":"false","groupName":"test","name":"监控点1","value":"1"},{"flag":"false","groupName":"test","name":"监控点2","value":"2"},{"flag":"false","groupName":"test","name":"监控点3","value":"3"},{"flag":"false","groupName":"test","name":"监控点4","value":"4"}]}
1.第一步从mysql里面获取数据。
代码如下:
public class DBDate{
public List<Items> getDBDate() throws Exception{
List<Items> list = new ArrayList<Items>();
Connection conn=null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
long start=System.currentTimeMillis(); //开始时间
//建立连接
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); //数据库地址,用户名,密码
long end = System.currentTimeMillis();
System.out.println(conn); //结束时间
System.out.println("连接耗时:"+(end - start)+"ms"); //结束时间-开始时间=耗时时间(ms)
//创建Statement对象
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from writevalue where groupname='test'"); //查询数据库的表的sql语句where groupname='test1' limit 10
System.out.println("groupname\tname\tflag\tvalue"); //输出数据库的表结构参数
while (rs.next()) {
System.out.println(rs.getString(2)+"\t"+rs.getString(3)
+"\t"+rs.getString(4)+"\t"+rs.getString(5)); //输出数据库里面所有的查询的3个参数数据
Items items = new Items(); //new了一个对象
/* items.setDmonId(rs.getString(2));*/ // 参数放到对象里面
items.setGroupName(rs.getString(2));
items.setName(rs.getString(3));
items.setFlag(rs.getString(4));
items.setValue(rs.getString(5));
list.add(items); //赋好值的数据放到数组里面
}
}catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (conn != null) {
conn.close(); //关闭连接
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
public class Fbox {
private String boxNo; //盒子号
private List<Items> items; //集合
public String getBoxNo() {
return boxNo;
}
public void setBoxNo(String boxNo) {
this.boxNo = boxNo;
}
public List<Items> getItems() {
return items;
}
public void setItems(List<Items> items) {
this.items = items;
}
}
public class Items {
private String groupName;
private String name;
private String flag;
private String value;
public String getGroupName() {
return groupName;
}
public void setGroupName(String groupName) {
this.groupName = groupName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getFlag() {
return flag;
}
public void setFlag(String flag) {
this.flag = flag;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Items{" +
"groupName='" + groupName + '\'' +
", name='" + name + '\'' +
", flag='" + flag + '\'' +
", value='" + value + '\'' +
'}';
}
}
package com.zking;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.jar.JarOutputStream;
/**
* @version 1.0
* @Description: 测试批量接口写值,从数据库获取数据提交接口。测试接口提交的速度
* @author: 曾晶晶
* @Date: 2020/9/28 10:46
* @Content-Type: json
* @请求方式: Post
*/
public class WriteValue {
private static String SEND_TP_URL = "http://zjj.com/api/v2/box/multi"; //接口地址
public static String send(String url, Object o) {
String json = new Gson().toJson( o );
String result = null;
try {
result = HttpRequest.post( url )
.timeout( 30000 )
.body( json, ContentType.JSON.toString() ).header("Authorization", "Bearer eyJhbGciOiJSUzI1NiIsImtpZCI6Ijg2") // token ,删除了一些
.header("Content-Type", "application/json")
.execute()
.body();
} catch (HttpException e) {
e.printStackTrace();
}
return result;
}
public static void sendfbox1()throws Exception{
DBDate dbDate = new DBDate();
List<Items> items = dbDate.getDBDate(); //从数据库获取数据
List<Items> list = new ArrayList<Items> ();
for (int i = 0; i < items.size(); i++) {
System.out.println(items.get(i)); //循环输出数据库对象
list.add(items.get(i)); //对象放到集合中
}
System.out.println(list);
System.out.println("................................................................................................................");
try {
JSONObject obj =new JSONObject();
obj.put("boxNo","300015050009");
obj.put("items",list); //再将集合中的数据放到对象中
System.out.println(obj);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");//设置日期格式
System.out.println("开始时间"+df.format(new Date()));// new Date()为获取当前系统时间
long start = System.currentTimeMillis();
String ret= send(SEND_TP_URL,obj);
long end = System.currentTimeMillis();
long time=end - start;
System.out.println("耗时:" +time +"ms");
System.out.println("结束时间"+df.format(new Date()));
System.out.println(SEND_TP_URL);
System.out.println(ret);
System.out.println("....................................................................................");
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
for(int i = 0; i <1; i++){
try {
sendfbox1();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
根据数据库的数据一次提交到接口,代码里面提交到接口的耗时时间。
demo我已经上传到我的资源里啦,想要下载的小伙伴可以去我的资源里下载哟。
最最最后~~ 喜欢博主的给博主点个关注点个赞鸭 ٩(๑>◡<๑)۶