Mybatis association mapping

Mybatis association mapping

The actual development, database operations often involve multiple tables, which will involve the relationship between the object and the object in the object-oriented. For operations between multiple tables, MyBatis provides association mapping,
through association mapping can be a good deal with the relationships between objects and objects.

1.1 Overview of relationship

In a relational database, there are three relationships between a plurality of tables, respectively, one to one, and many-to-many, as shown below:

Three relationships are as follows:

One: any other one of the primary key is introduced as a foreign key.

Many: one of the "many" add "a" of one of the primary key as a foreign key.

Many-: generating an intermediate table, the introduction of two primary key table as a foreign key, two primary keys become the primary key or use a new field as the primary key.

In Java, may be performed through the object relationships described, the FIG as shown:

One relationship: the object is defined in the other type of this class, as defined in the class A type B type B properties, class B, class A type attributes define a.

Many relationship: A class is a class B type corresponding to a plurality of types, need to be introduced into the manner set class type of the object B in the class A, class A defines a type attribute in class B.

Many relationship: class type definitions set B in A class defines a set of class A in class B type.

1.2 One to One

In real life, one to one relationship is very common. For example, a person can only have one ID card, an ID card at the same time will only correspond to a person

Element, a Child elements, MyBatis is to deal one on one relationship through the element.
in Elements, generally can configure the following properties:

property: Specifies the class object is mapped to the physical properties, the correspondence table fields;

column: Specifies the corresponding field in the table;

javaType: Specifies the type of the entity is mapped to the attribute of the object;

select: Specifies introduced nested query sub SQL statement for the property association mapping nested query;

fetchType: Specifies whether to delay loading is enabled when the associated query. This property has two attribute values ​​lazy and eager, lazy default value (i.e., the association map default lazy loading).

MyBatis loads relationship is primarily in two ways: nested queries and nested results.

Nested query: nested query is executed by another one mapped SQL statement that returns the complex type desired.

  嵌套查询是在查询SQL中嵌入一个子查询SQL;

  嵌套查询会执行多条SQL语句;

  嵌套查询SQL语句编写较为简单;

Nested Results: Results are nested using nested result mappings repeated to process a subset of the combined result.

  嵌套结果是一个嵌套的多表查询SQL;

  嵌套结果只会执行一条复杂的SQL语句;

  嵌套结果SQL语句编写比较复杂;

Although the use of nested queries is relatively simple, but the way nested query to execute multiple SQL statements, which for large data sets or lists show is not very good,
because this could lead to hundreds of thousands of SQL statements that are associated execution, which greatly reduces the consumption of database query performance and efficiency.

use Mapping elements in one association is very simple and requires only the following two examples with reference to the configuration.

Nested query, property refers to a class attribute, column refers to a table field, select represents nested subquery, javaType indicates that the associated attribute type.

<association property="card" column="card_id" 
     javaType="com.itheima.po.IdCard" select="com.itheima.mapper.IdCardMapper.findCodeById" />
嵌套结果,property指的是类属性,column指的是表字段,javaType表示关联属性类型。
<association property="card" javaType="com.itheima.po.IdCard">
              <id property="id" column="card_id" />
              <result property="code" column="code" />
</association>

1.3-to-many

Developers reach more relationship-many (or many). For example, a user can have multiple orders, multiple orders simultaneously all owned by a user.

Element, a Child elements, MyBatis-to-many relationship is handled by this element.
Most of sub-elements and attributes The same element, but it further comprises a special attribute --ofType.
ofType: ofType javaType attribute corresponding to the attribute, the element which is used to specify the type of entity object class attribute contains a collection.
Using the element is also very simple, with reference to the following two examples can also be configured specific code as follows:
nested query, refers to a class attribute Property, column refers to the table fields, represented by nested subquery SELECT, indicates that the associated ofType collection class attribute type.

Nested result, property refers to a class attribute, column refers to the table fields, ofType represents collections associated attribute type.



1.4-many

In the actual project development, many to many relationships is also very common. And to order merchandise, for example, may comprise a plurality of orders of goods, and a commodity and can belong to a plurality of order

in the database, usually associated with a many-to maintain the intermediate table, the intermediate table order id as foreign key to the table order id, item id id product table as a foreign key reference.

In MyBatis, the relationship many to many queries can also use previously described Elements for processing (its use-many relationships and query usage is basically the same).

summary

Mainly study the relationships between data tables between MyBatis and objects, as well as processing of the frame MyBatis association relationship.
The above is based on Java EE enterprise application development tutorial (Spring + Spring MVC + MyBatis) made some notes and summary.

Guess you like

Origin www.cnblogs.com/xidianzxm/p/11498767.html