栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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:




    
    
    
    
        select * from teacher where id=#{tid}
    




    
    
        
        
        
        
            
            
        
    

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();

    }
}

 方法一

方法二 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/962009.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号