XML file for SQL mapping in MyBatis

Source: Internet
Author: User
Tags object object

Mappers

Since MyBatis's behavior has been configured by the elements of the MyBatis configuration file described in the previous article, we are now going to define the SQL mapping statement. But first we need to tell MyBatis where to find these configurations. Java does not provide a good way to do this, so the best way is to tell MyBatis where to look for the mapping file. You can use a resource reference relative to a classpath, or a character representation, or a fully qualified name for the URL app.

In the MyBatis schema, the POJOs object object is a mapper interface, the Mapper interface, and the XML corresponding to the SQL map are under the same package.


XML file for SQL mapping

MyBatis true evidence of the power of the actual mapping statement. Here is where the miracle happened, for all the power, the SQL map XML file is quite simple. Of course, if you compare them with the equivalent JDBC code, you'll find that the mapping file saves about 95% of the code. MyBatis focuses on SQL. To make it original from the ordinary way.


The next section describes the details of each element, starting with the statement itself.

Select

Query statements are one of the most commonly used elements when using MyBatis. There will also be a lot of queries for each insert, modification or deletion, which is why MyBatis will focus and effort on the query and result mappings. The query element for a simple category is very simple. Like what:


The following is a description of the property: The ID represents the method name, corresponding to the method name in the Mapper interface class, and the unique identifier in the namespace, which can be used to refer to this statement. The parametertype represents the fully qualified name or alias of the parameter class that will pass in the precompiled statement as a query condition. Resulttype represents the type of the returned result set, specifically the fully qualified name or alias of the class of the expected type returned from this statement. Note the collection case, which should be the type that the collection can contain, not the collection itself. Use Resuttype or Resultmap, but not at the same time.

The Resultmap represents a resultmap that is named outside the reference. The return map is the most powerful feature of MyBatis, and with a good understanding of it, many complex mapping scenarios can be solved. Use Resulttype or Resultmap, but not at the same time. Flushcache controls a property of the cache with a default value of FALSE. Set it to true to indicate that no sentence will be quoted, causing the cache to be emptied.

UseCache set it to true will cause the result of this statement not to be cached, the default value: True. Timeout represents the maximum wait value that waits for the database to return a request result and throws an unexpected time. Fetchsize This is the number of result rows that imply that the driver returns in bulk each time. Default does not process (drive self-processing). StatementType represents a type of statement,prepared or callable. This will let MyBatis use the option to use Statement,preparedstatement or callablestatement. The default value is: PREPARED.

ResultsetType represents a forward_only, scroll_sensitive,scroll_insensitive. Default is not set.





MyBatis there is another way to handle a database that does not support auto-generated types, or if the JDBC driver does not support primary key generation problems when auto-generating primary keys. Here's a very simple (and even silly) example where he can generate a random ID (maybe you won't, but this shows the flexibility of mybatis handling problems because he doesn't care about the generation of IDs)



XML file for SQL mapping in 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.