五、Mybtis实现关联表查询

5.1一对一关联查询

五、Mybtis实现关联表查询_第1张图片

5.1.1创建表和数据

CREATE TABLE teacher(
   t_id INT PRIMARY KEY AUTO_INCREMENT,
   t_name VARCHAR(20)
);

CREATE TABLE class(
   c_id INT PRIMARY KEY AUTO_INCREMENT,
   c_name VARCHAR(20),teacher_id INT
);
ALTER TABLE class ADD CONSTRAINT fk_teacher_id FOREIGN KEY (teacher_id) REFERENCESteacher(t_id);
INSERT INTO teacher(t_name) VALUES('LS1');
INSERT INTO teacher(t_name) VALUES('LS2');
INSERT INTO class(c_name, teacher_id) VALUES('bj_a', 1);
INSERT INTO class(c_name, teacher_id) VALUES('bj_b', 2);

5.1.2定义两个实体类

Classes.java

package com.entity;

public class Classes {

    private int id;
    private String name;
    private Teacher teacher;
    public Classes()
    {
        
    }
    public Classes(int id, String name, Teacher teacher) {
        super();
        this.id = id;
        this.name = name;
        this.teacher = teacher;
    }
    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 Teacher getTeacher() {
        return teacher;
    }
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
    @Override
    public String toString() {
        return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher
                + "]";
    }
    
}

Teacher.java

package com.entity;

public class Teacher {
    private int id;
    private String name;
    public Teacher()
    {
        
    }
    public Teacher(int id, String name) {
        super();
        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 "Teacher [id=" + id + ", name=" + name + "]";
    }
    

}

5.1.3定义映射文件

ClassesMapper.xml




    

    
    

    
        
        
        
            
            
        
    


    
    

    

    
        
        
        
            
            
        
    



  • association:用于一对一的关联查询
  • property:对象属性名称
  • javaType:对象属性的类型
  • column:所对应的外键字段名称
  • select:使用另一个查询封装的结构

5.1.4config.xml中注册ClassMapper.xml


      

5.1.5测试类

package com.test;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.entity.Classes;
import com.util.MybatisUtil;

public class Test4 {

    //第一种方式的测试
    @Test
    public void getClass1() throws IOException
    {
        SqlSession session=MybatisUtil.getSession();
        String statement="com.config.ClassMapper.getClass";
        
        Classes classes=session.selectOne(statement, 1);
        session.close();
        System.out.println(classes);
    }
    
    //第二种方式的测试
        @Test
        public void getClass2() throws IOException
        {
            SqlSession session=MybatisUtil.getSession();
            String statement="com.config.ClassMapper.getClass2";
            
            Classes classes=session.selectOne(statement, 1);
            session.close();
            System.out.println(classes);
        }
}

5.2一对多关联表查询

五、Mybtis实现关联表查询_第2张图片
Paste_Image.png

5.2.1创建表和数据

CREATE TABLE student(
    s_id INT PRIMARY KEY AUTO_INCREMENT,
    s_name VARCHAR(20),class_id INT
);
INSERT INTO student(s_name, class_id) VALUES('xs_A', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_B', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_C', 1);
INSERT INTO student(s_name, class_id) VALUES('xs_D', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_E', 2);
INSERT INTO student(s_name, class_id) VALUES('xs_F', 2);

5.2.2定义实体类

Classes.java

package com.entity;

import java.util.List;

public class Classes {

    private int id;
    private String name;
    private Teacher teacher;
    private List list;

    public Classes() {

    }

    public Classes(int id, String name, Teacher teacher, List list) {
        super();
        this.id = id;
        this.name = name;
        this.teacher = teacher;
        this.list = list;
    }

    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 Teacher getTeacher() {
        return teacher;
    }

    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }

    @Override
    public String toString() {
        return "Classes [id=" + id + ", name=" + name + ", teacher=" + teacher
                + ", list=" + list + "]";
    }

}

5.2.3定义 **sql **映射文件 Class2Mapper.xml

Class2Mapper.xml




    

    

    

    
        
        
        
             
             
        
        
            
            
        
    
    
      
     
     
     
     
     
     
     
        
        
        
        
        
        
        
    

  • collection:做一对多关联映射查询
  • ofType:指点集合中元素对象类型

5.2.4将映射文件注册到config.xml中

config.xml


      

5.2.5测试类

Test5.java

package com.test;

import java.io.IOException;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;



import com.entity.Classes;
import com.util.MybatisUtil;

public class Test5 {
    
    //方式一
    @Test
    public void testSelect1() throws IOException
    {
        SqlSession session=MybatisUtil.getSession();
        String statement="com.config.Class2Mapper.getClass";
        
        Classes classes=session.selectOne(statement, 1);
        session.close();
        System.out.println(classes);
    }
    
    //方式二
        @Test
        public void testSelect2() throws IOException
        {
            SqlSession session=MybatisUtil.getSession();
            String statement="com.config.Class2Mapper.getClass2";
            
            Classes classes=session.selectOne(statement, 1);
            session.close();
            System.out.println(classes);
        }

}

你可能感兴趣的:(五、Mybtis实现关联表查询)