codecompleting
codecompleting

Reputation: 9621

Possible to avoid code changes during schema changes when using Jdbctemplate?

When using spring's JdbcTemplate, I am using the row mapper to map results coming back.

The benefit with this is that there are less places where I have to change my code if I change my mysql schema etc.

Are there any other tips on how to minimize changes in code when adding/removing columns in mysql?

Upvotes: 0

Views: 218

Answers (2)

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340883

If you are retrieving columns by name (SELECT col1, col2, col3) you will be immune to adding and rearranging of columns. Never use SELECT *.

However if you are removing columns, you have no choice. In fact, how was this suppose to work? Previously you fetched e.g. price column and used it in your business layer. Now the column does not exist - how to handle this?

But adding columns is safe, unless new columns are non-nullable. In this case you will have a problem when adding new records, since VALUES statement won't include new columns. Optional columns are fine.

Upvotes: 1

Victor Parmar
Victor Parmar

Reputation: 5789

One tip is to not do SELECT *, select on specific columns so in case you add stuff you don't break your code :)

Upvotes: 0

Related Questions