温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-10-12 07:48:38 来源:脚本之家 阅读:148 作者:阿进的写字台 栏目:编程语言

1 简介

1.1 单参数

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

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

1.2 多参数

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

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

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

又比如我们进行模糊搜索, 搜索条件只有两个, 但对应的 JavaBean 有 50+ 个成员变量, 那创建对应的 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 > #{bTime, jdbcType=TIMESTAMP} and gmt_created < #{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. 传入一个 key 为 btime 的时间, 作为开始时间;
  2. 传入一个 key 为 etime 的时间, 作为结束时间;

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 > #{bTime, jdbcType=TIMESTAMP} and gmt_created < #{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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI