简体   繁体   English

MyBatis - 找不到构造函数

[英]MyBatis - No constructor found

I have a problem with MyBatis mapping. MyBatis 映射有问题。 I have a domain class like this:我有一个这样的域类:

public class MyClass
{
   private Long id;
   private Date create;
   private String content;

   MyClass (Long id, Date create, String content)
   {
       this.id = id;
       this.create = create;
       this.content = content;
   }

   //getters and setters

A mapper class with a method like this:具有如下方法的映射器类:

   @Select("SELECT * FROM MyTable WHERE id=#{id}")
   MyClass getMyClass (@Param("id") Long id);

In the database the three columns are of type Number, Timestamp and Clob and have the same name as in the class fields.在数据库中,三列的类型为 Number、Timestamp 和 Clob,并且与类字段中的名称相同。

When I use this method I get a: ExecutorException: No constructor found in [MyClass;当我使用这个方法时,我得到一个: ExecutorException: No constructor found in [MyClass; matching [java.math.BigDecimal, java.sql.Timestamp, oracle.jdbc.OracleClob]匹配 [java.math.BigDecimal, java.sql.Timestamp, oracle.jdbc.OracleClob]

But if I remove the constructor from Myclass, then there is no problem at all .但是如果我从 Myclass 中删除构造函数,那么就完全没有问题了 I would like to have the constructor, how can I fix it?我想要构造函数,我该如何解决? I tried adding the @Results annotation in the mapper like so, but it didn't make any difference:我尝试像这样在映射器中添加 @Results 注释,但它没有任何区别:

   @Results(value = {
      @Result(column = "id", property = "id", javaType = Long.class),
      @Result(column = "create", property = "create", javaType = Date.class),
      @Result(column = "content", property = "content", javaType = String.class)
   })

MyBatis expects your model objects to have a no-arguments constructor (and possibly setters for each mapped field). MyBatis 期望你的模型对象有一个无参数的构造函数(可能还有每个映射字段的 setter)。 Add those and everything should work.添加这些,一切都应该有效。

You can use the @ConstructorArgs instead as follows:您可以使用@ConstructorArgs代替,如下所示:

@ConstructorArgs({
    @Arg(column = "id", javaType = Long.class)
    ,@Arg(column = "create", javaType = Date.class)
    ,@Arg(column = "content", javaType = String.class)
})

Just completing the answer of @Kazuki Shimizu .刚刚完成@Kazuki Shimizu的回答。

If you want to use the primitive type long instead of the wrapper Long in the constructor you need to change the binding to:如果要在构造函数中使用基本类型long而不是包装器Long ,则需要将绑定更改为:

@ConstructorArgs({
   @Arg(column = "id", javaType = long.class)
   ,@Arg(column = "create", javaType = Date.class)
   ,@Arg(column = "content", javaType = String.class)
})

Mind that you need to add mybatis @Param annotation to the constructor in case you want to use @ConstructorArgs .请注意,您需要在构造函数中添加@Param注释,以防您想使用@ConstructorArgs So your constructor would look like:所以你的构造函数看起来像:

public class MyClass
{
   private long id;
   private Date create;
   private String content;

   MyClass (@Param("id") long id, @Param("create") Date create, @Param("content") String content)
   {
       this.id = id;
       this.create = create;
       this.content = content;
   }

Then your mapper:然后你的映射器:

@ConstructorArgs({
   @Arg(name = "id", column = "id", javaType = long.class),
   @Arg(name = "create", column = "create", javaType = Date.class),
   @Arg(name = "content", column = "content", javaType = String.class)
})

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM