关联查询

关联查询:

一对一:

a.业务扩展类

核心:用resultType指定类的属性包含多表查询的所有字段

/MyBatisProject3/src/org/myy/mapper/studentMapper.xml

    

 

/MyBatisProject3/src/org/myy/mapper/StudentMapper.java

    StudentBusiness queryStudentByNoWithOO(int stuno);

 

/MyBatisProject3/src/org/myy/test/Test.java

// Connection - SqlSession操作Mybatis
        // conf.xml->reader
        Reader reader = Resources.getResourceAsReader("conf.xml");
        // reader->sqlSession
        // 可以通过build的第二参数 指定数据库环境
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development");
        SqlSession session = sessionFactory.openSession();

        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        
        StudentBusiness studentBusiness=studentMapper.queryStudentByNoWithOO(1);
        System.out.println(studentBusiness);
        session.close();

/MyBatisProject3/src/org/myy/entity/StudentBusiness.java

package org.myy.entity;

public class StudentBusiness extends Student{//学生业务扩展类
    private int cardId;
    private String cardInfo;
    public int getCardId() {
        return cardId;
    }
    public void setCardId(int cardId) {
        this.cardId = cardId;
    }
    public String getCardInfo() {
        return cardInfo;
    }
    public void setCardInfo(String cardInfo) {
        this.cardInfo = cardInfo;
    }
    @Override
    public String toString() {
        return super.toString()+","+this.cardId+","+this.cardInfo;
    }
}

b.resultMap

  i.通过属性成员将2个类建立起联系

/MyBatisProject3/src/org/myy/entity/Student.java

package org.myy.entity;


//学生类包含  1.学生信息 2.学生证信息
public class Student {
    //学生信息
    private int stuNo;
    private String stuName;
    private int stuAge;
    private String graName;
    private Boolean stuSex;
    //学生证信息
    private StudentCard card;
    
    public StudentCard getCard() {
        return card;
    }
    public void setCard(StudentCard card) {
        this.card = card;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    private Address address;//家庭、学校
    
    public int getStuNo() {
        return stuNo;
    }
    public void setStuNo(int stuNo) {
        this.stuNo = stuNo;
    }
    public Boolean getStuSex() {
        return stuSex;
    }
    public void setStuSex(Boolean stuSex) {
        this.stuSex = stuSex;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public int getStuAge() {
        return stuAge;
    }
    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }
    public String getGraName() {
        return graName;
    }
    public void setGraName(String graName) {
        this.graName = graName;
    }
    public Student() {
        super();
    }
    
    public Student(int stuNo, String stuName, int stuAge, String graName) {
        super();
        this.stuNo = stuNo;
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.graName = graName;
    }
    public Student(int stuNo, String stuName, int stuAge, String graName, Boolean stuSex) {
        super();
        this.stuNo = stuNo;
        this.stuName = stuName;
        this.stuAge = stuAge;
        this.graName = graName;
        this.stuSex = stuSex;
    }
    @Override
    public String toString() {
        return this.stuNo+"-"+this.stuName+"-"+this.stuAge+"-"+this.graName+"-性别:"+this.stuSex
                +","+this.card.getCardId()+"-"+this.card.getCardInfo();
    }
}

  ii.

/MyBatisProject3/src/org/myy/mapper/studentMapper.xml


    
    
        
        
        
        
        
            jdbcType="INTEGER" />
        
        
            
            
        
    

 

/MyBatisProject3/src/org/myy/mapper/StudentMapper.java

Student queryStudentByNoWithOO2(int stuno);

 

/MyBatisProject3/src/org/myy/test/Test.java

// Connection - SqlSession操作Mybatis
        // conf.xml->reader
        Reader reader = Resources.getResourceAsReader("conf.xml");
        // reader->sqlSession
        // 可以通过build的第二参数 指定数据库环境
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development");
        SqlSession session = sessionFactory.openSession();

        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        
        Student student = studentMapper.queryStudentByNoWithOO2(2);
        System.out.println(student);
        session.close();

 

一对一:association

一对多:collection

一对多(多对一,多对多的本质就是一对多的变化)

表:student studentclass

类:Student StudentClass

/MyBatisProject3/src/org/myy/entity/StudentClass.java

package org.myy.entity;

import java.util.List;

public class StudentClass {
    private int classId;
    private String className;
    //增加学生属性(通过该字段让Student类和StudentClass建立起关联)
    List students;
    public List getStudents() {
        return students;
    }
    public void setStudents(List students) {
        this.students = students;
    }
    public int getClassId() {
        return classId;
    }
    public void setClassId(int classId) {
        this.classId = classId;
    }
    public String getClassName() {
        return className;
    }
    public void setClassName(String className) {
        this.className = className;
    }
}

/MyBatisProject3/src/org/myy/mapper/studentMapper.xml


    
    
    
        
        
        
        
        
            
            
            
        
    

 

/MyBatisProject3/src/org/myy/mapper/StudentMapper.java

StudentClass queryClassAndStudents(int classId);

 

/MyBatisProject3/src/org/myy/test/Test.java

     // Connection - SqlSession操作Mybatis
        // conf.xml->reader
        Reader reader = Resources.getResourceAsReader("conf.xml");
        // reader->sqlSession
        // 可以通过build的第二参数 指定数据库环境
        SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader, "development");
        SqlSession session = sessionFactory.openSession();

        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        
        //班级
        StudentClass studentClass = studentMapper.queryClassAndStudents(1);
        System.out.println(studentClass.getClassId()+","+studentClass.getClassName());
        //班级对应的学生
        List students=studentClass.getStudents();
        for(Student student:students) {
            System.out.println(student.getStuNo()+","+student.getStuName()+","+student.getStuAge());
        }
        session.close();

 

你可能感兴趣的:(关联查询)