在分析 MyBatis XML 配置文件 properties 元素时提到了三种配置方式,其中 property 子元素 和 properties 文件都比较容易理解,但是为什么还要提供一种代码参数传递的方式呢?
假设一种使用场景,生产环境的数据库联系方式是加密的,故需要 jdbc.properties 文件中以密文的形式保存,而 MyBatis 默认不支持直接解密读取,此时就需要程序进行解密读取。
1 2 3 4 5 6 7 8 9 10
| String configResource = "mybatis-config.xml"; InputStream configInputStream = Resources.getResourceAsStream(configResource);
大专栏 MyBatis XML 配置文件 properties 元素扩展>String propertiesResource = "jdbc.properties"; InputStream propertiesInputStream = Resources.getResourceAsStream(propertiesResource); Properties properties = new Properties(); properties.load(propertiesInputStream);
properties.setProperty(decode(properties.getProperty("key"))); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream, properties);
|
实现解密方法:
1 2 3
| private String decode(String value) { // TODO }
|
这里只是介绍一种使用思路