Mybatis series notes (3) --- SqlMapConfig. xml parsing, mybatissqlmapconfig
SqlMapConfig. xml
SqlMapConfig. xml is the global configuration parameter of Mybatis. Specific MyBatis-API documentation is available for its specific use, which is very clear here, so I will give a few tips:
Its main configuration attributes are as follows:
1. properties
These attributes can be configured externally and can be dynamically replaced. They can be configured in a typical Java property file or passed through the sub-element of the properties element.
1 <! -- Load the property file --> 2 <properties resource = "db. properties"> 3 <! -- Other configuration-related items --> 4 <! -- <Property name = "jdbc. driver" value = "com. mysql. jdbc. Driver"/> --> 5 </properties> 6 <! -- If jdbc. driver is configured in db. properties and name = "", --> 7 <! -- Configure global parameter information -->
If the attribute is configured in more than one place, MyBatis will be loaded in the following order:
- The attribute specified in the properties element is read first.
- Then, read the attribute file in the class path based on the resource attribute in the properties element or the path specified by the url attribute, and overwrite the read attributes of the same name.
- Finally, read the attributes passed as method parameters and overwrite the attributes of the same name that have been read.
Therefore, the attributes passed through the method parameters have the highest priority, and the configuration files specified in the resource/url attribute are the second. The attribute specified in the properties attribute has the lowest priority.
2. settings global parameter configuration
This is an extremely important adjustment setting in MyBatis, which changes the runtime behavior of MyBatis.
Specifically, you do not need to write only one expression:
<! -- Configure global parameter information --> <settings> <setting name = "" value = ""/> </settings>
3. typeAliases (alias)
One of the biggest advantages of Alias formulation is to facilitate our development, because if we do not set an alias, in mapper. xml
Many Statement are defined. Statement requires parameterType to specify the type of the input parameter or the type of the output result, for example:
<! -- Query user information by id --> <select id = "findUserById" parameterType = "int" resultType = "com. study. model. user "> SELECT * from user where id =#{ id} </select>
If the resultType here is an object that requires the full name of the class, can we write only the user to achieve the same effect? Is it convenient for our development?
There are two methods:
1. Definition of a single alias
1 <typeAliases> 2 <! -- 3 Definition of a single alias 4 alias: alias, type: ing type --> 5 <! -- <TypeAlias type = "com. study. model. User" alias = "user"/> --> 6 </typeAliases> 7 <! -- In this way, you only need to enter the user to achieve the same effect -->
2. Define aliases in batches (frequently used)
1 <typeAliases> 2 <! -- Batch alias Definition 3: Specify the package path, automatically scan the pojo under the package, and define the alias. The default alias is the class name (lower-case or upper-case) 4 --> 5 <package name = "com. study. model "/> 6 </typeAliases>
4. mappers (ing configuration)
1. Load a single ing file using resource
1 <! -- Load the ing file --> 2 <mappers> 3 <mapper resource = "sqlmap/User. xml"/> 4 </mappers>
2. Load a single mapper through the mapper Interface
1 <mappers> 2 <! -- To load a single er file through the mapper interface, some rules must be followed: the mapper interface and mapper must be. the xml ing file name must be consistent and in the same directory --> 3 <mapper class = "com. study. mapper. userMapper "/> 4 </mappers>
3. Load mapper in batches (recommended)
1 <mappers> 2 <! -- 3 specify the package name of the ER interface. mybatis will automatically scan all the mapper Interfaces under the package and then load 4 --> 5 <package name = "com. study. mapper "/> 6 </mappers>
I will not talk about other things. For details, refer to the api. It is very detailed. Thank you for your advice. Thank you!