I heard that searching “Java Fish Boy” on wechat will make the technology improve faster

(1) Overview

The core configuration file of Mybatis is Mybatis -config.xml. I will first take out the address of Mybatis official Chinese website:

Mybatis official Chinese website: mybatis.org/mybatis-3/z…

The address above corresponds to the Mybatis configuration properties, which contain Settings and properties information that will deeply affect Mybatis behavior.

Environments and Mappers have been covered before, but here are some of the more important configuration items.

(2) Properties

When we first write mybatis-config.xml, we write the following code:

<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis? useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
Copy the code

This is where the various attributes are written down in the XML configuration file and can be cumbersome to modify, so you can use the properties attribute. Create a new db.properties directory under resources:

driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis? useSSL=true&useUnicode=true&characterEncoding=UTF-8 username=root password=123456Copy the code

Next, add the properties node to mybatis-config.xml. Note that this node must be in the first place

<properties resource="db.properties">
</properties>
Copy the code

You can then replace XML attributes with attributes in Properties

<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
Copy the code

(3) Settings

MyBatis changes the runtime behavior of MyBatis. This is a set of Settings that will change the runtime behavior of MyBatis.

Setting cacheEnabled enables or disables caching of configuration files globally.

Set whether to enable the hump naming rule

Whether lazy loading is enabled

Specifies the log to use

The configuration mode is as follows:

<settings>
  <setting name="cacheEnabled" value="true"/>
  <setting name="lazyLoadingEnabled" value="true"/>
  <setting name="mapUnderscoreToCamelCase" value="false"/>
  <setting name="logImpl" value="LOG4J"/>
</settings>
Copy the code

(4) typeAliases

A type alias sets an abbreviated name for a Java type. It is only used for XML configuration and is intended to reduce redundant fully qualified class name writing.

There are two ways to alias a type. The first is to alias the class, and the second is to specify a package name. For example, take out the SQL you wrote earlier

<select id="getUserById" resultType="com.javayz.pojo.User" parameterType="int">
    select * from user where id=#{id};
</select>
Copy the code

Here the resultType is written to the fully qualified name of the class, which we can simplify by using the type alias in mybatis-config.xml.

<typeAliases>
    <typeAlias type="com.javayz.pojo.User" alias="user"/>
</typeAliases>
Copy the code

This way you can use user directly in your resultType.

The second way is to specify a package name, Mybatis will search for the desired Javabeans under the specified package name path. Modify the above code to use the package name to specify

<typeAliases>
    <package name="com.javayz.pojo"/>
</typeAliases>
Copy the code

If we specify an Alias using @alias, we can use the specified Alias. If we do not specify an Alias, we use the lower case of the current entity class name as the Alias.

For example, the alias is user with or without an annotation

@Alias("user")
public class User {... }Copy the code

The following are the built-in aliases for Java types

(5) Mappers

Mybatis -config. XML: mybatis-config. XML: mybatis-config. XML: mybatis-config. XML: mybatis-config. XML: mybatis-config. XML: mybatis-config. XML: mybatis-config.

Mappers are introduced in four ways:

<! -- Use a resource reference relative to the classpath -->
<mappers>
  <mapper resource="org/mybatis/builder/AuthorMapper.xml"/>
  <mapper resource="org/mybatis/builder/BlogMapper.xml"/>
  <mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>
Copy the code
<! -- Use fully qualified resource locator (URL) -->
<mappers>
  <mapper url="file:///var/mappers/AuthorMapper.xml"/>
  <mapper url="file:///var/mappers/BlogMapper.xml"/>
  <mapper url="file:///var/mappers/PostMapper.xml"/>
</mappers>
Copy the code
<! Implement the fully qualified class name of the class using the mapper interface
<mappers>
  <mapper class="org.mybatis.builder.AuthorMapper"/>
  <mapper class="org.mybatis.builder.BlogMapper"/>
  <mapper class="org.mybatis.builder.PostMapper"/>
</mappers>
Copy the code
<! Register all mapper interface implementations as mapper
<mappers>
  <package name="org.mybatis.builder"/>
</mappers>
Copy the code

(6) Summary

The above introduces several commonly used attributes of Mybatis. It is enough to master the above attributes in daily use. It is still suggested that you start coding, take official documents as the general learning direction, and then study with blog content.