栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Mybatis实现复杂查询(多对一、一对多)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mybatis实现复杂查询(多对一、一对多)

注:本文章所有内容都建立在上篇文章的基础上

接上篇博客:https://blog.csdn.net/liyuuhuvnjjv/article/details/122244531

文章目录
  • 案例:查询所有Student的信息(多对一)
    • 步骤一:修改StudentMapper.xml文件
    • 步骤二:编写测试类
    • 查询结果分析
    • 解决办法:
      • 按照查询嵌套处理(子查询):
        • 修改StudentMapper.xml文件
        • 查询结果:
      • 按照结果嵌套处理(联表查询)
  • 案例:查询Teacher信息
    • 步骤一:修改实体类
      • Student类
      • Teacher类
    • 步骤二:修改TeacherMapper类
    • 步骤三:修改TeacherMapper.xml文件
    • 步骤四:编写测试类
    • 查询结果分析
    • 解决方案:
      • 按结果嵌套查询(联合查询):
        • 步骤一:修改实体类
        • 步骤二:修改TeacherMapper.xml文件
        • 步骤三:编写测试类
        • 查询结果


案例:查询所有Student的信息(多对一) 步骤一:修改StudentMapper.xml文件




    
        select * from mybatis.student
    

步骤二:编写测试类
import org.apache.ibatis.session.SqlSession;
import org.dao.StudentMapper;
import org.dao.TeacherMapper;
import org.junit.Test;
import org.pojo.Student;
import org.pojo.Teacher;
import org.utils.MybatisUtils;

import java.util.List;

public class MyTest {
    @Test
    public void testStudent(){
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtils.getSqlSession();
            StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            List studentList = mapper.getStudent();
            for(Student student : studentList){
                System.out.println(student);
            }
        }catch(Exception e){
            e.printStackTrace();
        }catch(Error e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
}

查询结果分析


问题:经过上述查询,虽然Student的id,name已经查到了,但是其对应的teacher为null!!!

解决办法: 按照查询嵌套处理(子查询):

利用resultMap处理结果集映射

修改StudentMapper.xml文件




    
    
        select * from mybatis.teacher where id=#{id}
    


查询结果:


可以看到,这样我们就得到了完整的Student信息

按照结果嵌套处理(联表查询)
  • 修改StudentMapper.xml文件





    
    
        select * from mybatis.teacher;
    

步骤四:编写测试类
import org.apache.ibatis.session.SqlSession;
import org.dao.TeacherMapper;
import org.junit.Test;
import org.pojo.Teacher;
import org.utils.MybatisUtils;

import java.util.List;

public class MyTest {
    @Test
    public void test(){
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtils.getSqlSession();
            TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
            List teacherList = mapper.getTeacher();
            for(Teacher teacher : teacherList){
                System.out.println(teacher);
            }
        }catch(Exception e){
            e.printStackTrace();
        }catch(Error e){
            e.printStackTrace();
        }finally{
            sqlSession.close();
        }
    }
}

查询结果分析

问题:虽然查询到了teacher的信息,但是其对应的Student为null!!!

解决方案: 按结果嵌套查询(联合查询): 步骤一:修改实体类

修改TeacherMapper类

package org.dao;

import org.apache.ibatis.annotations.Param;
import org.pojo.Teacher;

import java.util.List;

public interface TeacherMapper {
    Teacher getTeacher(@Param("tid") int id);
    //select s.id sid,s.name sname,t.name tname,t.id tid from student s,teacher t where s.tid = t.id
}

步骤二:修改TeacherMapper.xml文件




    
    
        select s.id sid,s.name sname,t.name tname,t.id tid
        from student s,teacher t
        where s.tid = t.id and t.id=#{tid}
    

    
        
        
        
        
            
            
            
        
    



步骤三:编写测试类
import org.apache.ibatis.session.SqlSession;
import org.dao.TeacherMapper;
import org.junit.Test;
import org.pojo.Teacher;
import org.utils.MybatisUtils;

import java.util.List;

public class MyTest {
    @Test
    public void testTeacher(){
        SqlSession sqlSession = null;
        try{
            sqlSession = MybatisUtils.getSqlSession();
            TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);
            Teacher teacher = mapper.getTeacher(1);
            System.out.println(teacher);
        }catch(Exception e){
            e.printStackTrace();
        }catch(Error e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }
    }
}

查询结果


可以看到,经过上面的按结果嵌套查询,我们已经得到了该teacher的信息,包括其下的student信息

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/685657.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号