简体   繁体   中英

How to add variables to xml properties file?

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;
}

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM