SpringBoot 使用 MyBatis 时传参规范

1,564 阅读4分钟

这是我参与2022首次更文挑战的第20天,活动详情查看:2022首次更文挑战

使用Mybatis作为持久层框架时,对于数据库的增删改查等操作都需要参数的传递,这里学习记录一下MyBatis中可用的参数传递方式。

1. 单个简单参数传递

使用 MyBatis 传递单个参数时比较简单,形式如#{a},#{b},#{param1}等都可以在 MyBatis 中获取到唯一参数,但是参数名尽量和入参名一致,这样更符合开发规范。

Mapper 文件定义

UserInfo selectById(String userId);

xml 文件定义

  • 单个参数时,标签中可不定义参数类型
<select id="selectByUserId" resultType="com.shone.entity.UserInfo">
    select user_id, user_name from user_info where user_id=#{userId}
</select>

2. 匿名参数顺序传递

匿名参数传递时使用的是 MyBatis 中索引传递的方式,对于传递的多个参数,在sql语句标签中使用#{param1}#{param2}等索引依次代表传入的参数值。

Mapper 文件

  • 使用匿名参数传递时,接口中定义的方法参数无需使用注解
UserInfo selectByNameAndAge(String userName, Integer age);

xml 文件

  • xml中读取参数时可以使用使用arg0, arg1, param1, param2等形式
  • 参数有多个类型且匿名,因此不需要指定入参类型
  • Mapper中没有使用注解,因此直接使用参数名时会报错参数名找不到
<select id="selectByNameAndAge" resultType="com.shone.entity.UserInfo">
    select user_id, user_name from user_info where user_name=#{param1} and user_gender=#{param2}
</select>
  • 匿名参数传递方法可读性差,对后期维护不友好,不符合开发规范。

3. 使用@Param注解传递

@Param注解用于指定key,指定了key之后,在sql标签中可以使用key来索引对应的参数值。

Mapper 文件

  • 接口中定义时使用 @Param注解指定参数对应的key
UserInfo selectByNameAndAge(@Param("userName") String userName, @Param("age") Integer age);

xml 文件

  • 不需要指定传入参数类型
  • 使用注解指定的key就可以获取到对应入参值
<select id="selectByNameAndAge" resultType="com.shone.entity.UserInfo">
    select user_id, user_name from user_info where user_name=#{userName} and user_gender=#{age}
</select>

@Param注解传参使用时清晰简洁明了,符合开发规范,在参数较少时建议使用该方式。

4. 使用Map传递参数

MyBatis框架底层就是将入参转换成Map类型进行传递的,因此我们也可以使用Map进行参数的传递,传入的参数在sql标签中可以根据Map中的key值直接获取。

使用时,需要将传入的参数放入到map中

  map.put("userName","tom");
  map.put("gender",1);

Mapper 文件

UserInfo selectByNameAndAge(Map<String, Object> map);

xml 文件

  • 入参类型指定为Map
<select id="selectByNameAndAge" resultType="com.shone.entity.UserInfo" paramterType="map">
    select user_id, user_name from user_info where user_name=#{userName} and user_gender=#{age}
</select>

使用Map传递参数符合MyBatis底层的设计,性能也没有问题,但是在开发中由于将所有值封装成为map,无法直观看到map中可能含有的参数,在后期维护上不太友好。

5. 使用JavaBean(POJO)传递

除了Map方式,还可以使用JavaBean的方式来传递多个参数,此时在sql标签中直接使用bean对象的属性来索引参数值,注意bean对象中 需要有相应参数的get方法,否则无法正常获取。

使用javabean作为参数,需要先定义JavaBean

@Data
public class UserInfo {
  private String userName;
  private Integer age;
}

Mapper 文件

UserInfo selectByEntity(UserInfo userInfo);

xml 文件

  • 指定入参类型为相应的JavaBean全路径限定类名
<select id="selectByNameAndAge" resultType="com.shone.entity.UserInfo" paramterType="com.shone.entity.UserInfo">
    select user_id, user_name from user_info where user_name=#{userName} and user_gender=#{age}
</select>

JavaBean的参数传递方式更符合项目开发的规范,实际使用时简洁明了,在参数较多时建议使用这种方式,创建JavaBean来作为参数传递对象。

6. 使用List、Array、Set传递

List、Array、Set等方式的传参通常用于sql标签中的in操作,即用于MyBatis中的标签。

使用list或array时,一般是做in操作,只有一个参数。

Mapper 文件

UserInfo selectByIdList(List<String> userIdList);

xml 文件

  • 不需要指定入参类型,或指定为List
  • 使用标签处理sql中的in操作
  • 如果参数是list,则collection为list;如果参数类型是array,则collection是array
  • 如果是多个参数或者是对象的属性值作为list,则传参应为map/bean类型,collection应为对象属性中的list
<select id="selectByIdList" resultMap="userResultMap">
    select * from user_info where status=1
    and user_id in
    <foreach collection="list" item="item" open="(" separator="," close=")">
        #{item}
    </foreach>
</select>