mybatis in-depth learning

Posted by caedo on Wed, 29 Dec 2021 13:55:48 +0100

1. parameterType configuration parameter description

We generally use parameterType to pass in parameters in SQL. The attribute type can be a specific basic type, such as reference type (String) or entity class type (POJO class).
Basic type and string can write type name directly or register Category: Java lang.String
For the entity class type, we can only use the fully 2 qualified category, because mybatis has registered the alias for the data type, but the entity class is not registered, so we must write the fully qualified class name.

2. resultType configures the result type

The resultType property can specify the type of result set, which supports basic types and entity class types. If you have registered a type alias, you can directly use the alias. If you have not registered, you must use the fully qualified class name.

3. resultMap result type

The resultMap tag can establish a corresponding relationship when the column name of the query is inconsistent with the attribute name of the entity class. So as to realize packaging. As follows:

<!-- establish User Correspondence between entity and database table
type Property: Specifies the fully qualified class name of the entity class
id Attribute: given a unique ID, it is used for query select For label reference.
-->
<resultMap type="com.itheima.domain.User" id="userMap">
    <id column="id" property="userId"/>
    <result column="username" property="userName"/>
    <result column="sex" property="userSex"/>
    <result column="address" property="userAddress"/>
    <result column="birthday" property="userBirthday"/>
</resultMap>
<!--id Label: used to specify the primary key field
result Label: used to specify non primary key fields
column Property: used to specify the database column name
property Attribute: used to specify the entity class attribute name
 The calling method is as follows-->
<!--Query all users
 Use here resultMap We specify the result type
-->
<select id="findAll" resultMap="userMap">
    select * from user;
</select>

4,SqlMapConfig. The content and order of configuration in XML (you can refer to the official document of mybatis)

-properties (Properties)
    --property
-settings(Global configuration parameters)
    --setting
-typeAliases (Type alias)
    --typeAliase
    --package
-typeHandlers(Type (processor)
-objectFactory(Object factory)
-plugins(Plug in)
-environments(Environment collection properties (object)
    --environment(Environment (sub attribute object)
    ---transactionManager(Transaction management)
    ---dataSource((data source)
-mappers (Mapper)
    --mapper
    --package

4.1. properties
When configuring with the properties tag, we can specify the property configuration in two ways.
The first is configured in the SqlMapConfig file, as follows:

stay SqlMapConfig.xml Configuration in:
<!--properties Property configuration-->
<properties>
    <property name="jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
    <property name="jdbc.url" value="jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=false&amp;serverTimezone=Asia/Shanghai"/>
    <property name="jdbc.username" value="root"/>
    <property name="jdbc.password" value="Admin890"/>
</properties>
<!--Configure environment variables-->
<environments default="mysql">
    <environment id="mysql">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <property name="driver" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </dataSource>
    </environment>
</environments>

The second is to read the configuration from an external file
Configuration external file: JDBC config properties

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=Admin890

Configure the corresponding configuration file

stay SqlMapConfig.xml Configuration in:
<properties resource="jdbcConfig.properties"></properties>
<!--Configure environment variables-->
<environments default="mysql">
    <environment id="mysql">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <property name="driver" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}"/>
            <property name="username" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
        </dataSource>
    </environment>
</environments>

4.3 type aliases
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. For example:

stay SqlMapConfig.xml Configuration in:
<typeAliases>
    <!--Single alias definition-->
    <typeAlias type="com.shenqiang.domain.User" alias="user"/>
    <!-- Batch alias definition, scan the classes under the whole package, and the alias is the class name (the first letter can be uppercase or lowercase) -->
    <package name="com.shenqiang.domain"/>
    <package name="Other packages"/>
</typeAliases>

Call in mapping configuration file

<!--Insert user-->
<insert id="insertUser" parameterType="user">
    insert into user(username,address,sex,birthday) values (#{userName},#{userAddress},#{userSex},#{userBirthday});
</insert>

4.4 mappers
Use the resource relative to the classpath
For example:

Fully qualified class name using mapper interface class path
For example:
Note: this method requires that the mapper interface name and mapper mapping file name are the same and placed in the same directory.

Register all mapper interfaces under the specified package
For example:
Note: this method requires that the mapper interface name and mapper mapping file name are the same and placed in the same directory.