如何在spring boot中为sql查询创建propirty文件?

时间:2017-01-06 05:08:47

标签: spring-boot properties

我的java代码中有一些sql查询。现在我想将它移动到属性文件。但我想创建另一个属性文件来存储我的SQL查询。我试过创建:

sample.properties

并写入此文件:

sql.getCallDetail = //sql query

然后在java类中:

PropertySource(value = "sample.properties")
public class ImeiBuilderDefault implements ImeiBuilder {

@Value("${sql.getCallDetail}")
    private String getCallDetail;

但我的项目没有建成。

1 个答案:

答案 0 :(得分:0)

请仔细阅读

 ** sql.properties **

queries.ex1="select * from ex1"
queries.ex2="select * from ex2"

@PropertySource(value="classpath:sql.properties")
@Configuration
@ConfigurationProperties
public class SqlQueries {

    private Map<String,String> queries;
//getters and setters
}

**TEST CLASS **

@Component
public class TestSQLQueries {

    @Autowired
    private SqlQueries queries;

    @PostConstruct
    public void init(){

        queries.getQueries().entrySet().stream().forEach(entry->{
            System.out.println(entry.getKey()+" "+entry.getValue());
        });
    }

}

请找到演示项目https://github.com/BarathArivazhagan/Spring-Task-Sample/tree/master/Spring-SQLQueries-props