简体   繁体   English

如何使用注释配置的MyBatis指定IN参数类型

[英]How to specify IN param type with annotation-configured MyBatis

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. 如果我想能够传递空值,似乎我需要明确告诉MyBatis用于java.util.Date IN参数的数据库类型。 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)

How does one specify a parameter type when using annotations? 使用注释时如何指定参数类型?

Other developers already commented about this kind of problem. 其他开发人员已经对这种问题发表了评论。

I quote from GitHub comments: 我引用GitHub评论:

@nglsatheesh MyBatis cannot cast/convert those types unless you tell it how. @nglsatheesh MyBatis不能强制转换/转换这些类型,除非您告诉它如何。 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} 从table_name中选择*,其中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()));
        }
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从注释配置的Mybatis映射器读取生成的sql语句 - how to read generated sql statement from annotation-configured Mybatis mapper 注释配置的框架(即Tomcat,Hibernate)如何扫描类路径中的所有类以进行注释? - How do annotation-configured frameworks (ie. Tomcat, Hibernate) scan all classes in the classpath for annotations? 如何在 MyBatis 中使用带有 @Many 注释的 UUID 类型处理程序? - How to use UUID type handler with @Many annotation in MyBatis? 如何指定Mybatis的字符编码? - How to specify the character encoding of Mybatis? 如何在Jersey / Jackson中为抽象方法参数指定具体类型? - How to specify concrete type for abstract method param in Jersey/ Jackson? 在SimpleXML批注中指定集合类型 - Specify collection type in SimpleXML annotation 如何在Java中指定应将注释应用于的字段类型? - How can I specify the type of the field that an Annotation should be applied to in Java? 如何在mybatis-spring中使用@Transactional注释? - How to use @Transactional annotation in mybatis-spring? 如何在带有注释的mybatis和oracle中插入时返回ID - How to return IDs on insert in mybatis and oracle with annotation 如何为Set指定typeHandler <MyEnum> 在mybatis? - How to specify typeHandler for Set<MyEnum> in mybatis?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM