How to configure parameter mapping in the SQL mapping file in MyBatis and how to use it

How to configure parameter mapping in the SQL mapping file in MyBatis and how to use it

MyBatis is an open source Java persistence framework, which can automatically map data in the database to Java objects, and make Java objects can be stored in the database very conveniently. In MyBatis, the SQL mapping file is a very important part. It can provide a mapping relationship between Java objects and database tables, and can configure parameter mapping so that the properties of Java objects can be mapped to the parameters of SQL statements. This article will introduce the parameter mapping configuration and usage of the SQL mapping file in MyBatis.

insert image description here

Parameter mappings in SQL mapping files

In the SQL mapping file, parameter mapping refers to mapping the properties of Java objects to the parameters of SQL statements. In MyBatis, parameter mapping can be configured in the following two ways:

Use #{} placeholders

In SQL statements, we can use #{} placeholders to represent parameters. For example, suppose we have a User class with an id attribute and a name attribute:

public class User {
    
    
    private int id;
    private String name;

    // 省略 getter 和 setter 方法
}

We can use #{} placeholders in the SQL statement to denote the id and name parameters:

<select id="getUserById" resultType="User">
    SELECT * FROM users WHERE id = #{id} AND name = #{name}
</select>

When executing this SQL statement, MyBatis will automatically set the values ​​of the id and name parameters to the #{} placeholder in the SQL statement.

Use ${} placeholders

In addition to #{} placeholders, we can also use KaTeX parse error: Expected 'EOF', got '#' at position 15: {} placeholders to represent parameters. Different from the #̲{} placeholder, the {} placeholder directly replaces the value of the parameter into the SQL statement. For example, suppose we have a Page class that contains an offset property and a limit property:

public class Page {
    
    
    private int offset;
    private int limit;

    // 省略 getter 和 setter 方法
}

We can use the ${} placeholder in the SQL statement to represent the offset and limit parameters:

<select id="getUsersByPage" resultType="User">
    SELECT * FROM users LIMIT ${offset}, ${limit}
</select>

When executing this SQL statement, MyBatis will directly replace the values ​​of the offset and limit parameters into the ${} placeholder in the SQL statement.

Use parameter type aliases

In the SQL mapping file, we can use parameter type aliases to simplify the configuration of parameter mapping. Parameter type aliases allow us to represent a Java type with a short name.

For example, we can define a parameter type alias of type Page in the following way:

<typeAliases>
    <typeAlias type="com.example.Page" alias="page" />
</typeAliases>

Then, in the SQL statement, we can use #{page.offset} and #{page.limit} to represent the offset and limit properties of the Page type:

<select id="getUsersByPage" resultType="User">
    SELECT * FROM users LIMIT #{page.offset}, #{page.limit}
</select>

Parameter mappings in SQL mapping files use

After configuring the parameter mapping in the SQL mapping file, we can use them in the following two ways:

Use Mapper interface method parameters

In the Mapper interface method, we can define a parameter to receive the parameters configured in the SQL mapping file. For example, suppose we have a UserMapper interface that contains a getUserById method:

public interface UserMapper {
    
    
    User getUserById(@Param("id") int id, @Param("name") String name);
}

In this method, we use the @Param annotation to specify the name of the parameter. This name should be the same as the parameter name in the SQL mapping file so that MyBatis can correctly map the parameter into the SQL statement.

Using dynamic SQL tags

In addition to using Mapper interface method parameters, we can also use dynamic SQL tags to use parameter mapping in SQL mapping files. Dynamic SQL tags can dynamically generate SQL statements according to conditions.

For example, suppose we have a UserMapper interface that contains a getUsersByPage method:

public interface UserMapper {
    
    
    List<User> getUsersByPage(@Param("page") Page page);
}

In this method, we use the @Param annotation to specify the name of the parameter. Then, in the SQL mapping file, we can use if tags to generate dynamic SQL statements based on the values ​​of the offset and limit attributes:

<select id="getUsersByPage" resultType="User">
    SELECT * FROM users
    <if test="page.offset != null and page.limit != null">
        LIMIT #{page.offset}, #{page.limit}
    </if>
</select>

In this SQL mapping file, we use the if tag to determine whether the value of the offset and limit attributes is empty. If none of them are empty, a LIMIT clause is generated.

sample code

The following is a sample code of a complete MyBatis SQL mapping file, including the configuration and usage of parameter mapping:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.example.UserMapper">

    <typeAliases>
        <typeAlias type="com.example.Page" alias="page" />
    </typeAliases>

    <select id="getUserById" resultType="User">
        SELECT * FROM users WHERE id = #{id} AND name = #{name}
    </select>

    <select id="getUsersByPage" resultType="User">
        SELECT * FROM users
        <if test="page.offset != null and page.limit != null">
            LIMIT #{page.offset}, #{page.limit}
        </if>
    </select>

</mapper>
public class User {
    
    
    private int id;
    private String name;

    // 省略 getter 和 setter 方法
}

public class Page {
    
    
    private int offset;
    private int limit;

    // 省略 getter 和 setter 方法
}

public interface UserMapper {
    
    
    User getUserById(@Param("id") int id, @Param("name") String name);

    List<User> getUsersByPage(@Param("page") Page page);
}

In this sample code, we define a User class and a Page class, where the User class contains an id attribute and a name attribute, and the Page class contains an offset attribute and a limit attribute. Then, we define a UserMapper interface, which contains a getUserById method and a getUsersByPage method.

In the SQL mapping file, we use the parameter type alias to define a Page type parameter type alias. Then, in the getUserById method, we used #{} placeholders to represent the id and name parameters, and in the getUsersByPage method, we used the if tag to generate dynamic SQL statements based on the values ​​of the offset and limit attributes.

Summarize

In MyBatis, parameter mapping is a very important concept. By configuring parameter mapping, we can map the properties of the Java object to the parameters of the SQL statement. When the SQL statement is executed, MyBatis will automatically set the value of the parameter to the SQL statement. In MyBatis, parameter mapping can be configured through #{} placeholders, ${} placeholders and parameter type aliases. When using parameter mapping, we can use them using Mapper interface method parameters or dynamic SQL tags.

Guess you like

Origin blog.csdn.net/2302_77835532/article/details/131654131