Mybatis中一个对象包含多个其他对象的处理

对应上一篇博客中,多个学生有同一个班主任的查询,那么要是想查询一个班主任下的所有学生,这样一对多的情况该如何处理呢?

文章目录

        • 1.数据库思想:连表查询
        • 2.面向对象思想

同样的,首先给出实体类,工具类,Mybatis核心配置文件的代码以便更好理解
Student.javaStudent实体类

package com.MLXH.pojo;

public class Student {
     
    private int id;
    private String name;
    
    public Student() {
     
    }

    public Student(int id, String name) {
     
        this.id = id;
        this.name = name;
    }

    public int getId() {
     
        return id;
    }

    public void setId(int id) {
     
        this.id = id;
    }

    public String getName() {
     
        return name;
    }

    public void setName(String name) {
     
        this.name = name;
    }

    @Override
    public String toString() {
     
        return "Student{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
}

TeacherTeacher实体类

package com.MLXH.pojo;

import java.util.List;

public class Teacher {
     
    private int id;
    private String name;

    //一个老师对应对个学生
    private List<Student> students;

    public Teacher() {
     
    }

    public Teacher(int id, String name, List<Student> students) {
     
        this.id = id;
        this.name = name;
        this.students = students;
    }

    public int getId() {
     
        return id;
    }

    public void setId(int id) {
     
        this.id = id;
    }

    public String getName() {
     
        return name;
    }

    public void setName(String name) {
     
        this.name = name;
    }

    public List<Student> getStudents() {
     
        return students;
    }

    public void setStudents(List<Student> students) {
     
        this.students = students;
    }

    @Override
    public String toString() {
     
        return "Teacher{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", students=" + students +
                '}';
    }
}

工具类:主要是为了更为方便的获得sqlSession对象并设置了自动提交事务

package com.MLXH.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;


//mybatis的工具类,重复的代码的提纯
public class MyBatisUtils {
     

    //类变量不需要设置默认值;
    private static SqlSessionFactory sqlSessionFactory;

    static {
     

        //在maven中,所有的资源文件一般都放在resources目录下,我们可以直接拿到。
        try {
     
            String resource = "mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
     
            e.printStackTrace();
        }

    }

    //设置SqlSessionFactory公共的方法
    public static SqlSessionFactory getSqlSessionFactory(){
     
        return sqlSessionFactory;
    }

    //获得一个带事务自动提交功能的SqlSession公共的方法
    public static SqlSession getSqlSession(){
     
        //自动提交事务
        return sqlSessionFactory.openSession(true);
    }
}

Mybatis核心配置文件:mybatis-config.xml



<configuration>

    
    <properties resource="database.properties"/>

    
    <settings>
        
        
        <setting name="logImpl" value="LOG4J"/>
    settings>

    
    <typeAliases>
        <package name="com.MLXH.pojo"/>
    typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            dataSource>
        environment>
    environments>

    <mappers>
        
        
        <mapper resource="com/MLXH/dao/TeacherMapper.xml"/>
    mappers>

configuration>

database.properties配置文件:数据库所需数据

driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf-8
username = root
password = 123456

TeacherDao接口

package com.MLXH.dao;

import com.MLXH.pojo.Teacher;

public interface TeacherDao {
     

    //获得一个老师下的所有学生信息; 老师是包含学生的集合;
    Teacher getTeacher(int id);

    Teacher getTeacherTwo(int id);
}

针对于TeacherDao接口的实现mapper文件:我们使用如下的方式来进行查询

1.数据库思想:连表查询




<mapper namespace="com.MLXH.dao.TeacherDao">

    
    <select id="getTeacherTwo" resultMap="TeacherStudent2">
        select * from mybatis.teacher where id = #{id}
    select>
    <resultMap id="TeacherStudent2" type="Teacher">
        <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="T2"/>
    resultMap>
    <select id="T2" resultType="Student">
        select * from mybatis.student where tid = #{id}
    select>

mapper>

测试

 @Test
    public void getTeacher(){
     
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TeacherDao mapper = sqlSession.getMapper(TeacherDao.class);
        Teacher teacher = mapper.getTeacher(1);

        System.out.println(teacher.getName());
        System.out.println(teacher.getStudents());

    }

2.面向对象思想




<mapper namespace="com.MLXH.dao.TeacherDao">

    
    
    <select id="getTeacher" resultMap="TeacherStudent">
        select s.name sname,s.id sid, t.name tname
        from mybatis.student as s,mybatis.teacher as t
        where s.tid = t.id and t.id = #{id}
    select>
    <resultMap id="TeacherStudent" type="Teacher">
        <result property="id" column="id"/>
        <result property="name" column="tname"/>
        <collection property="students" ofType="Student">
            <id property="id" column="sid"/>
            <result property="name" column="sname"/>
        collection>
    resultMap>
mapper>

测试

@Test
    public void getTeacherTwo(){
     
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        TeacherDao mapper = sqlSession.getMapper(TeacherDao.class);
        Teacher teacher = mapper.getTeacherTwo(1);

        System.out.println(teacher.getName());
        System.out.println(teacher.getStudents());
    }

结果:在这里插入图片描述
总结:

  • 一对多:collection 集合
  • 两种解决方式:
    面对对象的思想:关联对象
    SQL语句思想:联表查询

多对一使用 association :关联
一对多使用 collection :集合
可以理解为:多对一,为多个对象关联一个对象,一对多为一个对象有一个集合,这个集合包含了多个对象,这些更加便于理解和记忆…

你可能感兴趣的:(Mybatis,一对多)