MyBatis starts from entry to end 3: one-to-one association query, mybatis one-to-one

Source: Internet
Author: User

MyBatis starts from entry to end 3: one-to-one association query, mybatis one-to-one
Preface

To put it simply, it is difficult to implement association query in mybatis. xml because the framework is essentially a semi-automated Implementation of orm. In this case, the association attribute and the resultMap attribute are used for one-to-one association query using mybatis.

 

Build a Development Environment

Create a student table and a teacher table to create a one-to-one association query scenario. Add a foreign key supervisor_id to the student table to associate with the teacher table.

1 create table [dbo]. [t_teacher] (2 [id] [int] IDENTITY (1, 1) not null, 3 [name] [nvarchar] (30) NULL, 4 [title] [nvarchar] (30) NULL, 5 primary key clustered 6 (7 [id] ASC 8) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 9) ON [PRIMARY] 10 11 GOView Code 1 create table [dbo]. [t_student] (2 [id] [int] IDENTITY (1, 1) not null, 3 [name] [nvarchar] (30) NULL, 4 [major] [nvarchar] (30) NULL, 5 [supervisor_id] [int] NULL, 6 primary key clustered 7 (8 [id] ASC 9) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] 10) ON [PRIMARY]View Code

 

One-to-one association query

The key to one-to-one association is to create a resultMap in mapper. xml. The following code adds the property association in studentResultMap. property is the foreign key property name in the model class. Do not forget to specify JavaType.

 1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.autohome.mapper.Student"> 6     <resultMap id="studentResultMap" type="Student"> 7         <id property="id" column="id"/> 8         <result property="name" column="name"/> 9         <result property="major" column="major"/>10         <association property="supervisor" javaType="Teacher">11             <id property="id" column="t_id" />12             <result property="name" column="t_name"/>13             <result property="title" column="title"/>14         </association>15     </resultMap>16 17  18     <select id="getStudentById" parameterType="int" resultMap="studentResultMap">19         SELECT st.id,st.name,st.major,20                t.id t_id,t.name t_name,t.title21         FROM t_student st inner join t_teacher t on st.supervisor_id=t.id22         where st.id=#{id}23     </select>24 </mapper>

Teacher model

public class Teacher {    private int id;    private String name;    private String title;    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 String getTitle() {        return title;    }    public void setTitle(String title) {        this.title = title;    }}

Student model

public class Student {    private int id;    private String name;    private String major;    private Teacher supervisor;    public Teacher getSupervisor() {        return supervisor;    }    public void setSupervisor(Teacher supervisor) {        this.supervisor = supervisor;    }    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 String getMajor() {        return major;    }    public void setMajor(String major) {        this.major = major;    }}

  

Unit Test
@ Test public void getStudentById () {SqlSession sqlSession = null; try {sqlSession = sqlSessionFactory. openSession (); Student student = sqlSession. selectOne ("com. autohome. mapper. student. getStudentById ", 1); System. out. println ("id:" + student. getId () + ", name:" + student. getName () + ", instructor name:" + student. getSupervisor (). getName () + ", instructor title:" + student. getSupervisor (). getTitle ();} catch (Exception e) {e. printStackTrace ();} finally {sqlSession. close ();}}

Implementation

 

Nested resultMap

The above resultmap is always inconvenient to use. If we need to use the teacher table for other associated queries in the future, we will take teacherResultMap separately, not only that the resultMap can be nested, SQL statements can also be nested.

Create studentResultMap and suprvisorResultMap respectively. Then use resultMap to reference supervisorResultMap in the association of studentResultMap.

<mapper namespace="com.autohome.mapper.Student">    <resultMap id="studentResultMap" type="Student">        <id property="id" column="id"/>        <result property="name" column="name"/>        <result property="major" column="major"/>        <association property="supervisor" resultMap="suprvisorResultMap"/>    </resultMap>    <resultMap id="suprvisorResultMap" type="Teacher">        <id property="id" column="t_id" />        <result property="name" column="t_name"/>        <result property="title" column="title"/>    </resultMap>    <select id="getStudentById" parameterType="int" resultMap="studentResultMap">        SELECT st.id,st.name,st.major,               t.id t_id,t.name t_name,t.title        FROM t_student st inner join t_teacher t on st.supervisor_id=t.id        where st.id=#{id}    </select></mapper>

The implementation result is the same as above

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.