mybatis 多个接口参数的注解使用方式(@Param)

1 简介

1.1 单参数

Mybatis 中, 很多时候, 我们传入接口的参数只有一个。 对应接口参数的类型有两种, 一种是基本的参数类型, 一种是 JavaBean

例如在根据主键获取对象时, 我们只需要传入一个主键的参数即可。 而在插入, 更新等操作时, 一般会涉及到很多参数, 我们就使用 JavaBean

1.2 多参数

但是, 在实际的情况中, 我们遇到类似这样的情况可能:

  1. 接口需要使用的参数多于一个;
  2. 接口需要使用的参数又远少于对应 JavaBean 的成员变量, 或者需要多个 JavaBean 对象;
  3. 或者需要使用的参数对应 JavaBean 没有相应的成员变量。

比如 获取一段时间产生的日志信息, 日志对应的 JavaBean 只有一个日期, 那我们使用该 JavaBean 就无法满足我们的要求。

又比如我们进行模糊搜索, 搜索条件只有两个, 但对应的 JavaBean50+ 个成员变量, 那创建对应的 JavaBean 就过于浪费了。

对此, 我知道的有如下几种方法

2 多个接口参数的两种使用方式

2.1 Map 方法(不推荐)

Map 方法的使用很简单, 就是将对应的参数以 key-value 的方式存储, key 对应 SQL 中的参数名字, value 对应需要传入的参数值。

以获取一段时间内存储的用户为例

2.1.1 创建接口方法

     /**
     * 获取一段时间内的用户
     * @param params
     * @return
     */
    List<Student> selectBetweenCreatedTime(Map<String, Object> params);

该方法返回的是多个记录, 因此使用 List 作为返回值。

2.1.2 配置对应的SQL

  <select id="selectBetweenCreatedTime" parameterType="java.util.Map" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from student
    where gmt_created &gt; #{bTime, jdbcType=TIMESTAMP} and gmt_created &lt; #{eTime, jdbcType=TIMESTAMP}
  </select>

id 与 之前创建的方法名一样。

2.1.3 调用

@Test
public void testSelectBtweenCreatedTimeMap() {

    Map<String, Object> params = new HashMap<>();
    Calendar bTime = Calendar.getInstance();
    // month 是从0~11, 所以9月是8
    bTime.set(2018, Calendar.AUGUST, 29);
    params.put("bTime", bTime.getTime());

    Calendar eTime = Calendar.getInstance();
    eTime.set(2018,Calendar.SEPTEMBER,2);
    params.put("eTime", eTime.getTime());
    SqlSession sqlSession = null;
    try {
        sqlSession = sqlSessionFactory.openSession();

        StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
        List<Student> students = studentMapper.selectBetweenCreatedTime(params);
        for (int i = 0; i < students.size(); i++) {
            System.out.println(students.get(i));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

}

调用方法很简单, 传入相应的 Map 参数即可。 此时, Map 中的 key 对应。 因此, 在此例子中传入的参数

  1. 传入一个 keybtime 的时间, 作为开始时间;
  2. 传入一个 keyetime 的时间, 作为结束时间;

2.2 @Param 方法(推荐)

@Param 方法就是使用注解的方式,

2.2.1 创建接口方法

/**
 * 获取指定时间内的对象
 * @param pbTime 开始时间
 * @param peTime 结束时间
 * @return
 */
List<Student> selectBetweenCreatedTimeAnno(@Param("bTime")Date pbTime, @Param("eTime")Date peTime);

@Param(“bTime”) 就是告诉 mybatis , 参数 pbTime 在 SQL 语句中用 bTime 作为 key

也就是说, mybatis 帮我们完成了调用时, 类似 params.put(“bTime”, pbTime) 这个过程。

2.2.2 配置 SQL 语句

  <select id="selectBetweenCreatedTimeAnno" resultMap="BaseResultMap">
    select
    <include refid="Base_Column_List" />
    from student
    where gmt_created &gt; #{bTime, jdbcType=TIMESTAMP} and gmt_created &lt; #{eTime, jdbcType=TIMESTAMP}
  </select>

此处的 bTime 对应** @Param(“bTime”)** 中的 bTime, 需要完全一致。

eTime 也是一样。

2.2.3 调用

在调用时, 不需要创建 Map 了, 只需要按参数提示传入对应的实际参数即可。

 @Test
public void testSelectBtweenCreatedTimeAnno() {

    Map<String, Object> params = new HashMap<>();
    Calendar bTime = Calendar.getInstance();
    // month 是从0~11, 所以9月是8
    bTime.set(2018, Calendar.AUGUST, 29);


    Calendar eTime = Calendar.getInstance();
    eTime.set(2018,Calendar.SEPTEMBER,2);

    SqlSession sqlSession = null;
    try {
        sqlSession = sqlSessionFactory.openSession();

        StudentMapper studentMapper = (StudentMapper) sqlSession.getMapper(StudentMapper.class);
        List<Student> students = studentMapper.selectBetweenCreatedTimeAnno(bTime.getTime(), eTime.getTime());
        for (int i = 0; i < students.size(); i++) {
            System.out.println(students.get(i));
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (sqlSession != null) {
            sqlSession.close();
        }
    }

}

3 @Param 的优势

Map 方式的缺点就是需要手动创建 Map, 并对 SQL 中的参数进行赋值。其缺点:

  1. 手动创建 Map 这个过程很不简洁, 看着很繁琐。
  2. 手动对参数进行赋值, 很容易出错。 比如本来是要 params.put(“bTime”, bTime) 可能会不小心写成 params.put(“bime”, bTime), 但是这个时候编译器并不会提示。

相比于 Map 方式, 使用 @Param 时, 我们在使用上就像调用方法一样, 传入对应的实际参数即可。 调用时基本不会出错。

4 Github

相应代码, 可以访问 我的Github-helloMybatis

  • 7
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值