• mybatis多对一数据库查询


    StudentMapper.java

    package dao;
    
    import pojo.Student;
    
    import java.util.List;
    
    public interface StudentMapper {
    
        //查询所有学生信息
        public List<Student> getStudent1();
    
        public List<Student> getStudent2();
    }

    StudentMapper.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!--namespace=绑定一个对应的Dao/Mapper接口-->
    <mapper namespace="dao.StudentMapper">
    
        
        <!--第一种方法-->
        <resultMap id="ST1" type="Student">
            <result property="id" column="id"/>
            <result property="name" column="name"/>
            <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
        </resultMap>
    
        <select id="getStudent1" resultMap="ST1">
            select * from student
        </select>
        <select id="getTeacher" resultType="Teacher">
            select * from  teacher where id = #{id}
        </select>
    
    
        <!--===============================-->
        
        <!--第二种方法-->
        <resultMap id="ST2" type="Student">
            <result property="id" column="sid"/>
            <result property="name" column="sname"/>
            <association property="teacher" javaType="Teacher">
                <result property="name" column="tname"/>
            </association>
    
        </resultMap>
        <select id="getStudent2" resultMap="ST2">
            select s.id sid,s.name sname,t.name tname from student s,teacher t where s.tid=t.id
        </select>
    
    
    
    </mapper>

    Student.java

    package pojo;
    
    import lombok.Data;
    
    @Data
    public class Student {
        int id;
        String name;
        Teacher teacher;
    }

    Teacher.java

    package pojo;
    
    
    import lombok.Data;
    
    @Data
    public class Teacher {
        int id;
        String name;
    }

    MyTest.java

    package dao;
    
    import org.apache.ibatis.session.SqlSession;
    import org.junit.Test;
    import pojo.Student;
    import pojo.Teacher;
    import utils.MybatisUtils;
    
    import java.util.List;
    
    public class MyTest {
        @Test
        public void test()
        {
            SqlSession sqlSession= MybatisUtils.getSqlSession();
            TeacherMapper teacherMapper=sqlSession.getMapper(TeacherMapper.class);
            Teacher teacher = teacherMapper.getTeacher(1);
            System.out.println(teacher.getName());
            sqlSession.close();
        }
        @Test
        public void getStudent1()
        {
            SqlSession sqlSession=MybatisUtils.getSqlSession();
            StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
            List<Student> studentList=studentMapper.getStudent1();
            for (Student student : studentList) {
                System.out.println(student);
            }
        }
        @Test
        public void getStudent2()
        {
            SqlSession sqlSession=MybatisUtils.getSqlSession();
            StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class);
            List<Student> studentList=studentMapper.getStudent2();
            for (Student student : studentList) {
                System.out.println(student);
            }
        }
    }

    每添加一个接口类都要在核心配置文件mybatis-config.xml中绑定接口,最好使用typeAliases简化代码

    总体感觉来说还是第二中多对一比较好理解

  • 相关阅读:
    数据库访问性能优化--应用开发
    苹果终端wifi图标点亮慢和portal弹窗机制分析以及处理办法和建议
    蓝牙固件升级(OTA升级)原理设计
    C语言:内存字节对齐详解
    linux du与ls查看文件大小时的区别
    80211 组播速率及组播转单播
    视频帧、码流计算
    IEEE802.11数据帧在Linux上的抓取 80211格式转8023帧格式
    wifidog源码分析
    无线路由:关于WDS,Repeater等模式的说明
  • 原文地址:https://www.cnblogs.com/yizhixiaozhu/p/14678528.html
Copyright © 2020-2023  润新知