Reputation: 6349
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)
})
Upvotes: 4
Views: 13704
Reputation: 131
Mind that you need to add mybatis @Param
annotation to the constructor in case you want to use @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)
})
Upvotes: 1
Reputation: 3100
Just completing the answer of @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:
@ConstructorArgs({
@Arg(column = "id", javaType = long.class)
,@Arg(column = "create", javaType = Date.class)
,@Arg(column = "content", javaType = String.class)
})
Upvotes: 0
Reputation: 409
You can use the @ConstructorArgs
instead as follows:
@ConstructorArgs({
@Arg(column = "id", javaType = Long.class)
,@Arg(column = "create", javaType = Date.class)
,@Arg(column = "content", javaType = String.class)
})
Upvotes: 3
Reputation: 13048
MyBatis expects your model objects to have a no-arguments constructor (and possibly setters for each mapped field). Add those and everything should work.
Upvotes: 21