1

I'm trying to externalize SQL statements for usage with spring, as advised in https://stackoverflow.com/a/24141382/1194415.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>
    <entry key="SQL_MAX_ID">
        <![CDATA[
            SELECT MAX(id) FROM mytable
        ]]>
    </entry>
</properties>

Question: when having multiple sql statements, it would be nice to define mytable only once, and then refer to it as some kind of variable.

Is that possible in a simple properties file?

I'm loading the file as follows:

@Bean
public PropertiesFactoryBean sql() {
    PropertiesFactoryBean bean = new PropertiesFactoryBean();
    bean.setLocation(new ClassPathResource("sql.xml"));
    return bean;
}
membersound
  • 74,158
  • 163
  • 522
  • 986

1 Answers1

0

The recommended way to store properties in spring projects is to use property file, in your property file application.properties file under src/main/resources and have your property defined there

table=mytable 

Then use JSTL to access it in your xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

<properties>
  <entry key="SQL_MAX_ID">
    <![CDATA[
        SELECT MAX(id) FROM ${table}
    ]]>
</entry>
</properties>
Amer Qarabsa
  • 6,108
  • 3
  • 19
  • 38
  • Does not work unfortunately. The `${table}` statement remains as is and is not replaced by content is application.properties. Maybe I have to load the file in a different way (see the `@Bean` in my question)? – membersound Sep 25 '17 at 12:54