mybatis(7)自定义结果集(一对多/多对一)

1.数据表

## 一对多数据表
## 创建班级表
create table t_clazz(
    `id` int primary key auto_increment,
    `name` varchar(50)
);

## 插入班级信息
insert into t_clazz(`name`) values('javaEE20170228');
insert into t_clazz(`name`) values('javaEE20170325');
insert into t_clazz(`name`) values('javaEE20170420');
insert into t_clazz(`name`) values('javaEE20170515');

## 创建学生表
create table t_student(
    `id` int primary key auto_increment,
    `name` varchar(50),
    `clazz_id` int,
    foreign key(`clazz_id`) references t_clazz(`id`)
);

## 插入班级信息
insert into t_student(`name`,`clazz_id`) values('stu0228_1',1);
insert into t_student(`name`,`clazz_id`) values('stu0228_2',1);
insert into t_student(`name`,`clazz_id`) values('stu0228_3',1);
insert into t_student(`name`,`clazz_id`) values('stu0325_1',2);
insert into t_student(`name`,`clazz_id`) values('stu0325_2',2);
insert into t_student(`name`,`clazz_id`) values('stu0420_1',3);

2.Student类和Clazz类

public class Student {
    private Integer id;
    private String name;
}
public class Clazz {
    private Integer id;
    private String name;
    private List<Student> stus;}

3.ClazzMapper接口

public interface ClazzMapper {
    /**
     * 根据班级id的信息,直接查询出班级信息,以及这个班的全部学生信息。
     */
    public Clazz queryClazzByIdForSample(Integer id);

}

4.ClazzMapper.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.cn.mapper.ClazzMapper">

    <resultMap type="com.cn.pojo.Clazz" id="queryClazzByIdForSample_resultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <!-- 
            collection 标签是专门用来配置集合属性的标签
                property属性设置你要配置哪个集合属性
                ofType 属性设置这个集合中每个元素的具体类型
         -->
        <collection property="stus" ofType="com.cn.pojo.Student">
            <id column="stu_id" property="id"/>
            <result column="stu_name" property="name"/>
        </collection>
    </resultMap>

<!--         /** -->
<!--      * 根据班级id的信息,直接查询出班级信息,以及这个班的全部学生信息。 -->
<!--      */ -->
<!--     public Clazz queryClazzByIdForSample(Integer id); -->
    <select id="queryClazzByIdForSample" resultMap="queryClazzByIdForSample_resultMap">
        select
            t_clazz.*,t_student.id stu_id,t_student.name stu_name
        from 
            t_clazz left join t_student
        on 
            t_clazz.id = t_student.clazz_id
        where 
            t_clazz.id = #{id}    
    </select>
    
</mapper>

5.一对多的懒加载

ClazzMapper接口

/**
     * 根据班级id查询班级信息(只查班级)
     */
    public Clazz queryClazzByIdForTwoStep(Integer id);

StudentMapper接口

public interface StudentMapper {
    /**
     * 根据班级编号查询本班所有学生信息
     */
    public List<Student> queryStudentsByClazzId(Integer clazzId);

}

StudentMapper.xml配置文件:

<mapper namespace="com.cn.mapper.StudentMapper">

<!--     /** -->
<!--      * 根据班级编号查询本班所有学生信息 -->
<!--      */ -->
<!--     public List<Student> queryStudentsByClazzId(Integer clazzId); -->
    <select id="queryStudentsByClazzId" resultType="com.cn.pojo.Student">
        select id,name from t_student where clazz_id = #{clazzId}
    </select>
    
</mapper>

ClazzMapper.xml配置文件:

<resultMap type="com.cn.pojo.Clazz" id="queryClazzByIdForTwoStep_resultMap">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <!-- 
            collection 标签是专门用来配置集合属性的(它可以通过调用一个select查询得到需要集合)。
                property属性设置你要配置哪个集合属性
                select 属性设置你要调用哪个查询
                column 将哪个列的值传递给查询做为参数
         -->
        <collection property="stus" column="id"
            select="com.cn.mapper.StudentMapper.queryStudentsByClazzId" />
    </resultMap>
    
<!--         /** -->
<!--      * 根据班级id查询班级信息(只查班级) -->
<!--      */ -->
<!--     public Clazz queryClazzByIdForTwoStep(Integer id); -->
    <select id="queryClazzByIdForTwoStep" resultMap="queryClazzByIdForTwoStep_resultMap">
        select id,name from t_clazz where id = #{id}
    </select>

 

原文地址:https://www.cnblogs.com/ywqtro/p/12254346.html