注:实践内容参考人民邮电出版社的教程《 Spring Boot企业级开发教程》作者:黑马程序员,上传本文仅以实践过程以供大家共同学习解决问题,如有侵权不当行为,请告知后,我会更正或删除。
10.1 系统概述
本章涉及的引入资源请通过百度云盘下载:https://pan.baidu.com/s/1QCxpOfyjfKLsGcCDPfO1jQ
因内容较多,不少文字内容也以截图显示,敬请谅解。
10.1.1 系统功能介绍
10.1.2 项目效果预览
5.后台文件编辑页面预览
10.2 项目设计
10.2.1 系统开发及运行环境
10.2.2 文件组织结构
10.2.3 数据库设计
“ROLE_”开头。
10.3 系统环境搭建
10.3.1 准备数据库资源
-
1.运行MYSQL数据库,这里我们使用之前的phpstudy.
-
2.运行SQLyog,创建数据库blog_system
-
3.导入SQL文件完成数据库内容的创建。
-
4.执行SQL文件blog_system.sql
-
5.执行成功后,6张表成功创建及导入了数据,至此,准备数据库资源完成。如图:
10.3.2 准备项目环境
- 1.创建项目,引入依赖文件
<dependencies>
<!-- 阿里巴巴的Druid数据源依赖启动器 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<!-- MyBatis依赖启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!-- MySQL数据库连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Redis服务启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- mail邮件服务启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!-- thymeleaf模板整合security控制页面安全访问依赖 -->
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
<!-- Spring Security依赖启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- Thymeleaf模板引擎启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- Web服务启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- MyBatis分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.8</version>
</dependency>
<!-- String工具类包-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<!-- Markdown处理html -->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark</artifactId>
<version>0.11.0</version>
</dependency>
<!-- Markdown处理表格 -->
<dependency>
<groupId>com.atlassian.commonmark</groupId>
<artifactId>commonmark-ext-gfm-tables</artifactId>
<version>0.11.0</version>
</dependency>
<!-- 过滤emoji表情字符 -->
<dependency>
<groupId>com.vdurmont</groupId>
<artifactId>emoji-java</artifactId>
<version>4.0.0</version>
</dependency>
<!-- devtools热部署工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring Boot测试服务启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
-
2.编写配置文件
server: port: 80 spring: profiles: # 外置jdbc、redis和mail配置文件 active: jdbc,redis,mail # 关闭thymeleaf页面缓存 thymeleaf: cache: false # 配置国际化资源文件 messages: basename: i18n.logo # MyBatis配置 mybatis: configuration: #开启驼峰命名匹配映射 map-underscore-to-camel-case: true #配置MyBatis的xml映射文件路径 mapper-locations: classpath:mapper/*.xml #配置XML映射文件中指定的实体类别名路径 type-aliases-package: com.itheima.model.domain #pagehelper分页设置 pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true params: count=countSql #浏览器cookie相关设置 COOKIE: # 设置cookie默认时长为30分钟 VALIDITY: 1800
application-jdbc.properties内容如下:
#添加并配置第三方数据库连接池druid
spring.datasource.type = com.alibaba.druid.pool.DruidDataSource
spring.datasource.initialSize=20
spring.datasource.minIdle=10
spring.datasource.maxActive=100
#数据源连接配置
spring.datasource.url = jdbc:mysql://localhost:3306/blog_system?serverTimezone=UTC&useSSL=false
spring.datasource.username = root
spring.datasource.password = root
#driver-class-name可以省略
#spring.datasource.driver-class-name = com.mysql.jdbc.Driver
application-mail.properties内容如下:
#QQ邮箱邮件发送服务配置
spring.mail.host=smtp.qq.com
spring.mail.port=587
# 请根据自己的QQ和授权码,配置个人QQ账户和密码(密码是加密后的授权码)
spring.mail.username=24774331564@qq.com
spring.mail.password=zfulnobbgagjbgjf
application-redis.properties内容如下:
# Redis服务器地址,另外注意要开启Redis服务
spring.redis.host=127.0.0.1
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=8
-
3.前端资源引入,
-
4.后端基础代码引入
10.4 前台管理模块
10.4.1 文章分页展示
-
1.数据访问层实现
实现文章分类展示效果需要同时实现文章查询以及文章统计数据查询,这里先编 写文章类Article和统计类Statistic对应的数据访问方法。
-
(1)创建 Dao层接口文件
在com.itheima下创建dao包,并在该包下使用MyBatis框架分别创建文章类Article和统计类Statistic对应的Mapper接口文件,ArticleMapper.java内容如下:
package com.itheima.dao; import com.itheima.model.domain.Article; import org.apache.ibatis.annotations.*; import java.util.List; @Mapper public interface ArticleMapper { // 根据id查询文章信息 @Select("SELECT * FROM t_article WHERE id=#{id}") public Article selectArticleWithId(Integer id); // 发表文章,同时使用@Options注解获取自动生成的主键id @Insert("INSERT INTO t_article (title,created,modified,tags,categories," + " allow_comment, thumbnail, content)" + " VALUES (#{title},#{created}, #{modified}, #{tags}, #{categories}," + " #{allowComment}, #{thumbnail}, #{content})") @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id") public Integer publishArticle(Article article); // 文章发分页查询 @Select("SELECT * FROM t_article ORDER BY id DESC") public List<Article> selectArticleWithPage(); // 通过id删除文章 @Delete("DELETE FROM t_article WHERE id=#{id}") public void deleteArticleWithId(int id); // 站点服务统计,统计文章数量 @Select("SELECT COUNT(1) FROM t_article") public Integer countArticle(); // 通过id更新文章 public Integer updateArticleWithId(Article article); }
-
StatisticMapper.java内容如下:
```
package com.itheima.dao;
import com.itheima.model.domain.Article;
import com.itheima.model.domain.Statistic;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface Statisti