小言_互联网的博客

大屏监控系统实战(2)-后台工程搭建

188人阅读  评论(0)

一、概述

项目的后端技术栈为Java、SpringBoot、MybatisPlus、爬虫Jsoup、HttpClient、Maven项目构建。

各软件版本分别如下:

软件及环境 版本号
操作系统 Windows10&MacBook Pro
开发工具 IDEA2019.3
数据库工具 Navicat Premium12.0
MySQL 5.7
JDK 1.8
SpringBoot 2.1.8.RELEASE
Maven 3.6.0
部署服务器 CentOS7.2
httpclient 4.5.8
Jsoup 1.12.1
fastjson 1.2.54
lombok 1.18.10
mybatis-plus 3.2.0
druid 1.1.12
mysql-connector-java 5.1.48
maven插件 3.1.0&3.8.1

二、SpringBoot项目的配置文件

pom.xml


  
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation= "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0 </modelVersion>
  5. <parent>
  6. <groupId>com.anymk </groupId>
  7. <artifactId>parent-boot-vue </artifactId>
  8. <version>1.0-SNAPSHOT </version>
  9. </parent>
  10. <artifactId>csdn-reader </artifactId>
  11. <properties>
  12. <java.version>1.8 </java.version>
  13. <spring.boot.version>2.1.8.RELEASE </spring.boot.version>
  14. <maven.resource.version>3.1.0 </maven.resource.version>
  15. <maven.clean.version>3.1.0 </maven.clean.version>
  16. <maven.compiler.version>3.8.1 </maven.compiler.version>
  17. <java.source.version>1.8 </java.source.version>
  18. <java.target.version>1.8 </java.target.version>
  19. <file.encoding>UTF-8 </file.encoding>
  20. </properties>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot </groupId>
  24. <artifactId>spring-boot-starter-web </artifactId>
  25. </dependency>
  26. <dependency>
  27. <groupId>org.springframework.boot </groupId>
  28. <artifactId>spring-boot-starter-test </artifactId>
  29. <scope>test </scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>org.apache.httpcomponents </groupId>
  33. <artifactId>httpclient </artifactId>
  34. <version>4.5.8 </version>
  35. </dependency>
  36. <dependency>
  37. <groupId>com.alibaba </groupId>
  38. <artifactId>fastjson </artifactId>
  39. <version>1.2.54 </version>
  40. </dependency>
  41. <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
  42. <dependency>
  43. <groupId>org.jsoup </groupId>
  44. <artifactId>jsoup </artifactId>
  45. <version>1.12.1 </version>
  46. </dependency>
  47. <dependency>
  48. <groupId>org.projectlombok </groupId>
  49. <artifactId>lombok </artifactId>
  50. <version>1.18.10 </version>
  51. </dependency>
  52. <dependency>
  53. <groupId>mysql </groupId>
  54. <artifactId>mysql-connector-java </artifactId>
  55. <version>5.1.48 </version>
  56. </dependency>
  57. <!-- database -->
  58. <dependency>
  59. <groupId>com.baomidou </groupId>
  60. <artifactId>mybatis-plus-boot-starter </artifactId>
  61. <version>3.2.0 </version>
  62. </dependency>
  63. <dependency>
  64. <groupId>org.springframework.boot </groupId>
  65. <artifactId>spring-boot-starter-thymeleaf </artifactId>
  66. </dependency>
  67. <dependency>
  68. <groupId>com.alibaba </groupId>
  69. <artifactId>druid </artifactId>
  70. <version>1.1.12 </version>
  71. </dependency>
  72. </dependencies>
  73. <build>
  74. <finalName>csdn </finalName>
  75. <plugins>
  76. <plugin>
  77. <groupId>org.springframework.boot </groupId>
  78. <artifactId>spring-boot-maven-plugin </artifactId>
  79. <version>${spring.boot.version} </version>
  80. <!--解决打包后,执行java -jar 命令,找不到主清单属性-->
  81. <executions>
  82. <execution>
  83. <phase>package </phase>
  84. <goals>
  85. <goal>repackage </goal>
  86. </goals>
  87. </execution>
  88. </executions>
  89. </plugin>
  90. <!--clean插件-->
  91. <plugin>
  92. <groupId>org.apache.maven.plugins </groupId>
  93. <artifactId>maven-clean-plugin </artifactId>
  94. <version>${maven.clean.version} </version>
  95. <configuration>
  96. <filesets>
  97. <fileset>
  98. <directory>src/main/resources/static </directory>
  99. </fileset>
  100. <fileset>
  101. <directory>src/main/resources/templates </directory>
  102. </fileset>
  103. </filesets>
  104. </configuration>
  105. </plugin>
  106. <!--资源插件,主要为了从前端项目里复制打包好的文件到springboot项目-->
  107. <plugin>
  108. <groupId>org.apache.maven.plugins </groupId>
  109. <artifactId>maven-resources-plugin </artifactId>
  110. <version>${maven.resource.version} </version>
  111. <executions>
  112. <execution>
  113. <id>copy static </id>
  114. <phase>generate-resources </phase>
  115. <goals>
  116. <goal>copy-resources </goal>
  117. </goals>
  118. <configuration>
  119. <outputDirectory>src/main/resources/static </outputDirectory>
  120. <overwrite>true </overwrite>
  121. <resources>
  122. <resource>
  123. <!--因为vue-cli打包的目录在项目的根目录,所以从这里复制-->
  124. <directory>${project.parent.basedir}/construction-data/dist </directory>
  125. <includes>
  126. <include>css/ </include>
  127. <include>img/ </include>
  128. <include>js/ </include>
  129. <include>favicon.ico </include>
  130. <include>index.html </include>
  131. </includes>
  132. </resource>
  133. </resources>
  134. </configuration>
  135. </execution>
  136. <execution>
  137. <id>copy template </id>
  138. <phase>generate-resources </phase>
  139. <goals>
  140. <goal>copy-resources </goal>
  141. </goals>
  142. <configuration>
  143. <outputDirectory>src/main/resources/templates </outputDirectory>
  144. <overwrite>true </overwrite>
  145. <resources>
  146. <resource>
  147. <!--因为vue-cli打包的目录在项目的根目录,所以从这里复制-->
  148. <directory>${project.parent.basedir}/construction-data/dist </directory>
  149. <includes>
  150. <include>index.html </include>
  151. </includes>
  152. </resource>
  153. </resources>
  154. </configuration>
  155. </execution>
  156. </executions>
  157. </plugin>
  158. </plugins>
  159. </build>
  160. </project>

