Skip to content

MyBatis的参数解析 #4

Open
Open
@bitfishxyz

Description

@bitfishxyz

MyBatis的参数解析

MyBatis的接口类的方法的参数可能有下面几种情况:

  • 单个基本类型或者基本类型的包装类作为参数
  • JavaBean作为参数
  • 多个参数
  • 使用Map容器作为参数
  • 使用List容器作为参数

上面几种情况同代码表示就是这样的,假设我们有一个Monkey类:

@Getter @Setter
@NoArgsConstructor
public class Monkey {
    private Integer id;
    private String name;
    private Integer phoneNumber;
    private Date birthday;
}

我们为它编写一个接口

public interface MonkeyDao {
    // 单个基本类型或者基本类型的包装类作为参数
    Monkey getMonkeyById(Integer id);

    // JavaBean作为参数
    Boolean insertMonkey(Monkey monkey);

    // 多个参数
    List<Monkey> getMonkeysRangeById(Integer min, Integer max);

    // 多个参数 + 注解
    List<Monkey> getMonkeysRangeByIdWithParamNames
            (@Param("min") Integer min, @Param("max") Integer max);

    // 或者我们传递一个map类型,mapper文件中接口解构参数
    List<Monkey> getMonkeysRangeByIdWithMappedParamNames
            (Map<String, Object> map);

    // 通过列表传递参数
    List<Monkey> getMonkeysByIds(List<Integer> ids);
}

上面的代码是从这个项目中截取的,
https://github.com/fish-java/MyBatisImpl

我们把上面几种情况在一个接口中体现出来的,现在我们要为我们的接口编写mapper和动态SQL语句。那么我们在xml中,如何读取到接口中的参数呢?

简单参数

如果只有一个参数,而且参数类型是基本类型,那么这种情况很简单

    Monkey getMonkeyById(Integer id);
    <select id="getMonkeyById" parameterType="Integer"
            resultType="com.github.fish.mybatis.entity.Monkey">
      select * from monkey where id = #{id}
    </select>

直接使用#{}这种语法就行了。

JavaBean

如果参数类型是JavaBean,我们可以直接的提取出它的字段。

    Boolean insertMonkey(Monkey monkey);
    <insert id="insertMonkey" parameterType="com.github.fish.mybatis.entity.Monkey"
            useGeneratedKeys="true" keyProperty="id">
      insert into monkey (name, birthday, phone_number)
          values (#{name}, #{birthday}, #{phoneNumber})
    </insert>

多个参数

如果接口中有多个参数

    List<Monkey> getMonkeysRangeById(Integer min, Integer max);

要想接受到Java方法调用时传递的多个参数,必须写成#{param1} #{param2} 的形式

    <select id="getMonkeysRangeById" resultType="com.github.fish.mybatis.entity.Monkey">
      select * from monkey
      where id > #{param1} and #{param2} > id
    </select>

注意,必须写成#{param1} #{param2}的形式,不能写成#{p1} #{p2}等,也不能写成其他的。

如果你觉得使用#{param1} #{param2}的形式不直观,那么可以在接口参数中添加注解。

    List<Monkey> getMonkeysRangeByIdWithParamNames
            (@Param("min") Integer min, @Param("max") Integer max);
    <!-- 或者我们可以在Java中通过注解来给方法的参数命名,让MyBatis智能的映射 -->
    <select id="getMonkeysRangeByIdWithParamNames" resultType="com.github.fish.mybatis.entity.Monkey">
      select * from monkey
      where id > #{min} and #{max} > id
    </select>

通过注解中定义的名称来取值,显得比较直观。

Map类型的参数

如果参数类型是Map类型的,它的用法和JavaBean类似

    List<Monkey> getMonkeysRangeByIdWithMappedParamNames
            (Map<String, Object> map);

我们可以直接的按照key来取值就行了

    <select id="getMonkeysRangeByIdWithMappedParamNames" resultType="com.github.fish.mybatis.entity.Monkey">
      select * from monkey
      where id > #{min} and #{max} > id
    </select>

List类型的参数

如果参数类型是JavaBean,我们可以直接的提取出它的字段。

    List<Monkey> getMonkeysByIds(List<Integer> ids);
    <select id="getMonkeysByIds" resultType="com.github.fish.mybatis.entity.Monkey">
      select * from monkey
      where id = #{list[0]} or id = #{list[1]}
    </select>

按照#{list[0]} #{list[1]}来取值。

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions