Arpan Solanki
Arpan Solanki

Reputation: 855

mybatis dynamic sql inside annotation

I am using mybatis 3. I am using @select annotation to write the select query inside the mapper iterface.

Example:

@Select("select * from EMPLOYEE where ID>55")
public List<Employee> getEmployees();

Is there a way i can construct the query dynamically and pass it to the annotation. I found examples to do that in xml way but not with annotations. Is it possible to write dynamic queries with annotatoins? If yes, then how to do it.

To clarify the question i am not asking about passing an ID but constructing a dyanmic where statement.

Thanks.

Upvotes: 4

Views: 6475

Answers (2)

omiel
omiel

Reputation: 1593

With MyBatis 3, you can now have dynamic SQL in annotations (see this note).

Upvotes: 4

jddsantaella
jddsantaella

Reputation: 3687

AFAIK the @Select annotation doesn't support dynamic SQL. I think it is due to some limitations of Java Annotations.

In the documentation of myBatis you can read:

The annotations are a lot cleaner for simple statements, however, Java Annotations are both limited and messier for more complicated statements. Therefore, if you have to do anything complicated, you're better off with XML mapped statements

Upvotes: 4

Related Questions