• 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简化代码

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

  • 相关阅读:
    前端js(一)
    前端CSS
    前端HTML
    视图等
    Navicat使用
    查询语句
    SpringBoot不能直接访问templates下的静态资源
    Mybatis什么时候用${}
    thymeleaf使用restul风格URL
    SpringBoot使用PageHelper
  • 原文地址:https://www.cnblogs.com/yizhixiaozhu/p/14678528.html
Copyright © 2020-2023  润新知