Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass interval in mybatis from annotations?

I am using myBatis through annotations to fetch data from my server. Trying to get the data n days old, the annotation :

@Select("SELECT o.title from user_order o where current_date - date_trunc('day', o.dateoforder) < '#{n} days'")
ArrayList<OrderRecord> getOrderHistory(@Param("n") int n);

returns error:

The column index is out of range: 1, number of columns: 0. Error querying database. Cause: org.postgresql.util.PSQLException: The column index is out of range: 1, number of columns: 0.

Also,

@Select("SELECT o.title from user_order o where current_date - date_trunc('day', o.dateoforder) < #{n}")
ArrayList<OrderRecord> getOrderHistory(@Param("n") String n);

produces the similar error when n is something like "5 days".

What data-type is being expected?

I am using PostgreSQL.

like image 822
Manu Avatar asked Jan 27 '26 09:01

Manu


1 Answers

Mybatis is expecting an interval parameter and cannot cast the integer or String into it automatically.

An object of the type PGInterval needs to be passed.

PGInterval pginterval = new PGInterval("5 days");

and the annotation must be:

@Select("SELECT o.title from user_order o where current_date - date_trunc('day', o.dateoforder) < #{n}")
ArrayList<OrderRecord> getOrderHistory(@Param("n") PGInterval n);
like image 154
Manu Avatar answered Jan 29 '26 22:01

Manu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!