Mybatis_resultMap_级联、分布、延迟加载

一、环境准备
1、表结构
stu表
这里写图片描述
class表
这里写图片描述

2、表数据
stu表
这里写图片描述
class表
这里写图片描述

*3、对象类
Student*

package com.taozi.mybatis.bean;

public class Student {

    private int id;
    private String name;
    private char Sex;
    private Classes banji;;


    public Student() {
//      super();
        // TODO Auto-generated constructor stub
    }
    public Student(int id, String name, char sex) {
        super();
        this.id = id;
        this.name = name;
        Sex = sex;
    }
    public char getSex() {
        return Sex;
    }
    public void setSex(char gender) {
        this.Sex = gender;
    }
    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 Classes getBanji() {
        return banji;
    }
    public void setBanji(Classes banji) {
        this.banji = banji;
    }
    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", Sex=" + Sex + ", banji=" + banji + "]";
    }

}

Classes类因最开始没注意到类名,用了Class,后来为了方便就直接改了个名字,此处只为演示下列的查询()

package com.taozi.mybatis.bean;

public class Classes {

    private  int id;
    private String desc;

    public Classes() {
        // TODO Auto-generated constructor stub
    }

    public int getId() {
        return id;
    }

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

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    @Override
    public String toString() {
        return "Classes [id=" + id + ", desc=" + desc + "]";
    }

}

二、对象查询

查出某个学生的信息时,同时查询班级信息

1、简单级联查询

接口类中新增方法

public Student getStuClassById(int id);

sql映射

<resultMap type="com.taozi.mybatis.bean.Student" id="myStuMap_1">
        <id  column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="class_id" property="banji.id"/>
        <result column="des" property="banji.desc"/>
</resultMap>

 <select id="getStuClassById" resultMap="myStuMap_1">
    select a.id id,a.name,a.class_id,b.id class,b.description des from stu a,class b
    where a.class_id = b.id and a.id=#{id}
 </select>

将resultMap中,学生信息中的班级id映射到班级信息的主键id字段,属性为学生类对象的班级属性的属性值。
测试方法

2、association关联查询
修改sql映射文件

<resultMap type="com.taozi.mybatis.bean.Student" id="myStuMap_2">
    <id  column="id" property="id"/>
    <result column="name" property="name"/>
    <association property="banji" javaType="com.taozi.mybatis.bean.Classes">
        <id column="class_id" property="id"/>
        <result column="des" property="desc"/>
    </association>
 </resultMap> 
 <select id="getStuClassById" resultMap="myStuMap_2">
    select a.id id,a.name,a.class_id,b.id class,b.description des
    from stu a,class b
    where a.class_id = b.id and a.id=#{id}
 </select>

将学生中的班级属性使用association封装,内部的映射和普通映射类似
3、association分布查询
修改sql映射文件

<!-- 分步查询
 <association property:"banji"
  select:班级查询语句 所在包。select_id
  column:传递给select语句的参数值></association> 
  --> 
   <resultMap type="com.taozi.mybatis.bean.Student" id="myStuMap_3">
    <id  column="id" property="id"/>
    <result column="name" property="name"/>
    <!--  <result column="class_id" property="class_id"/>-->
    <association property="banji" 
    select="com.taozi.mybatis.test.ClassMapper.getClasById"       column="class_id">    
     </association>
 </resultMap>

<select id="getStuByIdStep" resultMap="myStuMap_3" databaseId="mysql">
    select * from stu where id =#{id}
 </select>

在查询学生信息时,嵌套班级查询

接口类中新增分步查询方法

public Student getStuByIdStep(int id);

测试方法

Student stu = mapper.getStuByIdStep(3);
System.out.println(stu);
System.out.println(stu.getBanji());

结果如下:
这里写图片描述
因sql查询上未映射sex字段,故该字段为空

三、集合查询
查询班级信息时,将所对应的学生信息一并查出

1、级联查询
班级接口新增学生集合属性

private List<Student> student;

为班级新增查询方法

