mk-tool
mk-tool

Reputation: 345

variable table name in Spring boot

I have tables like "operation_log_202001", "operation_log_202002", ...

(I know this table structure and system structure is so bad. But I have to maintain it.)

When I try to access the "user" table, I can connect to the "user" table easily because it doesn't have any variable part in the table name.

I tried to solve this problem as below.

@Query("select * from :table_name limit 20")
Flux<ReviewData> findAll(String table_name);

But the generated query is "select * from 'operation_log_202001' limit 20". I don't wanna it to append "'" to the table name.

May I ask how to solve this problem?

Upvotes: 1

Views: 464

Answers (2)

Grim
Grim

Reputation: 1986

Create a view, you can change it in runtime and can be mapped to an entity.

Elaborate:

A Database stores informations in tables. Those informations can be aggregated into views using custom SQL-queries. Views can act like tables, and inside their aggregation-query you are able to change the query's table-names - this way the table-name is variable.

Upvotes: 2

Mattyas yahya
Mattyas yahya

Reputation: 53

Try with database name before table name, example :

Select * from db_log.table_name limit 20;

Upvotes: 0

Related Questions