一、概述
项目的后端技术栈为Java、SpringBoot、MybatisPlus、爬虫Jsoup、HttpClient、Maven项目构建。
各软件版本分别如下:
软件及环境 | 版本号 |
操作系统 | Windows10&macOS |
开发工具 | 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
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.anymk</groupId>
<artifactId>parent-boot-vue</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>csdn-reader</artifactId>
<properties>
<java.version>1.8</java.version>
<spring.boot.version>2.1.8.RELEASE</spring.boot.version>
<maven.resource.version>3.1.0</maven.resource.version>
<maven.clean.version>3.1.0</maven.clean.version>
<maven.compiler.version>3.8.1</maven.compiler.version>
<java.source.version>1.8</java.source.version>
<java.target.version>1.8</java.target.version>
<file.encoding>UTF-8</file.encoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.12.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.48</version>
</dependency>
<!-- database -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
</dependencies>
<build>
<finalName>csdn</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring.boot.version}</version>
<!--解决打包后,执行java -jar 命令,找不到主清单属性-->
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
<!--clean插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>${maven.clean.version}</version>
<configuration>
<filesets>
<fileset>
<directory>src/main/resources/static</directory>
</fileset>
<fileset>
<directory>src/main/resources/templates</directory>
</fileset>
</filesets>
</configuration>
</plugin>
<!--资源插件,主要为了从前端项目里复制打包好的文件到springboot项目-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>${maven.resource.version}</version>
<executions>
<execution>
<id>copy static</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/resources/static</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<!--因为vue-cli打包的目录在项目的根目录,所以从这里复制-->
<directory>${project.parent.basedir}/construction-data/dist</directory>
<includes>
<include>css/</include>
<include>img/</include>
<include>js/</include>
<include>favicon.ico</include>
<include>index.html</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
<execution>
<id>copy template</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/resources/templates</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<!--因为vue-cli打包的目录在项目的根目录,所以从这里复制-->
<directory>${project.parent.basedir}/construction-data/dist</directory>
<includes>
<include>index.html</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
注意,这里我们给这个工程提供了一个父工程,父工程主要的目的是为了让这个前后端分离的项目最终能打包成一个Jar包去部署,提高运维效率,具体操作见:如何将SpringBoot+Vue前后端分离项目一次打包为一个Jar包运行?
application.yml
该文件位于resource目录下,需要大家修改的地方有:项目运行的端口号port,数据库的连接信息,其他都不用改
server:
#端口号
port: 8888
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/csdnreader?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
username: root
password: root
druid:
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: true
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
stat-view-servlet:
allow: 127.0.0.1
aop:
proxy-target-class: true
messages:
encoding: utf-8
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
#mybatis plus 设置
mybatis:
type-aliases-package: com.csdn.reader.entity
mapper-locations: classpath*:mapper/*/*.xml
configuration:
jdbc-type-for-null: null
global-config:
# 关闭 mybatis-plus的 banner
banner: false
三、数据库脚本
201个博主的投票信息会每五分钟去投票网站爬取一次,爬取后写到数据库以便于后续的数据分析,特提供数据库脚本如下:
/*
Navicat Premium Data Transfer
Source Server : localhost
Source Server Type : MySQL
Source Server Version : 50553
Source Host : localhost:3306
Source Schema : csdnreader
Target Server Type : MySQL
Target Server Version : 50553
File Encoding : 65001
Date: 21/01/2020 15:53:35
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for t_csdn_topn
-- ----------------------------
DROP TABLE IF EXISTS `t_csdn_topn`;
CREATE TABLE `t_csdn_topn` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`ranking` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '排名',
`name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '名字',
`nowVotes` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '票数',
`createDate` datetime NULL DEFAULT NULL COMMENT '采集时间',
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 315973 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Compact;
SET FOREIGN_KEY_CHECKS = 1;
四、目录结构及启动类
采用后端标准的三层模型进行开发
目录结构如下
ReaderApplication启动类
package com.csdn.reader;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@SpringBootApplication
@EnableScheduling//开启定时任务
@MapperScan("com.csdn.reader.dao")
public class ReaderApplication {
public static void main(String[] args) {
SpringApplication.run(ReaderApplication.class, args);
}
}
注解详解:
@EnableScheduling//开启定时任务,去定时爬取投票网站数据 @MapperScan("com.csdn.reader.dao")//设置扫描的数据访问层包路径,将数据insert到MySQL数据库中
五、项目启动
在开发环境中运行启动类的main函数即可启动。启动成功后截图:
over!