注意,这里我们给这个工程提供了一个父工程,父工程主要的目的是为了让这个前后端分离的项目最终能打包成一个Jar包去部署,提高运维效率,具体操作见:如何将SpringBoot+Vue前后端分离项目一次打包为一个Jar包运行?

application.yml

该文件位于resource目录下,需要大家修改的地方有:项目运行的端口号port,数据库的连接信息,其他都不用改


  
  1. server:
  2. #端口号
  3. port: 8888
  4. spring:
  5. datasource:
  6. driver- class-name: com.mysql.jdbc.Driver
  7. url: jdbc:mysql: //localhost:3306/csdnreader?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
  8. username: root
  9. password: root
  10. druid:
  11. initialSize: 5
  12. minIdle: 5
  13. maxActive: 20
  14. maxWait: 60000
  15. timeBetweenEvictionRunsMillis: 60000
  16. minEvictableIdleTimeMillis: 300000
  17. validationQuery: SELECT 1
  18. testWhileIdle: true
  19. testOnBorrow: true
  20. testOnReturn: false
  21. poolPreparedStatements: true
  22. maxPoolPreparedStatementPerConnectionSize: 20
  23. filters: stat,wall
  24. connectionProperties: druid.stat.mergeSql= true;druid.stat.slowSqlMillis= 5000
  25. stat-view-servlet:
  26. allow: 127.0 .0 .1
  27. aop:
  28. proxy-target-class: true
  29. messages:
  30. encoding: utf -8
  31. jackson:
  32. date-format: yyyy-MM-dd HH:mm:ss
  33. time-zone: GMT+ 8
  34. #mybatis plus 设置
  35. mybatis:
  36. type-aliases-package: com.csdn.reader.entity
  37. mapper-locations: classpath*:mapper /*/*.xml
  38. configuration:
  39. jdbc-type-for-null: null
  40. global-config:
  41. # 关闭 mybatis-plus的 banner
  42. banner: false

三、数据库脚本

201个博主的投票信息会每五分钟去投票网站爬取一次,爬取后写到数据库以便于后续的数据分析,特提供数据库脚本如下:


  
  1. /*
  2. Navicat Premium Data Transfer
  3. Source Server : localhost
  4. Source Server Type : MySQL
  5. Source Server Version : 50553
  6. Source Host : localhost:3306
  7. Source Schema : csdnreader
  8. Target Server Type : MySQL
  9. Target Server Version : 50553
  10. File Encoding : 65001
  11. Date: 21/01/2020 15:53:35
  12. */
  13. SET NAMES utf8mb4;
  14. SET FOREIGN_KEY_CHECKS = 0;
  15. -- ----------------------------
  16. -- Table structure for t_csdn_topn
  17. -- ----------------------------
  18. DROP TABLE IF EXISTS `t_csdn_topn`;
  19. CREATE TABLE `t_csdn_topn` (
  20. `id` int( 11) NOT NULL AUTO_INCREMENT,
  21. `ranking` varchar( 20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '排名',
  22. `name` varchar( 50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名字',
  23. `nowVotes` varchar( 20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '票数',
  24. `createDate` datetime NULL DEFAULT NULL COMMENT '采集时间',
  25. PRIMARY KEY ( `id`) USING BTREE
  26. ) ENGINE = InnoDB AUTO_INCREMENT = 315973 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;
  27. SET FOREIGN_KEY_CHECKS = 1;

四、目录结构及启动类

采用后端标准的三层模型进行开发

目录结构如下

ReaderApplication启动类


  
  1. package com.csdn.reader;
  2. import org.mybatis.spring.annotation.MapperScan;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.scheduling.annotation.EnableScheduling;
  7. import org.springframework.web.servlet.config.annotation.CorsRegistry;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
  9. @SpringBootApplication
  10. @EnableScheduling //开启定时任务
  11. @MapperScan( "com.csdn.reader.dao")
  12. public class ReaderApplication {
  13. public static void main(String[] args) {
  14. SpringApplication.run(ReaderApplication.class, args);
  15. }
  16. }

注解详解:


   
  1. @EnableScheduling//开启定时任务,去定时爬取投票网站数据
  2. @MapperScan("com.csdn.reader.dao")//设置扫描的数据访问层包路径,将数据insert到MySQL数据库中

五、项目启动

 在开发环境中运行启动类的main函数即可启动。启动成功后截图:

over!


转载:https://blog.csdn.net/m0_37609579/article/details/104062646
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场