MyBatis 一对多关系映射(双向)

年级表,地址表,学生表,通过年级id找对应该年级的学生;通过地址id找对应该地址的学生。这都是典型的一对多的关系。其实反向的还就是正向的一对一。

在这里我们只演示一下通过查找年级,找出对应的学生。

首相我们先构造实体类:

package com.maya.model;

import java.util.List;

public class Gread {
    private int id;
    private String greadName;
    private List<Student> students;//一个年级对应多个学生
public class Student {
    private int id;
    private String name;
    private int age;
    private Address address;//一对一
    private Gread gread;//同上
    

2.对应的接口

package com.maya.mappers;
import com.maya.model.Gread;

public interface GreadMapper {
    public Gread findById(int id);//
}
package com.maya.mappers;
import java.util.List;
import java.util.Map;
import com.maya.model.Student;

public interface StudentMapper {
    //添加
    public int add(Student student);
    //修改
    public int update(Student student);
    //删除
    public int delete(int id);
    //查询全部
    public List<Student> findAll();
    //查询单个
    public Student getOneById(int id);
    //根据条件查询
    public List<Student> findWhere(Map<String, Object>param);
    
    //根据年级id查找对应学生
    public List<Student> findByGreadId(int greadId);
}

3.映射文件

<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.maya.mappers.StudentMapper">

    <resultMap type="Student" id="StudentResult">
        <id property="id" column="id"></id>
        <result property="name" column="name" />
        <result property="age" column="age" />
<association property="address" column="addressId" select="com.maya.mappers.AddressMapper.getById"></association> <!-- property是属性名,column是外键列,select是对应的根据外键id寻找其表中数据的方法 --> <association property="gread" column="greadId" select="com.maya.mappers.GreadMapper.findById"></association>
</resultMap> <select id="findByGreadId" parameterType="int" resultMap="StudentResult"> select * from student where greadId=#{greadId} </select> </mapper>
<?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.maya.mappers.GreadMapper">
    <resultMap type="Gread" id="GreadResult">
        <id property="id" column="id" />
        <id property="greadName" column="name" />
        
        <!-- 通过年级的主键去学生表中查找对应的学生,典型的一对多。这里就要用collection标签 -->
        <collection property="students" column="id" select="com.maya.mappers.StudentMapper.findByGreadId"></collection>
        <!-- property同样是实体类中的属性名 -->
        <!--column
            注意: 
                1.一对一时,对应的column是自己的外键
                2.这里是一对多的关系,所以在这里对应的column是自己的id主键 
        -->
        <!-- select因为这里是通过自己的id去学生表中查找对应的信息,所以写StudentMapper中的根据greadId找学生的方法 -->
    </resultMap>

    <select id="findById" parameterType="int" resultMap="GreadResult" >
        select * from gread where id=#{id}
    </select>
</mapper>

junit测试类:

package com.maya.service;

import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.maya.mappers.GreadMapper;
import com.maya.mappers.StudentMapper;
import com.maya.model.Gread;
import com.maya.model.Student;
import com.maya.util.SqlSessionFactoryUtil;

public class OneToManyTest {
    private static Logger logger=Logger.getLogger(OneToManyTest.class);
    private SqlSession session=null;
    private GreadMapper greadMapper=null;
    private StudentMapper studentMapper=null;

    @Before
    public void setUp() throws Exception {
        session=SqlSessionFactoryUtil.openSession();
        greadMapper=session.getMapper(GreadMapper.class);
        studentMapper=session.getMapper(StudentMapper.class);
    }

    @After
    public void tearDown() throws Exception {
        session.close();
    }

    @Test
    public void testOneToMany() {
        logger.info("通过年级id查询该年级的所有学生,一对多");
        Gread gread=greadMapper.findById(4);
        System.out.println(gread);
    }
    
    @Test
    public void testOneToManyFanXiang() {
        logger.info("通过学生id查询所有所在年级和地址,反向一对多,和正向的一对一相同");
        Student student=studentMapper.getOneById(6);
        System.out.println(student);
    }

}
原文地址:https://www.cnblogs.com/AnswerTheQuestion/p/6739810.html