05. 开发系统公告管理功能
在线的代码生成器
本地复制代码的工具类
https://gitee.com/xqnode/CopyUtils/blob/master/CopyUtils.java
SQL
CREATE TABLE `notice` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '标题',
`content` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '内容',
`time` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '发布时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='系统公告';
开发后端接口
Notice.java
package com.example.entity;
public class Notice {
/**主键ID */
private Integer id;
/**标题 */
private String title;
/**内容 */
private String content;
/**发布时间 */
private String time;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
NoticeMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.NoticeMapper">
<select id="selectAll" resultType="com.example.entity.Notice">
select * from `notice`
<where>
<if test="title != null"> and title like concat('%', #{title}, '%')</if>
</where>
order by id desc
</select>
<insert id="insert" parameterType="com.example.entity.Notice" useGeneratedKeys="true">
insert into `notice`
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">id,</if>
<if test="title != null">title,</if>
<if test="content != null">content,</if>
<if test="time != null">time,</if>
</trim>
values
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">#{id},</if>
<if test="title != null">#{title},</if>
<if test="content != null">#{content},</if>
<if test="time != null">#{time},</if>
</trim>
</insert>
<update id="updateById" parameterType="com.example.entity.Notice">
update `notice`
<set>
<if test="id != null">
id = #{id},
</if>
<if test="title != null">
title = #{title},
</if>
<if test="content != null">
content = #{content},
</if>
<if test="time != null">
time = #{time},
</if>
</set>
where id = #{id}
</update>
</mapper>
开发前端页面
Notice.vue
<template>
<div>
<div class="card" style="margin-bottom: 5px;">
<el-input v-model="data.title" style="width: 300px; margin-right: 10px" placeholder="请输入标题查询"></el-input>
<el-button type="primary" @click="load">查询</el-button>
<el-button type="info" style="margin: 0 10px" @click="reset">重置</el-button>
</div>
<div class="card" style="margin-bottom: 5px">
<div style="margin-bottom: 10px">
<el-button type="primary" @click="handleAdd">新增</el-button>
</div>
<el-table :data="data.tableData" stripe>
<el-table-column prop="title" label="标题"></el-table-column>
<el-table-column prop="content" label="内容" show-overflow-tooltip></el-table-column>
<el-table-column prop="time" label="发布时间"></el-table-column>
<el-table-column label="操作" align="center" width="160">
<template #default="scope">
<el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
<el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
<div class="card">
<el-pagination @current-change="load" background layout="total, prev, pager, next" v-model:page-size="data.pageSize" v-model:current-page="data.pageNum" :total="data.total"/>
</div>
<el-dialog title="公告信息" width="40%" v-model="data.formVisible" :close-on-click-modal="false" destroy-on-close>
<el-form ref="formRef" :model="data.form" :rules="data.rules" label-width="100px" style="padding-right: 50px">
<el-form-item label="标题" prop="title">
<el-input placeholder="请输入标题" v-model="data.form.title" autocomplete="off" />
</el-form-item>
<el-form-item label="内容" prop="content">
<el-input type="textarea" :rows="3" maxlength="200" placeholder="请输入内容" v-model="data.form.content" autocomplete="off" />
</el-form-item>
</el-form>
<template #footer>
<span class="dialog-footer">
<el-button @click="data.formVisible = false">取 消</el-button>
<el-button type="primary" @click="save">保 存</el-button>
</span>
</template>
</el-dialog>
</div>
</template>
<script setup>
import request from "@/utils/request";
import {reactive, ref} from "vue";
import {ElMessageBox, ElMessage} from "element-plus";
const formRef = ref()
const data = reactive({
user: JSON.parse(localStorage.getItem('system-user') || '{}'),
pageNum: 1,
pageSize: 10,
total: 0,
formVisible: false,
form: {},
tableData: [],
title: null,
rules: {
title: [
{ required: true, message: '请输入标题', trigger: 'blur' }
],
content: [
{ required: true, message: '请输入内容', trigger: 'blur' }
],
}
})
// 分页查询
const load = () => {
request.get('/notice/selectPage', {
params: {
pageNum: data.pageNum,
pageSize: data.pageSize,
title: data.title
}
}).then(res => {
data.tableData = res.data?.list
data.total = res.data?.total
})
}
// 新增
const handleAdd = () => {
data.form = {}
data.formVisible = true
}
// 编辑
const handleEdit = (row) => {
data.form = JSON.parse(JSON.stringify(row))
data.formVisible = true
}
// 新增保存
const add = () => {
request.post('/notice/add', data.form).then(res => {
if (res.code === '200') {
load()
ElMessage.success('操作成功')
data.formVisible = false
} else {
ElMessage.error(res.msg)
}
})
}
// 编辑保存
const update = () => {
request.put('/notice/update', data.form).then(res => {
if (res.code === '200') {
load()
ElMessage.success('操作成功')
data.formVisible = false
} else {
ElMessage.error(res.msg)
}
})
}
// 弹窗保存
const save = () => {
formRef.value.validate(valid => {
if (valid) {
// data.form有id就是更新,没有就是新增
data.form.id ? update() : add()
}
})
}
// 删除
const handleDelete = (id) => {
ElMessageBox.confirm('删除后数据无法恢复,您确定删除吗?', '删除确认', { type: 'warning' }).then(res => {
request.delete('/notice/delete/' + id).then(res => {
if (res.code === '200') {
load()
ElMessage.success('操作成功')
} else {
ElMessage.error(res.msg)
}
})
}).catch(err => {})
}
// 重置
const reset = () => {
data.title = null
load()
}
load()
</script>
Home.vue
<template>
<div>
<div class="card" style="margin-bottom: 10px">
<div>欢迎您,<b>{{ data.user.name }}</b> 祝您今天过得开心!</div>
</div>
<div class="card" style="line-height:30px; margin-bottom: 10px">
<div>B站UP:<a style="color: #1890ff" href="https://space.bilibili.com/402779077">程序员青戈</a> 出品,感谢大家的支持~</div>
<div>从0开始带你做一套完整的前后端分离项目,<b style="color: red">完全免费</b>,大家多多三连支持一波噢~</div>
<div>获取项目资料请访问:<a style="color: #1890ff; font-weight: bold" href="https://javaxm.cn">https://javaxm.cn</a></div>
<a style="color: #1890ff; font-weight: bold" href="https://codenice.cn">https://codenice.cn</a></div>
</div>
<div class="card" style="padding: 20px">
<div style="font-size: 20px; font-weight: 400; margin-bottom: 20px">系统公告</div>
<el-timeline style="max-width: 600px">
<el-timeline-item
placement="top"
v-for="(notice, index) in data.noticeList"
:key="index"
color="#0bbd87"
:timestamp="notice.time"
>
<div style="margin-bottom: 10px; font-size: 18px">{{ notice.title }}</div>
<div style="color: #666">{{ notice.content }}</div>
</el-timeline-item>
</el-timeline>
</div>
</div>
</template>
<script setup>
import { reactive } from "vue";
import request from "@/utils/request";
const data = reactive({
user: JSON.parse(localStorage.getItem('system-user') || '{}'),
noticeList: []
})
request.get('/notice/selectAll').then(res => {
data.noticeList = res.data
})
</script>