I would like to check if columnName
is in table T
in database. I am using spring boot and mybatis.
This checking should be done exactly once, to not do overhead in network transfer and time.
Why ?
In my mybatis SQL query
I have SELECT * FROM T .. ORDER BY ${columnName}
. You know that it is prone to SQL Injection. So I must check if columnName
is proper column name in table.
Approach 1
Hard Code in app columns of table T
.
Approach 2
List <String> columnNames;
@PostConstruct
void initColumnNames() {
columnNames = mapper.getColumnNames();
}
Approach 3
in DAO (or mayby validation class) ?:
void someDaoMethod(String columnName) {
if (columnNames == null) { //using approach 2 this if is unneccessary
columnNames = mapper.getColumnNames();
}
//check if columName is allowed (= is in columnNames)
}
Can you help me solve this problem ? What approach should I choose ?