Mybatis多表操作

1.多表操作概述

多表模型分类

一对一:在任意一方建立外键,关联对方的主键

一对多:在多的一方建立外键,关联一的一方的主键

多对多:借助中间表,中间表至少两个字段,分别关联两张表的主键

2.一对一操作

一对一模型: 人和身份证,一个人只有一个身份证

数据准备

sql数据准备:

CREATE TABLE person(
    id INT PRIMARY KEY AUTO_INCREMENT,
    NAME VARCHAR(20),
    age INT
);
INSERT INTO person VALUES (NULL,'张三',23);
INSERT INTO person VALUES (NULL,'李四',24);
INSERT INTO person VALUES (NULL,'王五',25);

CREATE TABLE card(
    id INT PRIMARY KEY AUTO_INCREMENT,
    number VARCHAR(30),
    pid INT,
    CONSTRAINT cp_fk FOREIGN KEY (pid) REFERENCES person(id)
);
INSERT INTO card VALUES (NULL,'12345',1);
INSERT INTO card VALUES (NULL,'23456',2);
INSERT INTO card VALUES (NULL,'34567',3);

bean.Person:

package Mybatis03.bean;

public class Person {
    private Integer id;     //主键id
    private String name;    //人的姓名
    private Integer age;    //人的年龄

    public Person() {
    }

    public Person(Integer id, String name, Integer age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

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

bean.Card:

package Mybatis03.bean;

public class Card {
    private Integer id;  //主键id
    private String number;  //身份证号
    private Person p;  //所属人的对象

    public Card() {
    }

    public Card(Integer id, String number, Person p) {
        this.id = id;
        this.number = number;
        this.p = p;
    }

    public Integer getId() {
        return id;
    }

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

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public Person getP() {
        return p;
    }

    public void setP(Person p) {
        this.p = p;
    }

    @Override
    public String toString() {
        return "Card{" +
                "id=" + id +
                ", number='" + number + '\'' +
                ", p=" + p +
                '}';
    }
}

jdbc.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db1
username=root
password=123456

MyBatisConfig.xml:






    
    
    
    
        
    

    
    
        


    
    
        
    

        
    
        
        
            
            
            
            
                
                
                
                
                
            
        
    
    
    
        
        
    

功能实现

:配置字段和对象属性的映射关系标签。
    id 属性:唯一标识
    type 属性:实体对象类型
:配置主键映射关系标签。
:配置非主键映射关系标签。

    column 属性:表中字段名称
    property 属性: 实体对象变量名称
:配置被包含对象的映射关系标签。
    property 属性:被包含对象的变量名
    javaType 属性:被包含对象的数据类型
 

新建one_to_one包存放对应映射配置文件





    
    
        
        
        
        
        
            
            
            
        
    
    

创建接口table01.OneToOneMapper

package table01;

import Mybatis03.bean.Card;

import java.util.List;

public interface OneToOneMapper {
    //查询全部
    public abstract List selectAll();
}

测试类:

package table01;

import Mybatis03.bean.Card;
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 org.junit.Test;


import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectAll() throws Exception{
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        // 2.获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过工厂对象获取SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //4.获取StudentMapper接口的实现类对象
        OneToOneMapper mapper = sqlSession.getMapper(OneToOneMapper.class);
        //5.调用实现类的方法,接收结果
        List list = mapper.selectAll();
        //6.处理结果
        for(Card c : list){
            System.out.println(c);
        }
        //7.释放资源
        sqlSession.close();
        is.close();
    }
}

3.一对多操作 

数据准备

一对多模型: 一对多模型:班级和学生,一个班级可以有多个学生

sql数据准备:

CREATE TABLE classes(
  id INT PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(20)
);
INSERT INTO classes VALUES (NULL,'一班');
INSERT INTO classes VALUES (NULL,'二班');

CREATE TABLE student(
  id INT PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(30),
  age INT,
  cid INT,
  CONSTRAINT cs_fk FOREIGN KEY (cid) REFERENCES classes(id)
);
INSERT INTO student VALUES (NULL,'张三',23,1);
INSERT INTO student VALUES (NULL,'李四',24,1);
INSERT INTO student VALUES (NULL,'王五',25,2);
INSERT INTO student VALUES (NULL,'赵六',26,2);

 bean.Classes:

package Mybatis03.bean;

import java.util.List;

public class Classes {
    private Integer id;     //主键id
    private String name;    //班级名称
    private List students; //班级中所有学生对象		一个班级里多个学生

    public Classes() {
    }

