博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
浅谈mybatis多对一单向映射
阅读量:4228 次
发布时间:2019-05-26

本文共 2204 字,大约阅读时间需要 7 分钟。

刚刚在学mybatis,谈下我队多对一单向映射的看法,新手,有错误请指出,

mybatis的实体映射是通过在外键表中引入主键表的实体类的,

比如主键表为tb_clazz,外键表为tb_student,学生表的clazz_id作为外键引用到tb_clazz,

那么实体映射表为:

public class Clazz {

    private Integer id;
    private String code;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    @Override
    public String toString(){
        return "["+id+","+code+"]";
    }
}

public class Student {

    private Integer id;
    private String name;
    private String sex;
    private Integer age;
    private Clazz clazz;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public Clazz getClazz() {
        return clazz;
    }
    public void setClazz(Clazz clazz) {
        this.clazz = clazz;
    }
    @Override
    public String toString(){
        return "["+id+","+name+","+sex+","+age+","+clazz.toString()+"]";
    }
}

然后再xml中配置关系,通过resultMap实现

<resultMap type="org.fkit.domain.Student" id="studentResultMap">

        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <association property="clazz" column="clazz_id"
        javaType="org.fkit.domain.Clazz" select="selectClazzWithId"
        ></association>
    </resultMap>
    <select id="selectClazzWithId" resultType="org.fkit.domain.Clazz">
        select * from tb_clazz where id=#{id}
    </select>
    <select id="selectStudent" resultMap="studentResultMap">
        select * from tb_student
    </select>

因为表中存储的是id,但是实体映射类中存储的是实体,所有先需要根据表的id查询到实例,

DEBUG [main] -==>  Preparing: select * from tb_student

DEBUG [main] -==> Parameters:
DEBUG [main] -====>  Preparing: select * from tb_clazz where id=?
DEBUG [main] -====> Parameters: 1(Integer)
DEBUG [main] -<====      Total: 1
DEBUG [main] -====>  Preparing: select * from tb_clazz where id=?
DEBUG [main] -====> Parameters: 2(Integer)
DEBUG [main] -<====      Total: 1
DEBUG [main] -<==      Total: 4

由日志信息可以知道,先是查询tb_student表,然后对于外键在进行查询,但是并不重复查询

转载地址:http://dpjqi.baihongyu.com/

你可能感兴趣的文章
Information Systems : Achieving Success by Avoiding Failure
查看>>
AutoCAD & AutoCAD LT All-in-One Desk Reference For Dummies
查看>>
C# Programmer's Handbook
查看>>
Constructing Accessible Web Sites
查看>>
Linux Smart Homes For Dummies
查看>>
Building an ASP.NET Intranet
查看>>
Automating UNIX and Linux Administration
查看>>
Advanced C# Programming
查看>>
The Java(TM) Tutorial: A Short Course on the Basics (3rd Edition)
查看>>
Solaris(TM) Performance and Tools: DTrace and MDB Techniques for Solaris 10 and OpenSolaris
查看>>
Unicode Explained
查看>>
Essential C# 2.0
查看>>
Eric Sink on the Business of Software
查看>>
LPI Linux Certification in a Nutshell
查看>>
ATL Internals: Working with ATL 8 (2nd Edition)
查看>>
Programming Sudoku
查看>>
Collaborative Geographic Information Systems
查看>>
The Definitive Guide to the .NET Compact Framework
查看>>
Java 2 Platform, Enterprise Edition: Platform and Component Specifications
查看>>
C++ Coding Standards: 101 Rules, Guidelines, and Best Practices
查看>>