SpringBoot+Vue3实现模块之间的关联

1.什么叫模块之间的关联?它有那些形式?

1。两个普通业务模块之间

比如说:图书分类 (小说,爱情,教材),图书信息 (高等数学)

商品分类: (男装,女装,零食…) ,商品信息(瓜子,男士西服,女裙…)

2.普通业务模块和角色模块之间

旅游攻略 (是谁发布的) 管理员可以看到所有人发布的,用户只能看到自己发布的

xxx信息(是由那个用户负责的 )比如教学计划(是由那个教室负责的)

任务管理 (是由哪个用户负责的)

我们可以总结一下:它是一个业务模块,同时关联一个角色模块的id

2.两个普通业务模块之间

借助上节课的旅游分类,我们做一个新的模块叫: 攻略分类

然后把具体的旅游攻略分类绑定上就可以了

1
2
3
4
5
CREATE TABLE `category` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '分类标题',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='旅游攻略分类表';

快速复用一个模块:

怎么把旅游攻略和攻略分类关联上呢?

1.要在旅游攻略表里面创建一个关联字段 (这不是让你创建一个外键 只是一个很普通的字段而已)

1
2
3
4
5
6
7
8
9
CREATE TABLE `introduction` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '主键',
`img` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '封面图',
`content` longtext COLLATE utf8mb4_unicode_ci COMMENT '攻略内容',
`title` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '攻略标题',
`time` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '发布时间',
`category_id` int DEFAULT NULL COMMENT '分类ID',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='旅游攻略表';

2.在实体类和sql都要增加这样一个字段

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<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.categoryData"
:key="item.id"
:label="item.title"
:value="item.id"
/>
</el-select>
</el-form-item>
1
2
3
4
5
6
7
8
9
10
const loadCategory = () => {
request.get('/category/selectAll').then(res=>{
if(res.code === '200'){
data.categoryData = res.data
}else{
ElMessage.error(res.msg)
}
})
}
loadCategory()

旅游分类如何展示分类标题?因为数据库展示的是数据库的分类,两种方法,看你喜欢那个?

​ a.java代码里写关联逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
public PageInfo<Introduction> selectPage(Integer pageNum, Integer pageSize,Introduction introduction) {
//开启分页查询
PageHelper.startPage(pageNum,pageSize);
List<Introduction> introductions = introductionMapper.selectAll(introduction);
for (Introduction dbintroduction : introductions) {
Integer categoryId = dbintroduction.getCategoryId();
Category category = categoryMapper.selectById(categoryId);
if(ObjectUtil.isNotEmpty(category)){
dbintroduction.setCategoryTitle(category.getTitle());
}
}
return PageInfo.of(introductions);
}

​ b.sql

1
2
3
4
5
6
7
8
9
10
<select id="selectAll" resultType="com.yjy.entity.Introduction">
SELECT introduction.*,category.title as categoryTitle FROM `introduction`
left join category on introduction.category_id = category.id
<where>
<if test="title != null and title != ''">
introduction.title LIKE CONCAT('%', #{title}, '%')
</if>
</where>
ORDER BY id DESC
</select>

3.业务模块与角色模块之间

借助上节课的旅游攻略,我们把它跟用户角色关联上,达到用户只能看到自己发布的攻略,管理员可以看到所有的。

1.要在旅游攻略表里面创建一个关联字段 (这不是让你创建一个外键 只是一个很普通的字段而已)

1
2
alter table introduction
add user_id int null comment '用户ID';

2.在实体类和sql都要增加这样一个字段

获取用户字段插入

1
2
Account currentUser = TokenUtils.getCurrentUser();
introduction.setUserId(currentUser.getId());

旅游分类如何展示分类标题?因为数据库展示的是数据库的分类,两种方法,看你喜欢那个?

​ a.java代码里写关联逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public PageInfo<Introduction> selectPage(Integer pageNum, Integer pageSize,Introduction introduction) {
//开启分页查询
PageHelper.startPage(pageNum,pageSize);
List<Introduction> introductions = introductionMapper.selectAll(introduction);
for (Introduction dbintroduction : introductions) {
Integer categoryId = dbintroduction.getCategoryId();
Integer userId = dbintroduction.getUserId();
Category category = categoryMapper.selectById(categoryId);
User user = userMapper.selectById(userId.toString());

if(ObjectUtil.isNotEmpty(category)){
dbintroduction.setCategoryTitle(category.getTitle());
}

if(ObjectUtil.isNotEmpty(user)){
dbintroduction.setUsername(user.getName());
}
}
return PageInfo.of(introductions);
}

​ b.sql

1
2
3
4
5
6
7
8
9
10
11
12
<select id="selectAll" resultType="com.yjy.entity.Introduction">
SELECT introduction.*,category.title as categoryTitle,user.name as username FROM `introduction`
left join category on introduction.category_id = category.id
left join user on introduction.user_id = user.id
<where>
<if test="title != null and title != ''">
introduction.title LIKE CONCAT('%', #{title}, '%')
</if>
</where>
ORDER BY id DESC
</select>

用户只能看到自己发布的文章:用户可以获取ID

1
2
3
4
5
6
public PageInfo<Introduction> selectPage(Integer pageNum, Integer pageSize,Introduction introduction) {
//查之前先给他条件
Account currentUser = TokenUtils.getCurrentUser();
if("USER".equals(currentUser.getRole())){
introduction.setUserId(currentUser.getId());
}

管理员可以看到所有:

1
2
3
4
5
6
7
8
9
10
<select id="selectAll" resultType="com.yjy.entity.Introduction">
SELECT introduction.*,category.title as categoryTitle,user.name as username FROM `introduction`
left join category on introduction.category_id = category.id
left join user on introduction.user_id = user.id
<where>
<if test="title != null and title != ''">introduction.title LIKE CONCAT('%', #{title}, '%')</if>
<if test="userId!= null">introduction.user_id = #{userId}</if>
</where>
ORDER BY id DESC
</select>