mybatis的学习6____处理多对一的情况

Posted xbfchder

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis的学习6____处理多对一的情况相关的知识,希望对你有一定的参考价值。

本案例为maven项目:

1.搭建数据库环境:

数据环境中是多个学生对应于一个老师:

CREATE TABLE `teacher` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8

INSERT INTO teacher(`id`, `name`) VALUES (1, 秦老师); 

CREATE TABLE `student` (
  `id` INT(10) NOT NULL,
  `name` VARCHAR(30) DEFAULT NULL,
  `tid` INT(10) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `fktid` (`tid`),
  CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8


INSERT INTO `student` (`id`, `name`, `tid`) VALUES (1, 武同学, 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (2, 李同学, 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (3, 武同学, 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (4, 姚同学, 1); 
INSERT INTO `student` (`id`, `name`, `tid`) VALUES (5, 夏同学, 1);

需求:要求获得所有学生的姓名,学号,对应的老师的姓名。(注意:涉及到两张表)

2.  student的pojo实体类:

public class Student 
    private int id;
    private String name;
    private int tid;
    private Teacher teacher;
       //注意多个学生对应一个老师

    public Student() 
    

    public Student(int id, String name, int tid, Teacher teacher) 
        this.id = id;
        this.name = name;
        this.tid = tid;
        this.teacher = teacher;
    

    public int getId() 
        return id;
    

    public void setId(int id) 
        this.id = id;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public int getTid() 
        return tid;
    

    public void setTid(int tid) 
        this.tid = tid;
    

    public Teacher getTeacher() 
        return teacher;
    

    public void setTeacher(Teacher teacher) 
        this.teacher = teacher;
    

    @Override
    public String toString() 
        return "Student" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", tid=" + tid +
                ", teacher=" + teacher +
                ‘‘;
    

3.Dao层接口的编写StudentMapper:

public interface StudentDao 
//方法1;
//获取所有学生 //是以数据库的方式,进行连表查询 List<Student> getStudents();
//方法二:
//获取所有学生 //是以面向对象的方式,进行查询 List<Student> getStudents2();

4.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">

<mapper namespace="com.xbf.dao.StudentDao">
    <select id="getStudents" resultMap="StudentTeacher">
        select * from student
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <association column="tid" property="teacher" javaType="Teacher" select="getTeacherName"/>
    </resultMap>

    <select id="getTeacherName" resultType="Teacher">
        select name from teacher where id=#id
    </select>


    <!--cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc-->
    <select id="getStudents2" resultMap="StudentTeacher2">
        select s.id,s.name,t.name as tname from student s,teacher t where s.tid=t.id
    </select>

    <resultMap id="StudentTeacher2" type="Student">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <association property="teacher" javaType="Teacher">
            <result property="name" column="tname"></result>
        </association>
    </resultMap>



</mapper>

5.mybatis-config.xml核心文件的编写:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>

    <properties resource="database.properties"/>
    
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>

    <typeAliases>
        <package name="com.xbf.pojo"></package>
    </typeAliases>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="POOLED">
                <property name="driver" value="$driver"/>
                <property name="url" value="$url"/>
                <property name="username" value="$username"/>
                <property name="password" value="$password"/>

            </dataSource>
        </environment>
    </environments>


    <mappers>
       <mapper resource="com/xbf/dao/teacherMapper.xml"></mapper>
    </mappers>

</configuration>

6.测试类的编写:

public class StudentDaoTest 

    @Test
    public void getStudents()
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        List<Student> students = mapper.getStudents();
        for (Student student : students) 
            System.out.println(student.getName()+student.getTeacher().getName());
        
    

    @Test
    public void getStudents2()
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentDao mapper = sqlSession.getMapper(StudentDao.class);
        List<Student> students = mapper.getStudents2();
        for (Student student : students) 
            System.out.println(student.getName()+student.getTeacher().getName());
        
    

总结:

mybatis中多对一的情况得在StudentMapper.xml(接口对应的配置文件中)使用assosiation标签,共有两种解决问题的方式:

方法1:使用数据库的连表查询;

方法2:使用面向对象的对象关联。

以上是关于mybatis的学习6____处理多对一的情况的主要内容,如果未能解决你的问题,请参考以下文章

阶段3 1.Mybatis_12.Mybatis注解开发_6 mybatis注解开发一对一的查询配置

MyBatis-多对一查询

Mybatis_多对一关联查询

MyBatis学习05

07_关于联表的处理

Hibernate5-多对一双向关联-fetch="select",lazy="proxy",在一的一方的class标签中添加