项目的后端技术栈为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 |
4.0.0
com.anymk
parent-boot-vue
1.0-SNAPSHOT
csdn-reader
1.8
2.1.8.RELEASE
3.1.0
3.1.0
3.8.1
1.8
1.8
UTF-8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.apache.httpcomponents
httpclient
4.5.8
com.alibaba
fastjson
1.2.54
org.jsoup
jsoup
1.12.1
org.projectlombok
lombok
1.18.10
mysql
mysql-connector-java
5.1.48
com.baomidou
mybatis-plus-boot-starter
3.2.0
org.springframework.boot
spring-boot-starter-thymeleaf
com.alibaba
druid
1.1.12
csdn
org.springframework.boot
spring-boot-maven-plugin
${spring.boot.version}
package
repackage
org.apache.maven.plugins
maven-clean-plugin
${maven.clean.version}
src/main/resources/static
src/main/resources/templates
org.apache.maven.plugins
maven-resources-plugin
${maven.resource.version}
copy static
generate-resources
copy-resources
src/main/resources/static
true
${project.parent.basedir}/construction-data/dist
css/
img/
js/
favicon.ico
index.html
copy template
generate-resources
copy-resources
src/main/resources/templates
true
${project.parent.basedir}/construction-data/dist
index.html
注意,这里我们给这个工程提供了一个父工程,父工程主要的目的是为了让这个前后端分离的项目最终能打包成一个Jar包去部署,提高运维效率,具体操作见:如何将SpringBoot+Vue前后端分离项目一次打包为一个Jar包运行?
该文件位于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;
采用后端标准的三层模型进行开发
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!