15. Springboot3+Vue3实现模块之间的关联

861 字约 2 分钟读完4454 次阅读更新于 2026/5/3

什么叫模块之间的关联?它都有哪些形式?

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

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

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

……

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

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

教学计划(是由哪个教师负责的)

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

……

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

两个普通业务模块之间

借助上节课的旅游攻略,我们做一个新的模块叫:攻略分类。然后把具体的旅游攻略跟攻略分类绑定上就可以了。

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. 要在旅游攻略表里面创建一个关联字段(这不是让你创建一个外键,只是一个很普通的字段而已)
  2. 在实体类和sql里面对应的也加上这个字段
  3. 我要给这个字段初始化数据了,在页面新增或者编辑的时候,要把数据传递到后端,操作到数据库里
<el-form-item prop="title" label="攻略分类">
  <el-select
      v-model="data.form.categoryId"
      placeholder="请选择攻略分类"
      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>
const loadCategory = () => {
  request.get('/category/selectAll').then(res => {
    if (res.code === '200') {
      data.categoryData = res.data
    } else {
      ElMessage.error(res.msg)
    }
  })
}
loadCategory()
  1. 数据库存的是分类的id,如何展示分类标题?有两个方法,我都教给你,看你喜欢哪个?
    1. java代码里写关联逻辑
// 这个list里面存储了旅游攻略的原始数据(只有分类id,categoryId)
for (Introduction dbIntroduction : list) {
    // 先拿到categoryId
    Integer categoryId = dbIntroduction.getCategoryId();
    // 通过categoryId从category表里通过主键查询出分类数据
    Category category = categoryMapper.selectById(categoryId);
    if (ObjectUtil.isNotEmpty(category)) {
        // 把分类的title赋值给categoryTitle
        dbIntroduction.setCategoryTitle(category.getTitle());
    }
}
2. sql里写关联逻辑
select introduction.*, category.title as categoryTitle from `introduction`
left join category on introduction.category_id = category.id

业务模块与角色模块之间

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

怎么把旅游攻略和用户模块关联上呢?

  1. 要在旅游攻略表里面创建一个关联字段(这不是让你创建一个外键,只是一个很普通的字段而已)
  2. 在实体类和sql里面对应的也加上这个字段
  3. 我要给这个字段初始化数据了,在页面新增或者编辑的时候,要把数据传递到后端,操作到数据库里
Account currentUser = TokenUtils.getCurrentUser();
introduction.setUserId(currentUser.getId());
  1. 数据库存的是分类的id,如何展示分类标题?有两个方法,我都教给你,看你喜欢哪个?
    1. java代码里写关联逻辑
    2. sql里写关联逻辑

关联查询汇总代码(a和b两种方法):

public PageInfo<Introduction> selectPage(Integer pageNum, Integer pageSize, Introduction introduction) {
    // 开启分页查询
    PageHelper.startPage(pageNum, pageSize);
    List<Introduction> list = introductionMapper.selectAll(introduction);
    // 这个list里面存储了旅游攻略的原始数据(只有分类id,categoryId)
    /*for (Introduction dbIntroduction : list) {
        // 先拿到categoryId
        Integer categoryId = dbIntroduction.getCategoryId();
        Integer userId = dbIntroduction.getUserId();
        // 通过categoryId从category表里通过主键查询出分类数据
        Category category = categoryMapper.selectById(categoryId);
        User user = userMapper.selectById(userId.toString());
        if (ObjectUtil.isNotEmpty(category)) {
            // 把分类的title赋值给categoryTitle
            dbIntroduction.setCategoryTitle(category.getTitle());
        }
        if (ObjectUtil.isNotEmpty(user)) {
            dbIntroduction.setUserName(user.getName());
        }
    }*/
    return PageInfo.of(list);
}
<select id="selectAll" resultType="com.example.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 != ''">and introduction.title like concat('%', #{title}, '%')</if>  <!-- 相当于 title like '%1%' -->
    </where>
    order by id desc
</select>

第13节权限补充

// 查之前要先给他条件
Account currentUser = TokenUtils.getCurrentUser();
if ("USER".equals(currentUser.getRole())) {
    introduction.setUserId(currentUser.getId());
}