目录
第八章 文章管理模块
创建新的Spring Boot项目,综合运用视频中的知识点,做一个文章管理的后台应用。 新的Spring Boot项目Lession20-BlogAdmin。Maven构建工具,包名称com.bjpowernode.blog JDK19,依赖:
- Spring Web
- Lombok
- Thymeleaf
- MyBatis Framework
- MySQL Driver
依赖还需要Bean Validation 需求:文章管理工作,发布新文章,编辑文章,查看文章内容等
8.1 配置文件
组织配置文件
app-base.yml
article:
#最低文章阅读数量
low-read: 10
#首页显示最多的文章数量
top-read: 20
db.yml
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?serverTimezone=Asia/Shanghai
username: root
password: 123456
hikari:
auto-commit: true
maximum-pool-size: 10
minimum-idle: 10
#获取连接时,检测语句
connection-test-query: select 1
connection-timeout: 20000
#其他属性
data-source-properties:
cachePrepStmts: true
dataSource.cachePrepStmtst: true
dataSource.prepStmtCacheSize: 250
dataSource.prepStmtCacheSqlLimit: 2048
dataSource.useServerPrepStmts: true
8.2 视图文件
logo文件
favicon.ico放在static/ 根目录下
创建模板页面
articleList.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>文章列表</title>
<link rel="icon" href="../favicon.ico" type="image/x-icon"/>
<script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
</head>
<body>
<div style="margin-left: 200px">
<h3>阅读最多的前10篇文章</h3>
<table border="1px" cellspacing="0px" cellpadding="2px">
<thead>
<th>选择</th>
<th>序号</th>
<th>标题</th>
<th>副标题</th>
<th>已读数量</th>
<th>发布时间</th>
<th>最后修改时间</th>
<th>编辑</th>
</thead>
<tbody>
<tr th:each="article,loopStats : ${articleList}">
<td><input type="checkbox" th:value="${article.id}"></td>
<td th:text="${loopStats.index+1}"></td>
<td th:text="${article.title}"></td>
<td th:text="${article.summary}"></td>
<td th:text="${article.readCount}"></td>
<td th:text="${article.createTime}"></td>
<td th:text="${article.updateTime}"></td>
<td>
<a th:href="@{/article/get(id=${article.id})}">编辑</a>
</td>
</tr>
<tr>
<td colspan="8">
<table width="100%">
<tr>
<td><button id="add" onclick="addArticle()">发布新文章</button></td>
<td><button id="delete" onclick="deleteArticle()">删除文章</button></td>
<td><button id="read" onclick="readOverview()">文章概览</button></td>
</tr>
</table>
</td>
</tr>
</tbody>
</table>
<form id="frm" th:action="@{/view/addArticle}" method="get">
</form>
<form id="delfrm" th:action="@{/article/removeArticle}" method="post">
<input type="hidden" id="idsDom" name="ids" value="" >
</form>
</div>
<script type="