Mybatis多对一关系映射处理

Mybatis多对一关系映射处理三种方式:

数据库表:
t_class
在这里插入图片描述
t_student
在这里插入图片描述
实体类:
Student.java

public class Student {
    private Integer sid;
    private String name;
    private Cla cla;

    public Student() {
    }

    public Student(Integer sid, String name) {
        this.sid = sid;
        this.name = name;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Cla getCla() {
        return cla;
    }

    public void setCla(Cla cla) {
        this.cla = cla;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", name='" + name + '\'' +
                ", cla=" + cla +
                '}';
    }
}

Cla.java

package com.example.mybatis;


public class Cla {
    private Integer cid;
    private String cname;

    public Cla() {
    }

    public Cla(Integer cid, String cname) {
        this.cid = cid;
        this.cname = cname;
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getCname() {
        return cname;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    @Override
    public String toString() {
        return "Cla{" +
                "cid=" + cid +
                ", cname=" + cname +
                '}';
    }
}

一、级联方式处理
StudentoneMapper.java

package com.example.mybatis;

import org.apache.ibatis.annotations.Param;


public interface StudentoneMapper {
    
    Student queryfind(@Param("sid") Integer sid);
    }

StudentoneMapper.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.example.mybatis.StudentoneMapper">
 <resultMap id="qu" type="com.example.mybatis.Student">
        <id property="sid" column="sid"></id>
        <result property="name" column="name"></result>
        <result property="cla.cid" column="cid"></result>
        <result property="cla.cname" column="cname"></result>
    </resultMap>
    <select id="queryfind" resultMap="qu">
        select student.*,cla.* from t_student student left join t_class cla on student.cid = cla.cid where student.sid = #{sid}
    </select>
</mapper>

test.java

    @Test
    public void query() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSession sqlSession = sqlSessionFactoryBuilder.build(is).openSession(true);
        StudentoneMapper mapper = sqlSession.getMapper(StudentoneMapper.class);
        Student queryfind = mapper.queryfind(1);
        System.out.println(queryfind);
    }

在这里插入图片描述
二、association处理
StudentoneMapper.java

package com.example.mybatis;

import org.apache.ibatis.annotations.Param;


public interface StudentoneMapper {
    
    Student queryfind(@Param("sid") Integer sid);
    }

StudentoneMapper.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.example.mybatis.StudentoneMapper">
 <resultMap id="qu" type="com.example.mybatis.Student">
        <id property="sid" column="sid"></id>
        <result property="name" column="name"></result>
               <association property="cla" javaType="com.example.mybatis.Cla">
            <id property="cid" column="cid"></id>
            <result property="cname" column="cname"></result>
        </association>
    </resultMap>
    <select id="queryfind" resultMap="qu">
        select student.*,cla.* from t_student student left join t_class cla on student.cid = cla.cid where student.sid = #{sid}
    </select>
</mapper>

test.java

    @Test
    public void query() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSession sqlSession = sqlSessionFactoryBuilder.build(is).openSession(true);
        StudentoneMapper mapper = sqlSession.getMapper(StudentoneMapper.class);
        Student queryfind = mapper.queryfind(1);
        System.out.println(queryfind);
    }

在这里插入图片描述
三、分步查询
StudentoneMapper.java

package com.example.mybatis;

import org.apache.ibatis.annotations.Param;


public interface StudentoneMapper {
    
    Student queryone(@Param("sid") Integer sid);
    }

StudentoneMapper.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.example.mybatis.StudentoneMapper">
    <resultMap id="student" type="com.example.mybatis.Student">
        <id property="sid" column="sid"></id>
        <result property="name" column="name"></result>
        <association property="cla" select="com.example.mybatis.ClatwoMapper.querytwo"
                     column="cid"></association>
    </resultMap>
    <select id="queryone" resultMap="student">
        select * from t_student where sid = #{sid}
    </select>
    </mapper>

ClatwoMapper.java

package com.example.mybatis;

import org.apache.ibatis.annotations.Param;

public interface ClatwoMapper {
    Cla querytwo(@Param("cid") Integer cid);
    }

ClatwoMapper.xml

<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mybatis.ClatwoMapper">
    <select id="querytwo" resultType="com.example.mybatis.Cla">
        select * from t_class where cid = #{cid}
    </select>
    </mapper>

test.java

    @Test
    public void mapquery() throws IOException {
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSession sqlSession = sqlSessionFactoryBuilder.build(is).openSession(true);
        StudentoneMapper mapper = sqlSession.getMapper(StudentoneMapper.class);
        Student queryone = mapper.queryone(1);
        System.out.println(queryone.getName());

    }

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值