通过HttpClient发起Get请求,获取Json数据,然后转为java数据,然后批量保存数据库;
程序员文章站
2022-06-23 16:57:45
Json转java所需Jar包: commons-beanutils-1.8.0.jar,commons-collections-3.2.1.jar,commons-lang-2.5.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.jar,json-lib-2 ......
json转java所需jar包: commons-beanutils-1.8.0.jar,commons-collections-3.2.1.jar,commons-lang-2.5.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.jar,json-lib-2.4.jar 下载地址:https://pan.baidu.com/s/1qhhymdlsk95d7pyeygtfjg 提取码:ddso
httpclient-4.2.2.jar,httpcore-4.2.2.jar 下载地址:https://pan.baidu.com/s/1xojyz4uqdqfsceyho8oyxg 提取码:1lws |
1 import org.apache.http.httpentity; 2 import org.apache.http.httpresponse; 3 import org.apache.http.client.clientprotocolexception; 4 import org.apache.http.client.httpclient; 5 import org.apache.http.client.methods.httpget; 6 import org.apache.http.impl.client.defaulthttpclient; 7 import org.apache.http.util.entityutils; 8 import org.springframework.beans.factory.annotation.autowired; 9 import org.springframework.stereotype.controller; 10 import org.springframework.web.bind.annotation.requestmapping; 11 12 import net.sf.json.jsonarray; 13 import net.sf.json.jsonobject; 14 import net.sf.json.jsonconfig; 15 16 public string courselist() { 17 string url = ""; 18 httpclient httpclient = new defaulthttpclient(); 19 httpget httpget = new httpget(url); 20 string json = null; 21 try { 22 // 通过httpclient get请求返回json数据 23 httpresponse response = httpclient.execute(httpget); 24 httpentity entity = response.getentity(); 25 if (entity != null) { 26 json = entityutils.tostring(entity, "utf-8").trim(); 27 jsonobject jsonmodelobject = jsonobject.fromobject(json); 28 // 通过字典值拿取实体类数据集合 29 string model = jsonmodelobject.getstring("model"); 30 jsonobject jsoncurpagedataobj = jsonobject.fromobject(model); 31 // 拿取分页 当前页数据 32 string curpagedata = jsoncurpagedataobj.getstring("curpagedata"); 33 // 转为jsonarray数组 34 jsonarray jarray = jsonarray.fromobject(curpagedata); 35 // 转为java数组 36 list<?> list = jsonarray.tolist(jarray, new course(), new jsonconfig()); 37 int result = courseservice.insertcourse(list); 38 if (result > 0) { 39 system.out.println("成功"); 40 } 41 } 42 } catch (clientprotocolexception e) { 43 e.printstacktrace(); 44 } catch (ioexception e) { 45 e.printstacktrace(); 46 } finally { 47 httpget.abort(); 48 } 49 return "success"; 50 }
<foreach>批量插入数据:
<insert id="insert" parametertype="map"> insert into course (courseid,coursename,courseschool,courseteacher,coursebrief,courselearnnum,coursetype) values <foreach collection="list" item="item" index="index" separator=","> (#{item.courseid},#{item.coursename},#{item.courseschool},#{item.courseteacher},#{item.coursebrief},#{item.courselearnnum},#{item.coursetype}) </foreach> </insert>