Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

MyBatis passes multiple parameters


May 16, 2021 MyBatis


Table of contents


Method 1: Use the map interface to pass parameters

Strictly speaking, map is suitable for almost all scenarios, but we don't use much. There are two reasons: first, map is a set of key values corresponding to the user to read its keys, in order to understand its role;

public List<Role> findRolesByMap(Map<String, Object> parameterMap);
<select id="findRolesByMap" parameterType="map" resultType="role">
    select id, role_name as roleName, note from t_role where role_name like concat('%', #{roleName}, '%') and note like concat('%', #{note}, '%')
</select>

Method two: Use annotations to pass multiple parameters

MyBatis provides developers with an annotation @Param (org.apache.ibatis.annotations.Param), which defines the parameter names of the maple, uses it to get better readability This time you need to modify the code of the mapping file, you don't need to give the parameterType property, so that MyBatis automatic exploration can greatly improve readability, user convenience, but this can cause a hassle. If SQL is complex and has more than 10 parameters, then the interface method has more parameters, which is not easy to use, but don't worry, MyBatis also provides the form of passing Java Beans.

public List<Role> findRolesByAnnotation(@Param("roleName") String rolename, @Param("note") String note);
<select id="findRolesByAnnotation" resultType="role">
    select id, role_name as roleName, note from t_role where role_name like concat('%', #{roleName}, '%') and note like concat('%', #{note}, '%')
</select>

Method 3: Pass multiple parameters through the Java Bean

public List<Role> findRolesByBean(RoleParams roleParam);
<select id="findRolesByBean" parameterType="com.xc.pojo.RoleParams" resultType="role">
    select id, role_name as roleName, note from t_role where role_name like concat('%', #{roleName}, '%') and note like concat('%', #{note}, '%')
</select>

Method 4: Mixed use

In some cases, you may need to use a mixture of several methods to pass parameters. For example, querying a role can be done with role names and notes, while also supporting pedding

public List<Role> findByMix(@Param("params") RoleParams roleParams, @Param("page") PageParam PageParam);
<select id="findByMix" resultType="role">
    select id, role_name as roleName, note from t_role
    where role_name like concat('%', #{params.roleName}, '%') and note like concat('%', #{params.note}, '%') limit #{page.start}, #{page.limit}
</select>

Summarize:

Four methods of passing multiple parameters are described, and the methods are reviewed and summarized to benefit our application in practice.

The use of map pass parameters leads to a loss of business readability, which leads to difficulties in subsequent scaling and maintenance, and is decisively abandoned in real-world applications.

Using @Param multiple parameters through annotations, which are affected by the number of parameters (n). This is the best way to pass parameters when n≤5, which is better than using Java Beans because it is more intuitive, and when n>5, multiple parameters make the call difficult, which is not recommended at this time.

The Java Bean method is recommended when there are more than 5 parameters.

For the use of mixed parameters, to clarify the rationality of the parameters.