Mybatis对复杂属性的处理

本文介绍了一种多对一的数据处理方式,在MyBatis框架下实现学生与老师间的关系映射。主要内容包括:定义学生实体类及其与老师之间的关联;创建DAO层接口及XML配置文件,实现学生信息查询并关联其所属老师的详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

多对一处理

第一:编写一个学生的实体类

package com.zzy.pojo;

import lombok.Data;
import org.apache.ibatis.annotations.Delete;

@Data
public class Student {
    private int id;
    private String name;
    private Teacher teacher;
}

注意:在student实体类中老师的属性是一个javabean对象所以在student类中老师属于复杂属性

第二:编写一个student的dao层

package com.zzy.dao;

import com.zzy.pojo.Student;
import com.zzy.pojo.Teacher;

import java.util.List;

public interface StudentMapper {

    //查询学生信息通过id
    Student getStudent(int id);
    //查询所有的学生
    List<Student> getStudents();
    //查询老师
    Teacher getTeacher(int id);
    List<Student> getStudents1();
}

第三:编写一个学生的dao层对应的studentMapper.xml
注意:result property=“id” column=“id”
property对应你的实体类的属性 column对应你的数据库子段

<?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.zzy.dao.StudentMapper">
    <select id="getStudent" resultType="Student">
        select * from student where id=#{id}
    </select>

<!--    1,查询所有的学生信息  2根据查询的学生信息的tid来查询老师的信息-->
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--        复杂的属性 必须单独处理 association :对象的处理   collection:对集合的处理-->
        <association property="teacher" column="tid" javaType="Student" select="getTeacher"/>
    </resultMap>

    <select id="getStudents" resultMap="StudentTeacher">
       select  *from student
    </select>

    <select id="getTeacher" resultType="Teacher">
        select *from teacher where id=#{id}
    </select>

<!-- ##########################按照结果集查询###################################   -->
    
    <resultMap id="StudentTeacher1" 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="getStudents1" resultMap="StudentTeacher1">
        select  s.id sid,s.name sname ,t.name tname from student s ,teacher t  where s.tid=t.id;
    </select>
</mapper>

注意:因为老师再student中是一个对象所以要特殊处理在resultmap中进行处理

<!--    1,查询所有的学生信息  2根据查询的学生信息的tid来查询老师的信息-->
    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--        复杂的属性 必须单独处理 association :对象的处理   collection:对集合的处理-->
        <association property="teacher" column="tid" javaType="Student" select="getTeacher"/>
    </resultMap>

注意:在注入sql语句时他的属性是resultmap=id(是resultmap的id)

  <select id="getStudents" resultMap="StudentTeacher">
       select  *from student
    </select>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值