mybatis读取properties文件内容

今天在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()即可

猜你喜欢

转载自blog.csdn.net/fearlessnesszhang/article/details/80482745