    public Classes(Integer id, String name, List students) {
        this.id = id;
        this.name = name;
        this.students = students;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public List getStudents() {
        return students;
    }

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

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

bean.Student:

package Mybatis03.bean;

public class Student {
    private Integer id;     //主键id
    private String name;    //学生姓名
    private Integer age;    //学生年龄

    public Student() {
    }

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

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

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

功能实现

:配置字段和对象属性的映射关系标签。
    id 属性:唯一标识
    type 属性:实体对象类型
:配置主键映射关系标签。
:配置非主键映射关系标签。

    column 属性:表中字段名称
    property 属性: 实体对象变量名称
:配置被包含集合对象的映射关系标签。
    property 属性:被包含集合对象的变量名
    ofType 属性:集合中保存的对象数据类型
 

新建one_to_many包存放映射配置文件





    
        
        

        
        
            
            
            
        
    
    

配置映射文件: 


        
        
        
    

创建接口:

package Mybatis03.table02;

import Mybatis03.bean.Classes;

import java.util.List;

public interface OneToManyMapper {
    //查询全部
    public abstract List selectAll();
}

测试类:

package Mybatis03.table02;

import Mybatis03.bean.Classes;
import Mybatis03.bean.Student;
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 org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectAll() throws Exception {
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        //2.获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过工厂对象获取SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //4.获取OneToManyMapper接口的实现类对象
        OneToManyMapper mapper = sqlSession.getMapper(OneToManyMapper.class);
        //5.调用实现类的方法,接收结果
        List classes = mapper.selectAll();
        //6.处理结果
        for(Classes cls : classes){
            System.out.println(cls.getId()+","+cls.getName());
            List students = cls.getStudents();
            for(Student student : students){
                System.out.println("\t"+student);
            }
        }
        //7.释放资源
        sqlSession.close();
        is.close();
    }
}

4.多对多操作

数据准备

多对多模型:学生和课程,一个学生可以选择多门课程、一个课程也可以被多个学生所选择

sql数据准备:

CREATE TABLE course(
  id INT PRIMARY KEY AUTO_INCREMENT,
  NAME VARCHAR(20)
);
INSERT INTO course VALUES (NULL,'语文');
INSERT INTO course VALUES (NULL,'数学');

CREATE TABLE stu_cr(
  id INT PRIMARY KEY AUTO_INCREMENT,
  sid INT,
  cid INT,
  CONSTRAINT sc_fk1 FOREIGN KEY (sid) REFERENCES student(id),
  CONSTRAINT sc_fk2 FOREIGN KEY (cid) REFERENCES course(id)
);
INSERT INTO stu_cr VALUES (NULL,1,1);
INSERT INTO stu_cr VALUES (NULL,1,2);
INSERT INTO stu_cr VALUES (NULL,2,1);
INSERT INTO stu_cr VALUES (NULL,2,2);

bean.Course:

package Mybatis03.bean;

public class Course {
    private Integer id;     //主键id
    private String name;    //课程名称

    public Course() {
    }

    public Course(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

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

修改bean.Student:

package Mybatis03.bean;


import java.util.List;

public class Student {
    private Integer id; //主键id
    private String name;//学生姓名
    private Integer age;//学生年龄
    private List courses;//学生所选择的课程集合

    public Student() {
    }

    public Student(Integer id, String name, Integer age, List courses) {
        this.id = id;
        this.name = name;
        this.age = age;
        this.courses = courses;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List getCourses() {
        return courses;
    }

    public void setCourses(List courses) {
        this.courses = courses;
    }

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

功能实现 

:配置字段和对象属性的映射关系标签。
    id 属性:唯一标识
    type 属性:实体对象类型
 :配置主键映射关系标签。
 :配置非主键映射关系标签。

    column 属性:表中字段名称
    property 属性: 实体对象变量名称
:配置被包含集合对象的映射关系标签。
    property 属性:被包含集合对象的变量名
    ofType 属性:集合中保存的对象数据类型

配置文件:





    
        
        
        
        
            
            
        
    
    

 配置映射文件:


        
        
        
        
    

接口:

package Mybatis03.table03;

import Mybatis03.bean.Student;

import java.util.List;

public interface ManyToManyMapper {
    //查询全部
    public abstract List selectAll();
}

测试类:

package Mybatis03.table03;

import Mybatis03.bean.Course;
import Mybatis03.bean.Student;
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 org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class Test01 {
    @Test
    public void selectAll() throws Exception {
        //1.加载核心配置文件
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
        //2.获取SqlSession工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //3.通过工厂对象获取SqlSession
        SqlSession sqlSession = sqlSessionFactory.openSession(true);
        //4.获取ManyToManyMapper接口的实现类对象
        ManyToManyMapper mapper = sqlSession.getMapper(ManyToManyMapper.class);
        //5.调用实现类的方法,接收结果
        List students = mapper.selectAll();
        //6.处理结果
        for (Student student : students) {
            System.out.println(student.getId()+","+student.getName()+","+student.getAge());
            List courses = student.getCourses();
            for(Course course : courses){
                System.out.println("\t"+course);
            }
        }
        //7.释放资源
        sqlSession.close();
        is.close();
    }
}

你可能感兴趣的:(Mybatis,mybatis)