public PageInfo<Course> selectPage(Course course, int pageNum, int pageSize) { //查之前先给条件,判断角色 Account currentUser = TokenUtils.getCurrentUser(); if ("TEACHER".equals(currentUser.getRole())) { course.setTeacherId(currentUser.getId()); // 老师设置 teacherId } else { course.setTeacherId(null); // 显式清除可能存在的残留值 } //开启分页查询 PageHelper.startPage(pageNum, pageSize); List<Course> list = courseMapper.selectAll(course); return new PageInfo<>(list); }<?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.CourseMapper"> <select id="selectAll" resultType="com.example.entity.Course"> select course.*,category.title as categoryTitle from `course` left join category on course.category_id = category.id left join teacher on course.teacher_id = teacher.id <where> <if test="name !=null and name!=''" >and course.name like concat('%',#{name},'%')</if> <if test="teacherId != null"> and teacher_id = #{teacherId} </if> <if test="ids!=null and ids!='' " > and id in <foreach collection="idsArr" open="(" close=")" separator="," item="id"> #{id} </foreach> </if> </where> order by id asc </select> <insert id="insert" parameterType="com.example.entity.Course"> insert into `course` ( name,intro,type,teacher_id,img,recommend,video,file,category_id,category_parent_id,view_count) values (#{name},#{intro},#{type},#{teacherId},#{img},#{recommend},#{video},#{file},#{categoryId},#{categoryParentId},#{viewCount}) </insert> <update id="updateById" parameterType="com.example.entity.Course"> update `course` set name=#{name}, intro=#{intro}, type=#{type}, teacher_id=#{teacherId}, img=#{img}, recommend=#{recommend}, video=#{video}, file=#{file}, category_id=#{categoryId}, category_parent_id=#{categoryParentId}, view_count=#{viewCount} where id=#{id} </update> </mapper>还是不行,分页查询如何让老师发布的课程只有发布的它的老师可以看到,但是管理员可以看到全部的课程,现在已经实现老师只能查看自己发布的课程,但是管理员一个课程都看不到了
时间: 2025-07-10 15:43:13 浏览: 15
<think>好的,我现在需要解决用户的问题:在使用分页查询时,管理员无法看到任何课程,而老师只能看到自己发布的课程。用户希望管理员能查看全部课程,而老师仅查看自己的。根据提供的代码和Mapper文件,可能存在哪里出错了呢?
首先,查看用户提供的Java方法selectPage。这里根据当前用户的角色,如果是老师,设置course的teacherId为当前用户ID,否则设为null。然后进行分页查询。问题可能出在这里,当用户是管理员时,teacherId被显式设置为null,导致查询条件中teacher_id = null,而数据库中没有这样的记录,所以管理员看不到任何课程。
接下来,检查Mapper XML中的selectAll语句。在where条件中,有一个判断:如果teacherId不为null,就添加and teacher_id = #{teacherId}。当管理员查询时,course对象的teacherId被设置为null,因此这个条件不生效,理论上应该返回所有课程。但实际结果却是管理员看不到课程,这说明可能存在其他条件限制了查询结果。
查看Mapper中的其他条件,发现还有一个关于ids的判断:如果ids不为空,就添加id in (…)。这可能存在问题,因为当管理员查询时,如果course对象中的ids属性为null或空,这个条件可能被错误地应用,导致查询结果为空。例如,如果ids没有被正确初始化,或者前端传递的参数有误,可能会导致生成类似id IN ()的无效SQL,从而返回空结果。
此外,用户提到管理员应该看到所有课程,但当前代码在管理员情况下将teacherId设为null,而Mapper中的teacherId条件只有在teacherId不为null时才会添加。因此,管理员查询时,teacher_id条件不会被添加,但可能由于其他条件(如ids)导致查询结果为空。
可能的解决方案是:
1. 确保在管理员情况下,course对象中的teacherId为null,并且其他条件(如ids)不会错误地限制查询结果。可能需要检查前端传递的参数,或者在设置course对象时清除不必要的条件。
2. 修改Java代码中的条件设置逻辑,确保管理员不会设置teacherId,并且其他参数如ids被正确处理。例如,在管理员情况下,不仅设置teacherId为null,还要确保其他条件如ids被正确清除或设置为null,以避免生成无效的SQL条件。
3. 检查Mapper文件中的SQL语句,特别是关于ids的条件。如果ids参数由前端传递,可能在管理员情况下未正确传递,导致生成id IN ()的条件,从而返回空结果。需要确保当ids不存在时,该条件不被添加。
例如,在Mapper中,判断ids是否为空的条件是否正确?当前代码中的条件是<if test="ids!=null and ids!='' ">,但ids可能是一个数组或集合,可能需要使用不同的判断方式。例如,如果ids是一个数组,应该检查是否为空数组,而不是字符串是否为空。可能这里的条件有误,导致即使管理员没有传递ids参数,也错误地添加了id IN ()的条件,从而导致查询无结果。
另外,检查生成的SQL语句,看管理员查询时实际执行的SQL是什么样的,是否有不正确的条件。例如,是否有类似id IN ()的情况,这会导致SQL语法错误,从而返回空结果。此时,需要修正Mapper中的条件判断,确保只有当ids存在且不为空时才添加该条件。
总结问题可能出在Mapper中的ids条件判断,导致管理员查询时附加了无效的id IN ()条件,从而返回空结果。解决方案是修正该条件,确保只有当ids有效时才应用该条件,否则不添加。此外,确保在管理员情况下,其他条件如teacherId不会干扰查询。</think>我们来逐步分析并解决分页查询中管理员无法查看课程的问题:
**问题根源分析:**
1. **Mapper XML中的`ids`条件判断有误**
```xml
<if test="ids!=null and ids!='' ">
```
当`ids`是数组时,这种判断方式无效。MyBatis无法正确处理数组的空值检查,导致生成`id IN ()`的无效SQL。
2. **管理员查询时参数传递问题**
若前端传递了空`ids`参数,会导致无效查询条件。
**解决方案:**
**步骤1:修正Mapper中的数组判空条件**
```xml
<if test="idsArr != null and idsArr.length > 0"> <!-- 使用数组长度判断 -->
and id in
<foreach collection="idsArr" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
```
**步骤2:确保Java代码正确处理参数**
```java
public PageInfo<Course> selectPage(Course course, int pageNum, int pageSize) {
Account currentUser = TokenUtils.getCurrentUser();
if ("TEACHER".equals(currentUser.getRole())) {
course.setTeacherId(currentUser.getId());
} else {
course.setTeacherId(null); // 确保非老师角色不携带teacher_id
// 清除可能干扰查询的参数(如前端误传的ids)
course.setIds(null);
}
PageHelper.startPage(pageNum, pageSize);
List<Course> list = courseMapper.selectAll(course);
return new PageInfo<>(list);
}
```
**步骤3:验证生成的SQL语句**
管理员查询时应生成如下SQL(无多余条件):
```sql
SELECT ...
FROM course
LEFT JOIN ...
ORDER BY id ASC
```
**关键点解释:**
1. **数组判空逻辑**
使用`idsArr.length > 0`确保只有当数组存在且非空时添加`IN`条件。
2. **参数清理**
管理员查询时主动清除`teacherId`和可能干扰的`ids`参数,避免残留值影响查询。
**补充建议:**
- 调试时输出`course`对象的参数值,确认管理员查询时无额外限制条件
- 检查数据库权限,确保管理员账号有课程表查询权限
- 使用MyBatis日志功能查看实际执行的SQL语句
**修正后的完整Mapper片段:**
```xml
<select id="selectAll" resultType="com.example.entity.Course">
select course.*, category.title as categoryTitle
from `course`
left join category on course.category_id = category.id
left join teacher on course.teacher_id = teacher.id
<where>
<if test="name != null and name != ''">
and course.name like concat('%', #{name}, '%')
</if>
<if test="teacherId != null">
and teacher_id = #{teacherId}
</if>
<if test="idsArr != null and idsArr.length > 0">
and id in
<foreach collection="idsArr" open="(" close=")" separator="," item="id">
#{id}
</foreach>
</if>
</where>
order by id asc
</select>
```
通过以上调整,系统将实现:
- 老师只能查看自己发布的课程(通过`teacher_id`过滤)
- 管理员可查看全部课程(无附加过滤条件)
- 避免无效SQL条件导致的空结果问题
阅读全文
相关推荐



















