mybatis讀取properties檔案內容

fearlessnesszhang發表於2018-05-28

今天在mybatis中想讀取檔案內容,一開始是建立的xml檔案,發現必須要表頭,不然就是<beans>;完全不適用,然後突然靈光一現,可以建立properties檔案,讀取檔案,通過鍵值對獲取內容。

Properties proper = new Properties();
proper.load(this.getClass().getResourceAsStream("/xx.properties"));//讀取檔案,我儲存檔案的位置在src/main/resource
Map<String, String> map = new HashMap<String, String>();
Iterator<Object> keys = proper.keySet().iterator();
    while (keys.hasNext()) {//遍歷檔案內容
        String key = (String) keys.next();
        if (proper.getProperty(key) != null) {
            map.put(key, proper.getProperty(key).toString());//儲存在map中
            }
        }
結果為:a=1,b=2  想要獲取a的值:map.get("a").toString()即可

相關文章