根据回复状态、教师姓名、学生姓名(三表连接)实现查询

1、实现效果


2、DAO接口写法

List<Interact> selectByStatusAndPerson(@Param("interactStatus")Integer interactStatus,
            @Param("teacherName")String teacherName,@Param("studentName")String studentName);

3、MyBatis动态SQL

<resultMap type="interact" id="interactMap">
        <id column="inteId" property="interactId"/>
        <result column="inteId" property="interactId"/>
        <result column="inteTitle" property="interactTitle"/>
        <result column="inteStatus" property="interactStatus"/>
        <association property="teacher" javaType="teacher"> 
            <id column="teacher_id" property="teacherId"/>
            <result column="teaName" property="teacherName"/>
        </association>
        <association property="student" javaType="student"> 
            <id column="student_id" property="studentId"/>
            <result column="stuName" property="studentName"/>
        </association>        
    </resultMap>
<select id="selectByStatusAndPerson" resultMap="interactMap">
        select inte.interact_id as inteId,
        inte.interact_title as inteTitle,tea.teacher_name as teaName,
        stu.student_name as stuName,inte.interact_status as inteStatus
        from (select * from interact_interact
        <where>
            <if test="interactStatus!=null">
                interact_status = #{interactStatus}
            </if>
        </where>
        ) inte join (select * from interact_teacher
        <where>
            <if test="teacherName!=null">
                teacher_name like '%'||#{teacherName}||'%'
            </if>
        </where> 
        ) tea on inte.teacher_id = tea.teacher_id  
        join (select * from interact_student
        <where>
            <if test="studentName!=null">
                student_name like '%'||#{studentName}||'%'
            </if>
        </where>
        ) stu on inte.student_id = stu.student_id         
        order by inte.create_time desc
    </select>
4、Controller
@RequestMapping("/queryByStatusAndPerson")
    public String queryByStatusAndPerson(Integer interactStatus,
            String teacherName,String studentName,Model model){
        List<Interact> queryByStatusAndPerson = is.queryByStatusAndPerson(interactStatus, teacherName, studentName);
        model.addAttribute("interacts", queryByStatusAndPerson);
        return "teacher/queryInteracts";
    }
5、jsp
<form action="${pageContext.request.contextPath }/teacher/queryByStatusAndPerson.do" method="post" id="queryForm">       
        <div class="input-append" style="padding-left:40px">
        <select class="span3" name="interactStatus" id="sele" style="width: 150px; margin-left: 10px; border-radius:4px;" >
                 <option value="" >请选择回复状态</option>
       			 <option value="0" >未回复</option>
       			 <option value="1">已回复</option>
        </select>      
            <input value="" class="span2" type="text" placeholder="请输入教师姓名" name="teacherName" style="width: 250px; height: 20px;margin-left: 10px; border-radius:4px;"> 
            <input value="" class="span2" type="text" placeholder="请输入学生姓名" name="studentName" style="width: 250px; height: 20px;margin-left: 10px; border-radius:4px;"> 
         	<button class="btn" type="submit" style="margin-left: 10px; border-radius:10px;" >搜索</button>
          </div>
      </form>

猜你喜欢

转载自blog.csdn.net/weixin_42273990/article/details/80723462