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!
Statement of this 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
Interpretation of MyBatis dynamic SQL tags: Detailed explanation of Set tag usage MyBatis is an excellent persistence layer framework. It provides a wealth of dynamic SQL tags and can flexibly construct database operation statements. Among them, the Set tag is used to generate the SET clause in the UPDATE statement, which is very commonly used in update operations. This article will explain in detail the usage of the Set tag in MyBatis and demonstrate its functionality through specific code examples. What is Set tag Set tag is used in MyBati
Detailed explanation of MyBatis caching mechanism: One article to understand the principle of cache storage Introduction When using MyBatis for database access, caching is a very important mechanism, which can effectively reduce access to the database and improve system performance. This article will introduce the caching mechanism of MyBatis in detail, including cache classification, storage principles and specific code examples. 1. Cache classification MyBatis cache is mainly divided into two types: first-level cache and second-level cache. The first-level cache is a SqlSession-level cache. When
MyBatis is a popular Java persistence layer framework that implements the mapping of SQL and Java methods through XML or annotations, and provides many convenient functions for operating databases. In actual development, sometimes a large amount of data needs to be inserted into the database in batches. Therefore, how to optimize batch Insert statements in MyBatis has become an important issue. This article will share some optimization tips and provide specific code examples. 1.Use BatchExecu
MyBatisGenerator is a code generation tool officially provided by MyBatis, which can help developers quickly generate JavaBeans, Mapper interfaces and XML mapping files that conform to the database table structure. In the process of using MyBatisGenerator for code generation, the setting of configuration parameters is crucial. This article will start from the perspective of configuration parameters and deeply explore the functions of MyBatisGenerator.
As network technology continues to develop, database attacks are becoming more and more common. SQL injection is one of the common attack methods. Attackers enter malicious SQL statements into the input box to perform illegal operations, causing data leakage, tampering or even deletion. In order to prevent SQL injection attacks, developers must pay special attention when writing code, and when using an ORM framework such as MyBatis, they need to follow some best practices to ensure the security of the system. 1. Parameterized query Parameterized query is the anti-
Detailed explanation of MyBatis first-level cache: How to improve data access efficiency? During the development process, efficient data access has always been one of the focuses of programmers. For persistence layer frameworks like MyBatis, caching is one of the key methods to improve data access efficiency. MyBatis provides two caching mechanisms: first-level cache and second-level cache. The first-level cache is enabled by default. This article will introduce the mechanism of MyBatis first-level cache in detail and provide specific code examples to help readers better understand
MyBatis is a lightweight Java persistence layer framework that provides many convenient SQL statement splicing functions, among which dynamic SQL tags are one of its powerful features. In MyBatis, the Trim tag is a very commonly used tag, used to dynamically splice SQL statements. In this article, we will take a deep dive into the functionality of the Trim tag in MyBatis and provide some concrete code examples. 1. Introduction to Trim tag In MyBatis, the Trim tag is used to remove the generated S
MyBatis is an excellent persistence layer framework. It supports database operations based on XML and annotations. It is simple and easy to use. It also provides a rich plug-in mechanism. Among them, the paging plug-in is one of the more frequently used plug-ins. This article will delve into the principles of the MyBatis paging plug-in and illustrate it with specific code examples. 1. Paging plug-in principle MyBatis itself does not provide native paging function, but you can use plug-ins to implement paging queries. The principle of paging plug-in is mainly to intercept MyBatis