Explain in simple terms Mybatis (5) Detailed explanation of SqlMapConfig.xml

1.properties property configuration

<properties resource="jdbc.properties">
        <!--<property name="" value=""/>-->
</properties>

You can reference external properties such as in the jdbc.properties file, or you can define name and value yourself,

properties:

Notice:

mybatis will load properties in the following order:

Properties defined in the properties element body are read first.

Then it will read the properties loaded by resource or url in the properties element, it will overwrite the read properties of the same name.

Finally, read the property value passed by parameterType, and only define the property value in the properties file.

Defining properties in the properties file must have certain specificity, such as xxx.xxx

2.settings global parameter configuration

Some parameters of mybatis framework runtime

3. typeAlias ​​(alias)

If the type of the input parameter and the type of the output parameter in mapper.xml are the pojo type defined by ourselves, the input character will be very long. At this time, we need to define our own alias to shorten the string length of the input and output type.

<typeAliases>
        <!-- Definition of a single alias
            type: the path to the type
            alias: alias
         -->
        <!--<typeAlias type="com.beyond.mybatis.po.User" alias="user"/>-->
        <!-- Definition of bulk aliases
            Specify the package name, mybatis will automatically scan the po class under the package, and the alias is the name of the class (the first letter can be uppercase or lowercase)
            alias: alias
         -->
        <package name="com.beyond.mybatis.po"/>
    </typeAliases>

4.typeHandler type handler

In myabtis, the conversion between jdbc type and java type is completed through typeHandler.

Under normal circumstances, the types supported by mybatis by default are sufficient.

5. Mapping configuration mapper.

Load the mapper file.

There are two ways for a single load. There is one kind of bulk loading.

<mappers>
        <!-- resource loads a single mapper file -->
        <mapper resource="sqlmap/userMapper.xml"/>
        <mapper resource="mapper/userMapper.xml"/>
        <!-- Load via mapper interface
            Follow some specifications: you need to put the mapper interface and the mapper mapping file in the same directory with the same name,
            Premise: use mapper proxy method
         -->
        <mapper class="com.beyond.mybatis.mapper.UserMapper" />
        <!-- Bulk load mapper
            Specify the package name of the mapper interface, mybatis will automatically scan all mapper interfaces under the package for loading
            Follow some specifications: you need to put the mapper interface and the mapper mapping file in the same directory with the same name,
            Premise: use mapper proxy method
         -->
        <package name="com.beyond.mybatis.mapper"/>

</mappers>

An exception may occur when using batch loading or class loading (if it is a maven project)

 Invalid bound statement (not found): com.beyond.mybatis.mapper.UserMapper.selectUser

The following configuration needs to be added to pom.xml

<!-- If this node is not added, the mapper.xml file of mybatis will be missed. -->
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474733&siteId=291194637