每天定时查询CSDN博客访问量,并通过echarts进行展示
生活随笔
收集整理的這篇文章主要介紹了
每天定时查询CSDN博客访问量,并通过echarts进行展示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
-
效果展示
- Github鏈接:https://github.com/qushencn/springboot
- Csdn下載鏈接:https://download.csdn.net/download/weixin_38959210/11082611
?只寫了Controller到dao層嗎,沒有寫中間的Service層
后臺代碼:@Controller:
@Controller public class UserController {@Autowiredprivate FwlMapper fwlmapper;@RequestMapping("/hello")public String HelloSpringBoot() {System.out.println("從Controller跳轉前臺頁面");return "user/index";}@RequestMapping("/csdn")@ResponseBody@Scheduled(cron="0 0 12 * * ?") //每天12點執行一次public String selectcsdn() throws IOException {csdn csdn=new csdn();int fwl=csdn.selectcsdn();fwl wl=new fwl();wl.setFwl(fwl);wl.setTime(getTime());System.out.println("fwl:"+fwl);System.out.println("顯示時間是:"+getTime());int a=fwlmapper.insertfwl(wl);System.out.println("a:"+a);return "插入成功";}@RequestMapping("/selecttime")@ResponseBodypublic List<fwl> selecttime(){System.out.println("進入了查詢csdn訪問量");System.out.println("查詢到的數據是:"+fwlmapper.selectall().get(2).getTime());return fwlmapper.selectall();}public static String getTime() {Date date=new Date();SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");String updateTime=sdf.format(date);return updateTime;}}抓取CSDN博客評論代碼:
import java.io.IOException; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.select.Elements;public class csdn {public int selectcsdn() throws IOException {Document doc = Jsoup.connect("https://blog.csdn.net/weixin_38959210").header("Accept-Encoding", "gzip, deflate").userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0").maxBodySize(0).timeout(600000).get();Elements csdndoc=doc.select(".grade-box");Elements csdn_dd=csdndoc.select("dl dd");String fangwenliang=csdn_dd.get(1).attr("title");int fwliang = Integer.parseInt(fangwenliang);System.out.println("csdn訪問量:"+fangwenliang);return fwliang;} }?
dao層代碼 @Mapper:
@Mapper public interface FwlMapper {public int insertfwl(fwl wl);public List<fwl> selectall();}mapper.xml:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.dao.FwlMapper"><resultMap type="fwl" id="fwlMap"><id column="id" property="id" jdbcType="INTEGER" /><result column="fwl" property="fwl" jdbcType="VARCHAR" /><result column="time" property="time" jdbcType="VARCHAR" /></resultMap><insert id="insertfwl" parameterType="fwl" >insert into csdnfwl(fwl, time) values(#{fwl}, #{time})</insert><select id="selectall" resultType="com.example.demo.entity.fwl">select id,fwl,LEFT(time,10) time from (select * from csdnfwl order by time desc limit 8) u order by time asc</select></mapper>?前端代碼:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <style> body {padding:0; /*去除內邊距*/border:0; /*去除邊框*/margin:0; /*去除外邊距*/ } </style><script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> <script src="https://cdn.bootcss.com/echarts/4.2.1-rc1/echarts-en.common.js"></script> <body><div id="main" style="width: 1920px;height:800px;" ></div></body><script>$(function () {// 基于準備好的dom,初始化echarts實例var myChart = echarts.init(document.getElementById('main'));// 指定圖表的配置項和數據var option = {title: {text: 'csdn博客訪問量',subtext: '單位:人次'},tooltip:{show:true },toolbox: {show : true,feature : {mark : {show: false},dataView : {show: true, readOnly: false},magicType : {show: true, type: ['line', 'bar']},restore : {show: true},saveAsImage : {show: true}}},legend: {data:[]},xAxis: {data: []},yAxis: {scale:true // 最小值},series: [{type: 'line',data:[]}]};// 使用剛指定的配置項和數據顯示圖表。myChart.setOption(option);var taking=[]; //收入數組(實際用來盛放X軸坐標值)var yearmonth=[]; //年月數組(實際用來盛放Y坐標值)$.ajax({async : true, //異步請求(同步請求將會鎖住瀏覽器,用戶其他操作必須等待請求完成才可以執行)url : "/selecttime", //請求發送到TestServlet處dataType : "json", //返回數據形式為jsonsuccess : function(result) {//請求成功時執行該函數內容,result即為服務器返回的json對象if (result != null && result.length > 0) {for(var i=0;i<result.length;i++){ taking.push(result[i].fwl); //挨個取出收入并填入類別數組}for(var i=0;i<result.length;i++){ yearmonth.push(result[i].time); //挨個取出年月并填入銷量數組}myChart.hideLoading(); //隱藏加載動畫myChart.setOption({ //加載數據圖表xAxis: {data: yearmonth,},series: [{// 根據名字對應到相應的系列data: taking}]});}},error : function(errorMsg) {//請求失敗時執行該函數alert("圖表請求數據失敗!");myChart.hideLoading();}})});</script> </html>?實體類命名:
private int id;private int fwl;private String time;數據庫命名:
總結
以上是生活随笔為你收集整理的每天定时查询CSDN博客访问量,并通过echarts进行展示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring boot实现异步
- 下一篇: springboot使用PageHelp