MyBatis-找不到构造函数 [英] MyBatis - No constructor found

查看:410
本文介绍了MyBatis-找不到构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MyBatis映射有问题. 我有一个像这样的域类:

I have a problem with MyBatis mapping. 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);

在数据库中,三列的类型分别为Number,Timestamp和Clob,并且名称与类字段中的名称相同.

In the database the three columns are of type Number, Timestamp and Clob and have the same name as in the class fields.

当我使用这种方法时,我得到: ExecutorException:在[MyClass;中找不到构造函数;匹配[java.math.BigDecimal,java.sql.Timestamp,oracle.jdbc.OracleClob]

When I use this method I get a: ExecutorException: No constructor found in [MyClass; matching [java.math.BigDecimal, java.sql.Timestamp, oracle.jdbc.OracleClob]

但是如果我从Myclass中删除了构造函数,那么就没有问题了.我想要构造函数,该如何解决? 我尝试像这样在映射器中添加@Results批注,但没有任何区别:

But if I remove the constructor from Myclass, then there is no problem at all. 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(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期望您的模型对象具有无参数的构造函数(并且可能有每个映射字段的设置器).添加这些,一切都将正常工作.

MyBatis expects your model objects to have a no-arguments constructor (and possibly setters for each mapped field). Add those and everything should work.

这篇关于MyBatis-找不到构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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