SQL mapping file for MyBatis

Source: Internet
Author: User

1. Using MyBatis to implement conditional queries

(1) SQL mapping file

    • Mapper: The root element node of the map file, with only one attribute namespace, functions as follows:

<1> is used to differentiate between different mapper, globally unique

<2> binds the DAO interface, which is interface-oriented programming. When namespace binds an interface, it can not write the implementation class of the interface, MyBatis will find the corresponding mapper configuration through the interface's fully qualified name to execute the SQL statement. So the name of the namespace must be the same as the interface.

    • Cache: Configure caching for a given namespace
    • Cache-ref: Referencing cache configuration from other namespaces
    • Resultmap: Used to describe the correspondence between a database result set and an object
    • SQL: SQL blocks that can be reused, or can be referenced by other statements
    • Insert: Map INSERT statement
    • Update: Map Updates statement
    • Delete: Map DELETE statement
    • Select: Map Query statements

2. Use Resultmap to complete the display of query results

(1) Resulttype

Resulttype directly represents the return type, including the underlying data type and the complex data type

(2) Resultmap

Resultmap is a reference to the external resultmap definition, corresponding to the ID of the external Resultmap, indicating which resultmap the return result maps to. Its application scenario is generally: database field information inconsistent with object properties or need to make complex joint queries to freely control the mapping results

(3) Association of Resulttype and Resultmap

In the MyBatis query mapping, in fact, the query out of each field value is placed in a corresponding map inside, where the key is the field name, the value is its corresponding value. When the return type property provided by the Select element is Resulttype, MyBatis takes the key-value pair out of the map to the property that is assigned to the object specified by Resulttype (that is, the setter method that invokes the property in the corresponding object is populated).

The Resulttype property and the Resultmap property must never be present at the same time

3. Using @param annotations for multi-parameter entry

4. Using Resultmap for advanced result mapping

(4.1) Association

Association only handles one-to-one affinity relationships

(4.2) Collection

5.RESULTMAP Auto-mapping level and MyBatis cache

(1) MyBatis three matching levels for Resultmap auto-mapping:

    • NONE: Disable automatic matching
    • PARTIAL (default): Automatically matches all attributes, except for internal nesting (association, collection)
    • Full: Automatically matches all

 

  

SQL mapping file for MyBatis

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.