Mybatis多对一的处理

根据结果嵌套查询

数据表如上一篇博文一对多的学生和老师,实体类修改为:
学生类:

package com.kuang.pojo;

import lombok.Data;

@Data
public class Student {
    
    
    private int id;
    private String name;
    private int tid;

}

老师类:

package com.kuang.pojo;
import lombok.Data;
import java.util.List;
@Data
public class Teacher {
    
    
    private int id;
    private String name;
private List<Student>stus;//一个老师关联多个学生
}

接口:

package com.kuang.dao;
import com.kuang.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface TeacherMapper {
    
    
    List<Teacher>getTeacher();
}

xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.TeacherMapper">
    <select id="getTeacher" resultType="com.kuang.pojo.Teacher">
        select * from teacher;
    </select>
</mapper>

测试类:

 @Test
    public void  testeacher(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
for(Teacher teacher: sqlSession.getMapper(TeacherMapper.class).getTeacher()){
    
    
    System.out.println(teacher);
}
        sqlSession.close();
    }

结果:
在这里插入图片描述
用sql语句查询一个老师下的所有学生:

select * from student s,teacher t where s.tid=t.id

在这里插入图片描述

// ListgetTeacher();
//获取指定老师下所有学生的信息

> <!--    这里的 <collection property="stus" 对应   private 
> List<Student>stus;一个老师关联多个学生  javatype:指定属性的类型 oftype:集合中的泛型信息-->

注意:
Teachermapper.xml文件中不能有中午注释,否则会报错
输出一个老师对应的多个学生进行的处理:
接口:

public interface TeacherMapper {
    
    
    //获取指定老师下所有学生的信息
    Teacher getteacher(@Param("tid") int id);
}

实体:

@Data
public class Teacher {
    
    
    private int id;
    private String name;
private List<Student>stus;//一个老师关联多个学生
}

测试类:

   @Test
    public void  testeacher(){
    
    
        SqlSession sqlSession = MybatisUtils.getSqlSession();
Teacher teacher=sqlSession.getMapper(TeacherMapper.class).getteacher(1);
    System.out.println(teacher);

        sqlSession.close();
    }

xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.kuang.dao.TeacherMapper">
    <select id="getteacher" resultMap="Teacherstudent">

        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}
    </select>
    <resultMap id="Teacherstudent" type="com.kuang.pojo.Teacher">

      <result property="id" column="tid"/>

    <result property="name" column="tname"/>
        <collection property="stus" ofType="com.kuang.pojo.Student">
            <result property="id" column="sid"></result>
            <result property="name" column="sname"/>
            <result property="tid" column="tid"></result>
        </collection>
    </resultMap>
</mapper>

运行结果:
在这里插入图片描述
注意
xml中的语法非常严格 一定要注意拼写和符号 空格等方面都不能出错

javatype和oftype

1.javatype 用来指定实体类中属性的类型
2。oftype 用来指定映射到list中或者集合中的pojo类型

猜你喜欢

转载自blog.csdn.net/qq_41358574/article/details/113786101