对于resource引入配置文件 以及 使用property子标签的使用都是会的
但是我希望引入多个properties文件
请问怎么办
mybatis 不支持配置文件中指定多个 resources
标签,如果想引入多个 proprties 文件,可以先将多个 properties 文件解析为 Properties 实例,然后再将解析出的 Properties 键值对合并到一个 Properties 作为构建 SqlSessionFactory 的参数,示例代码如下。
public class Test {
public static void main(String[] args) throws IOException {
Properties properties1 = new Properties();
properties1.load(Test.class.getResourceAsStream("/mybatis1.properties"));
Properties properties2 = new Properties();
properties2.load(Test.class.getResourceAsStream("/mybatis2.properties"));
Properties properties = new Properties();
properties1.forEach((key, value) -> properties.setProperty((String) key, (String) value));
properties2.forEach((key, value) -> properties.setProperty((String) key, (String) value));
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"), properties);
}
}