Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mybatis dynamic sql inside annotation

Tags:

mybatis

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.

like image 208
Arpan Solanki Avatar asked Sep 15 '25 08:09

Arpan Solanki


1 Answers

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

like image 50
jddsantaella Avatar answered Sep 17 '25 14:09

jddsantaella