简体   繁体   English

如何将变量添加到xml属性文件?

[英]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 . https://stackoverflow.com/a/24141382/1194415所建议的,我正在尝试将SQL语句外部化以供spring使用。

<?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. 问题:当有多个sql语句时,最好只定义一次mytable ,然后将其称为某种变量。

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 建议在Spring项目中存储属性的方法是使用属性文件,该属性文件位于src / main / resources下的属性文件application.properties文件中,并在那里定义了属性

table=mytable 

Then use JSTL to access it in your xml 然后使用JSTL在您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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