如何使用注释配置的MyBatis指定IN param类型 [英] How to specify IN param type with annotation-configured MyBatis

查看:516
本文介绍了如何使用注释配置的MyBatis指定IN param类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想传递空值,我需要明确告诉MyBatis用于java.util.Date IN参数的db-type。但我找不到这样做的方法。

It seems I need to explicitly tell MyBatis what db-type to use for java.util.Date IN parameters if I want to able to pass null values. But I can't find a way of doing that.

我尝试了以下不同的变种而没有运气:

I tried different variations of following with no luck:

@Select("<script>SELECT ... WHERE ... " +
    "<if test='#{dateFrom,jdbcType=TIMESTAMP} != null'>" +
    "  AND date &gt; #{dateFrom,jdbcType=TIMESTAMP}" + 
    "</if></script>")
List<MyType> getRecords(@Param("dateFrom") dateFrom)

如何指定参数类型什么时候使用注释?

推荐答案

其他开发者已经就此类问题发表了评论。

Other developers already commented about this kind of problem.

  • MyBatis it wont override the JdbcType if i specify the JdbcType in the paramete #139

我引用GitHub评论:


@nglsatheesh MyBatis无法转换/转换这些类型,除非你告诉它如何。
所有你需要的是一个简单的自定义类型处理程序。

@nglsatheesh MyBatis cannot cast/convert those types unless you tell it how. All you need is a simple custom type handler.



public class StrToIntTypeHandler implements TypeHandler<String> {
  @Override
  public void setParameter(PreparedStatement ps, int i,
      String parameter, JdbcType jdbcType) throws SQLException {
    ps.setInt(i, Integer.parseInt(parameter));
  }
  // other methods are for binding query results.
}




select * from table_name where id =#{ value,typeHandler = StrToIntTypeHandler}

select * from table_name where id = #{value,typeHandler=StrToIntTypeHandler}

现在,如果您要创建这样的自定义类型处理程序:

So now, if you will create such a custom typehandler:

public class Null2DateTypeHandler implements TypeHandler<Date> {

    @Override
    public void setParameter(PreparedStatement ps, int i, java.util.Date parameter, JdbcType jdbcType) throws SQLException {
        System.err.println(String.format("ps: %s, i: %d, param: %s, type: %s", ps.toString(), i, parameter, jdbcType.toString()));

        if (parameter == null) {
            ps.setDate(i, null); // ??? I'm not sure. But it works.
        } else {
            ps.setDate(i, new java.sql.Date(parameter.getTime()));
        }
    }
}

并且,mapper方:

And, mapper side:

@Select({
    "<script>"
    , "SELECT * FROM `employees` WHERE `hire_date` "
    , "  BETWEEN
    , "  #{dateFrom,typeHandler=*.*.*.Null2DateTypeHandler}"
    , "  AND"
    , "  #{dateTo,typeHandler=*.*.*.Null2DateTypeHandler}"      
    ,"</script>"
})
@Results({
      @Result(property = "empNo", column = "emp_no"),
      @Result(property = "birthDate", column = "birth_date"),
      @Result(property = "firstName", column = "first_name"),
      @Result(property = "lastName",  column = "last_name"),
      @Result(property = "gender",    column = "gender"),
      @Result(property = "hireDate",  column = "hire_date")          
})  
List<Employees> selectBetweenTypeHandler(@Param("dateFrom") Date dateFrom, @Param("dateTo") Date dateTo);

我的记录它看起来工作正常。

My logging, it looks working fine.

DEBUG [main] - ==>  Preparing: SELECT * FROM `employees` WHERE `hire_date` BETWEEN ? AND ? 
ps: org.apache.ibatis.logging.jdbc.PreparedStatementLogger@369f73a2, i: 1, param: null, type: OTHER
DEBUG [main] - ==> Parameters: null, null
ps: org.apache.ibatis.logging.jdbc.PreparedStatementLogger@369f73a2, i: 2, param: null, type: OTHER
DEBUG [main] - <==      Total: 0

这篇关于如何使用注释配置的MyBatis指定IN param类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