Detailed introduction to resultmap of mybatis tutorial
黄舟
Release: 2017-09-02 10:34:55
Original
1681 people have browsed it
This article mainly introduces the resultmap of the mybatis tutorial. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
SQL mapping XML file is where all sql statements are placed. A workspace needs to be defined, which is generally defined as the path to the corresponding interface class. After writing the SQL statement mapping file, it needs to be quoted in the mappers tag of the MyBAtis configuration file, for example:
When the Java interface and XML file are in a relative path When, it does not need to be declared in the mappers of the myBatis configuration file. Some basic elements of SQL mapping XML files: 1. cache – configure the cache for a given schema
2. cache-ref – reference a cache from another schema 3. resultMap – This is the most complex but powerful element, which describes how to load objects from the result set 4. sql – a SQL block that can be reused by other statements 5. insert – mapping INSERT statement 6. update – maps UPDATE statement 7. delete – maps DELEETE statement 8. select – maps SELECT statement
2.1 resultMap
resultMap is the most important and powerful element in MyBatis. You can save 90% of the code than using JDBC to call the result set, and it also allows you to do many things that JDBC does not support. In fact, it may take thousands of lines of code to write a complex statement equivalent to an interactive mapping. The purpose of ResultMaps is to use such simple statements without the need for redundant result mapping. For more complex statements, no more is needed except for some absolutely necessary statements to describe relationships.
resultMap attribute: type is the java entity class; id is the identifier of the resultMap.
resultMap can set the mapping: 1. constructor – a constructor used to reflect the result to an instantiated class
a ) idArg – ID parameter; mark the result set as ID to facilitate global calls
b) arg – the usual result reflected to the constructor
2. id – ID result, mark the result set as ID , to facilitate global calls3. result – the ordinary result reflected to the JavaBean property4. association – the combination of complex types; the type of multiple result synthesis
a) nested result mappings – several resultMap nested relationships within itself, or can be referenced to another one5. collection – a collection of complex types6. nested result mappings – a collection of resultMap, which can also be referenced to another one7. discriminator – uses a result value to decide which resultMap to use
a) case – the result of some basic value Mapped case situations
i. Nested result mappings – A case situation is itself a result map, so it can also contain some of the same elements, or it can reference an external resultMap.
id, result
id, result is the simplest mapping, id is the primary key mapping; result is other basic database table fields to entity classes Mapping of properties.
id, result statement attribute configuration details:
Property Description
Property
Description
##property
The property name that needs to be mapped to JavaBean.
column
The column name or label of the data table Alias.
javaType
A complete class name, or is a type alias. If you match a JavaBean, MyBatis will usually detect it on its own. Then, if you want to map to a HashMap, then you need to specify the purpose of javaType.
jdbcType
List of types supported by the data table. This attribute is only useful for columns that allow nulls during insert, update or delete. JDBC requires this, but MyBatis does not. If you are coding directly against JDBC and have columns that allow nulls, you will want to specify this.
typeHandler
Use this attribute to override the type processor. This value can be a complete class name or a type alias.
The above is the detailed content of Detailed introduction to resultmap of mybatis tutorial. For more information, please follow other related articles on the PHP Chinese website!
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn