Mybatis高级映射多对一映射映射关系解析-----Mybatis框架

多对一映射(主表和附表)
Mapping映射到JVM中得到两个对象
多个的我们称为主表(一的那个作为多个的属性property存在)
谁是主表,多对一,多是主表,一对多,一在前,一是主表
如果主表是t_student,那么JVM的主对象就是Student对象
内存结构中就会据此生成student的成员变量clazz对象(通过这个引用就找到了这个对象)
反过来如果是研究一对多,那么就是clazz类的对象实例中有一个List<Student>,通过student的cid进行关联
多对一映射(主表和附表)
Mapping映射到JVM中得到两个对象
多个的我们称为主表(一的那个作为多个的属性property存在)
谁是主表,多对一,多是主表,一对多,一在前,一是主表
如果主表是t_student,那么JVM的主对象就是Student对象
内存结构中就会据此生成student的成员变量clazz对象(通过这个引用就找到了这个对象)
反过来如果是研究一对多,那么就是clazz类的对象实例中有一个List<Student>,通过student的cid进行关联
<?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.powernode.mybatis.mappers.StudentMapper">
    <resultMap id="studentResultMap" type="student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <result property="clazz.cid" column="cid"></result>
        <result property="clazz.cname" column="cname"></result>
    </resultMap>
    <select id="selectById" resultMap="studentResultMap">
        select
            s.sid,s.sname,c.cid,c.cname
        from
            t_stu as s left join t_clazz as c on s.cid = c.cid
        where
            s.sid = #{sid};
    </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.powernode.mybatis.mappers.StudentMapper">
    <resultMap id="studentResultMap" type="student">
        <id property="sid" column="sid"></id>
        <result property="sname" column="sname"></result>
        <result property="clazz.cid" column="cid"></result>
        <result property="clazz.cname" column="cname"></result>
    </resultMap>
    <select id="selectById" resultMap="studentResultMap">
        select
            s.sid,s.sname,c.cid,c.cname
        from
            t_stu as s left join t_clazz as c on s.cid = c.cid
        where
            s.sid = #{sid};
    </select>
</mapper>
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Student;

public interface StudentMapper
{
    //根据ID获取学生信息,获取学生关联的ID信息
    Student selectById(Integer id);
}
package com.powernode.mybatis.mappers;

import com.powernode.mybatis.pojo.Student;

public interface StudentMapper
{
    //根据ID获取学生信息,获取学生关联的ID信息
    Student selectById(Integer id);
}

package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.StudentMapper;
import com.powernode.mybatis.pojo.Student;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StudentMapperTest
{
    private static final Logger logger = LoggerFactory.getLogger(StudentMapperTest.class);
    @Test
    public void selectById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(1);
        logger.info(student.toString());
        SqlSessionUtils.close(sqlSession);
    }
}
package com.powernode.mybatis.Test;

import com.powernode.mybatis.mappers.StudentMapper;
import com.powernode.mybatis.pojo.Student;
import com.powernode.mybatis.utils.SqlSessionUtils;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StudentMapperTest
{
    private static final Logger logger = LoggerFactory.getLogger(StudentMapperTest.class);
    @Test
    public void selectById()
    {
        SqlSession sqlSession = SqlSessionUtils.openSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Student student = mapper.selectById(1);
        logger.info(student.toString());
        SqlSessionUtils.close(sqlSession);
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值