Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MyBatis - No constructor found

Tags:

java

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);

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

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

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)
   })
like image 929
enkara Avatar asked Nov 29 '22 00:11

enkara


1 Answers

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

like image 88
agnul Avatar answered Dec 06 '22 08:12

agnul