Mybatis implements one-to-one association and ing instance code. mybatis is one-to-one

Source: Internet
Author: User

Mybatis implements one-to-one association and ing instance code. mybatis is one-to-one

Preface

In the objective world, objects are rarely isolated, such as the relationship between classes and students, the relationship between students and courses, and their instances can access each other. This is the relationship. The Association ing of MyBatis can greatly simplify the access to data in the persistent layer. The classification of associations is as follows:

  • One-to-one
  • One-to-multiple
  • Many-to-many

We first draw a simplified E-R diagram to represent three associations.

Three relationships:

  • One-to-one: one class teacher only belongs to one class, and one class teacher can only have one class teacher.
  • One to multiple: one class has multiple students, and one student belongs to only one class.
  • Many-to-many: one student can select multiple courses, and one course can be selected by multiple students.

Introduction

In actual project development, there is often a one-to-one relationship. For example, if a person corresponds to an ID card, this is a one-to-one relationship. Below is a simple

Instance:

1. I skipped the table creation process. It is mainly a Person table and an IDCard table. The related attributes are shown in step 2Pojo class attributes;

2. Create a Person object and an IDCard object:

Mybatis/pri/xiaoyang/otot/pojo/IDCard. java

Public class IDCard implements Serializable {private int id; // primary key private String cardCode; // id card number private String carAddress; // ID card address // constructor and Getter/Setter/toString ...}

Mybatis/pri/xiaoyang/otot/pojo/Person. java

Public class Person implements Serializable {private int id; // primary key private String name; // name private String sex; // gender private IDCard iDCard; // id card object, one-to-one relationship between people and ID card // constructor and Getter/Setter/toString ...}

3. The following is the XML ing file:

Mybatis/pri/xiaoyang/otot/mapper/IDCardMapper. xml

<Mapper namespace = "pri. xiaoyang. otot. mapper. IDCardMapper"> <! -- Query IDCard information based on the primary key, encapsulate it as an IDCard object, and return --> <select id = "selectIDCardById" pamaeterType = "int" resultType = "pri. xiaoyang. otot. pojo. IDCard "> select * from idcard where id =#{ id} </select> </mapper>

Mybatis/pri/xiaoyang/otot/mapper/PersonMapper. xml

<Mapper namespace = "pri. xiaoyang. otot. mapper. PersonMapper"> <! -- Query the person information based on the Person primary key, encapsulate the object as a Person and return --> <select id = "selectPersonById" pamaeterType = "int" resultMap = "PersonMapper"> select * from person where id = # {id} </select> <! -- Map the resultMap of the Person object --> <resultMap type = "pri. xiaoyang. otot. pojo. person "id =" PersonMapper "> <id property =" id "column =" id "/> <result property =" name "column =" name "/> <result property = "sex" column = "sex"/> <! -- One-to-one association ing: association --> <association property = "iDCard" column = "card_id" select = "pri. xiaoyang. otot. mapper. IDCardMapper. selectIDCardById "javaType =" pri. xiaoyang. otot. pojo. IDCard "/> </resultMap> </mapper>

Note:In the PersonMapper. A <select/> label is defined in xml. The SQL statement of this label is to query the unique Person information based on the primary key field of the Person table. Because the Person table is associated with the IDCard table, therefore, a map object whose resultMap value is personMap is returned. PersonMap uses the <association/> label to map one-to-one associations, the select attribute in the label indicates that the column attribute value "card_id" will be used as the parameter to run the selectIDCardById defined in IDCardMapper to query the corresponding IDCard data, the queried data is encapsulated in the "card" Object of the property value.

Summary

The above is all the content of this article. I hope the content of this article has some reference and learning value for everyone's learning or work. If you have any questions, please leave a message to us, thank you for your support.

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.