public List<Student> getClassStuById(int id);

sql映射

 <!-- public List<Student> getClassStuById(int id); -->
 <resultMap type="com.taozi.mybatis.bean.Classes" id="myGetClassStu">
    <id column="id" property="id"/>
    <result column="des" property="desc"/>
    <collection  property="student" ofType="com.taozi.mybatis.bean.Student">
        <id column="stu_id" property="id"/>
        <result column="name" property="name"/>
    </collection>
 </resultMap>

 <select id="getClassStuById" resultMap="myGetClassStu">

        select a.id id,a.description des,
        b.id stu_id,b.name name 
        from class a left join stu b 
        on a.id = b.class_id 
        where a.id=#{id}
 </select>

测试方法

    @Test
    public void test02() throws IOException
    {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                  new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();

        ClassMapper mapper = session.getMapper(ClassMapper.class);
        Classes banji = mapper.getClassStuById(1);
        System.out.println(banji);
        System.out.println(banji.getStudent());
    }

测试结果
这里写图片描述

2、分步查询
Student新增查询方法(根据班级id查询)

public List<Student> getStuByClass(int id);

sql映射

<!-- public List<Student> getStuByClass(int id); -->
 <select id="getStuByClass" resultType="com.taozi.mybatis.bean.Student">
  select * from stu where class_id=#{id}
 </select>

班级类新增查询方法

public Classes getClassStuByIdStep(int id);

sql映射

  <!-- public Classes getClassStuByIdStep(int id); -->
  <resultMap type="com.taozi.mybatis.bean.Classes" id="myGetClassStuStep">
    <id column="id" property="id"/>
    <result column="des" property="desc"/>
  <collection property="student"     select="com.taozi.mybatis.test.Mybatis_mapper_04.getStuByClass"
     column="id">
  </collection>
  </resultMap>
       <select id="getClassStuByIdStep" resultMap="myGetClassStuStep">
        select id,description des from class where id=#{id}
 </select>

测试方法

ClassMapper mapper = session.getMapper(ClassMapper.class);
Classes banji = mapper.getClassStuByIdStep(1);
System.out.println(banji);

测试结果
这里写图片描述

延迟加载
开启全局设置中的lazyLoadingEnabled属性及aggressiveLazyLoading属性

Mybatis级联查询可以通过在Mapper文件中使用嵌套查询的方式实现,具体步骤如下: 1. 在POJO类中定义关联属性(如一对多、多对多等),并提供对应的setter和getter方法。 2. 在Mapper文件中定义对应的嵌套查询语句,使用关联属性的getter方法来获取关联对象的数据,例如: ```xml <select id="findUserById" parameterType="int" resultMap="userResultMap"> select * from user where id = #{id} </select> <resultMap id="userResultMap" type="User"> <id column="id" property="id"/> <result column="username" property="username"/> <result column="age" property="age"/> <association property="department" javaType="Department"> <id column="dept_id" property="id"/> <result column="dept_name" property="name"/> </association> </resultMap> <select id="findUserWithDeptById" parameterType="int" resultMap="userResultMap"> select u.*, d.dept_name from user u left join department d on u.dept_id = d.id where u.id = #{id} </select> ``` 在上述代码中,`findUserById`只查询User表中的数据,而`findUserWithDeptById`则查询User表和Department表中的数据,并将Department作为User对象的关联属性返回。 3. 在业务层中调用Mapper接口的方法,即可进行级联查询,例如: ```java User user = userMapper.findUserWithDeptById(1); System.out.println(user.getDepartment().getName()); ``` 在上述代码中,`userMapper.findUserWithDeptById`方法会返回一个User对象,其中的Department属性已经被赋值为关联的Department对象,通过getDepartment()方法即可获取Department对象的数据。 需要注意的是,在进行级联查询时,需要定义好关联属性的类型和对应的嵌套查询语句,否则会导致查询失败或数据不完整。同时,级联查询也会增加数据库的查询开销,应该根据实际情况进行使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值