MyBatis:多对一的处理(狂神)

MyBatis:多对一的处理(狂神)_第1张图片

MyBatis:多对一的处理(狂神)_第2张图片

MyBatis:多对一的处理(狂神)_第3张图片

MyBatis:多对一的处理(狂神)_第4张图片

MyBatis:多对一的处理(狂神)_第5张图片

MyBatis:多对一的处理(狂神)_第6张图片

MyBatis:多对一的处理(狂神)_第7张图片

MyBatis:多对一的处理(狂神)_第8张图片

do.properties:

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

 mybatis-config.xml:







    
        
        
        
    



    
        
        
        

    


    

    
        
        
    

    
        
            
            
            
                
                
                
                
            
        
    


    
        
        
    



 Student:使用Lombok

package com.kuang.pojo;

import lombok.Data;

@Data
public class Student {
    private int id;
    private String name;
    //学生需要关联一个老师
    private Teacher teacher;
}

Teacher:

package com.kuang.pojo;

import lombok.Data;

@Data
public class Teacher {
    private int id;
    private String name;
}

 TeacherMapper:使用注解

package com.kuang.dao;

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

public interface TeacherMapper {

    @Select("select * from teacher where id=#{tid}")
    Teacher getTeacher(@Param("tid") int id);
}

StudentMapper:

package com.kuang.dao;

import com.kuang.pojo.Student;

import java.util.List;

public interface StudentMapper {
    //查询所有的学生信息,以及所有的老师的信息
    public List getStudent();

    public List getStudent2();
}

StudentMapper.xml:




    
    
    
    
    
        
        
        
        
    

    
    




    
    
        
        
        
        
            
            
        
    

MyTest:

import com.kuang.dao.StudentMapper;
import com.kuang.dao.TeacherMapper;
import com.kuang.pojo.Student;
import com.kuang.pojo.Teacher;
import com.kuang.utils.MybaitsUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.util.List;

public class MyTest {
    public static void main(String[] args) {
        SqlSession sqlSession= MybaitsUtils.getSqlSession();
        TeacherMapper mapper=sqlSession.getMapper(TeacherMapper.class);

        Teacher teacher = mapper.getTeacher(1);
        System.out.println(teacher);
        sqlSession.close();
    }
    //方法一 查询嵌套
    @Test
    public void testStudent(){
        SqlSession sqlSession= MybaitsUtils.getSqlSession();
        StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);

        List studentList = mapper.getStudent();

        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();

    }
    //方法二 按照结果集查询
    @Test
    public void testStudent2(){
        SqlSession sqlSession= MybaitsUtils.getSqlSession();
        StudentMapper mapper=sqlSession.getMapper(StudentMapper.class);

        List studentList = mapper.getStudent2();

        for (Student student : studentList) {
            System.out.println(student);
        }
        sqlSession.close();

    }
}

MyBatis:多对一的处理(狂神)_第9张图片

 方法一MyBatis:多对一的处理(狂神)_第10张图片

方法二 

MyBatis:多对一的处理(狂神)_第11张图片

 

MyBatis:多对一的处理(狂神)_第12张图片

MyBatis:多对一的处理(狂神)_第13张图片

你可能感兴趣的:(MyBatis框架学习,java,mysql,MyBatis)