MyBatis Series notes (3)---sqlmapconfig.xml analysis

Source: Internet
Author: User
Tags aliases

Sqlmapconfig.xml

Sqlmapconfig.xml is MyBatis's global configuration parameters, about his specific use of a special MYBATIS-API document, which is very clear, so I will pick a few to say:

The properties of his main configuration are as follows:

1.properties Properties

These properties are externally configurable and can be dynamically replaced, can be configured in a typical Java properties file, or passed through a child element of the properties element.

1 <!--Load Properties File -2     <PropertiesResource= "Db.properties">3         <!--other things that can be related to configuration -4         <!--<property name= "Jdbc.driver" value= "Com.mysql.jdbc.Driver"/> -5     </Properties>6     <!--here if the db.properties and Name= "" are configured with Jdbc.driver, then the first name of the -7     <!--to configure global parameter information -    

If the properties are configured in more than one place, the MyBatis will be loaded in the following order:

    • The property specified in the properties element is first read.
    • It then reads the class path subordinate file according to the resource property in the properties element or reads the property file based on the path specified by the URL property and overwrites the Read property with the same name.
    • Finally, the property passed as a method parameter is read and overridden with the same name as the Read property.

Therefore, the property passed by the method parameter has the highest priority, the configuration file specified in the Resource/url property is the second, and the lowest priority is the property specified in the Properties property.

2.settings Global configuration of parameters

This is an extremely important adjustment setting in MyBatis, which alters the runtime behavior of the MyBatis.

Specifically, it's not written. Write only one expression:

<!---           <settings>          <   name= ""  value= ""/>      </  Settings>

3.typeAliases (alias)

One of the biggest advantages of setting up aliases is the convenience of our development, because if we do not set aliases, in Mapper.xml

Many statement are defined, statement need to ParameterType specify the type of input parameter or specify the type of output result such as:

<!---    < id = "Finduserbyid"  ParameterType= "int"  resulttype= "Com.study.model.User">         SELECT * from USER WHERE id= #{id}     </ Select >

Resulttype here if the object must be the full name of the class, then we can only write the user will be able to achieve the same effect, this is not conducive to the simple development of our

There are two ways of doing this:

1. Definition of a single alias

1 <typealiases>2         <!--3 definition of a single alias4 Alias : Aliases, type: Types of alias Mappings -5             <!--<typealias type= "Com.study.model.User" alias= "User"/> -6     </typealiases>7 <!--in this way, you can do the same thing just by typing the user. -

2. Batch definition aliases (common)

 1  <  typealiases  >  2   bulk alias definition  3   Specifies the package path, automatically scans the Pojo under the package, defines aliases, aliases The default is the class name (first letter lowercase or uppercase)  4  -->  5  <  package  name  = "Com.study.model"  />  Span style= "color: #008080;" >6  </ typealiases  >  

4.mappers (Map configuration)

1. Loading a single mapping file via resource

1 <!--  - 2     <mappers>3         < Resource = "sqlmap/user.xml"/>4            </mappers>

2. Loading a single mapper via the Mapper interface

1 < mappers > 2         <!--  - 3         <class= " Com.study.mapper.UserMapper "/>     4     </mappers >

3. Bulk load mapper (recommended)

 1  <  mappers  >  2  3   Specifies the package name of the Mapper interface MyBatis automatically scans all mapper interfaces under this package and then executes the load  4  -->  5  <  package  name  = "Com.study.mapper"  />  6  Span style= "color: #0000ff;" ></ mappers  >  

Other I will not speak, to detailed can find the API, very detailed, welcome to guide, thank you!

MyBatis Series notes (3)---sqlmapconfig.xml analysis

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.