本文最后更新于 278 天前,如有错误请邮件至 zhiligyi222na@gmail.com
两个普通业务模块之间
新建文章分类表
CREATE TABLE `category` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '分类标题',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='文章分类表';
把文章模块和文章分类模块关联上
1.在文章信息表里面创建一个关联字段(不是创建一个外键)
2.在实体类和sql里面也加上这个字段
3.初始化字段数据,再页面新增或编辑的时候,把数据传递到后端,操作到数据库里
<el-form-item prop="title" label="文章分类">
<el-select
v-model="data.form.categoryId"
placeholder="请选择文章分类"
size="large"
style="width: 100%"
>
<el-option
v-for="item in data.categoryDate"
:key="item.id"
:label="item.title"
:value="item.id"
/>
</el-select>
</el-form-item>
const loadCategory = () => {
request.get('/category/selectAll').then(res => {
if (res.code === '200') {
data.categoryDate = res.data
} else {
ElMessage.error(res.msg)
}
})
}
loadCategory()
4.将分类Id展示为分类标题,java代码里写相关逻辑
// 这个list是数据库查询出来的数据
for (Article dbArticle: list){
// 获取分类ID
Integer categoryId = dbArticle.getCategoryId();
// 根据分类ID查询分类
Category category = categoryMapper.selectById(categoryId);
if (ObjectUtil.isNotEmpty(category)) {
// 把分类的title赋值给categoryTitle
dbArticle.setCategoryTitle(category.getTitle());
}
}
业务模块与角色模块之间
把业务模块和角色模块关联上
1.在文章信息表里面创建一个关联字段(不是创建一个外键)
2.在实体类和sql里面也加上这个字段
3.初始化字段数据,再页面新增或编辑的时候,把数据传递到后端,操作到数据库里
Account currentUser = TokenUtils.getCurrentUser();
article.setUserId(currentUser.getId() );
4.将分类Id展示为分类标题,java代码里写相关逻辑
汇总代码
ArticleService
public PageInfo<Article> selectPage(Integer pageNum, Integer pageSize, Article article) {
//开启分页查询
PageHelper.startPage(pageNum, pageSize);
List<Article> list = articleMapper.selectAll(article);
// 这个list是数据库查询出来的数据
for (Article dbArticle: list){
// 获取分类ID
Integer categoryId = dbArticle.getCategoryId();
Integer userId = dbArticle.getUserId();
// 根据分类ID查询分类
Category category = categoryMapper.selectById(categoryId);
User user = userMapper.selectById(userId.toString());
if (ObjectUtil.isNotEmpty(category)) {
// 把分类的title赋值给categoryTitle
dbArticle.setCategoryTitle(category.getTitle());
}
if (ObjectUtil.isNotEmpty(user)){
dbArticle.setUserName(user.getName());
}
}
return PageInfo.of(list);
}
权限补充
//判断是普通用户还是管理员
Account currentUser = TokenUtils.getCurrentUser();
if ("USER".equals(currentUser.getRole())){
article.setUserId(currentUser.getId());
}









