Mybatis学习之读取配置文件(三)

本文展示了一个Java程序,该程序使用MyBatis框架提供的Resources工具类来读取mybatis-config.xml配置文件和jdbc.properties属性文件,并将读取到的内容加载到Properties对象中。此外,还展示了如何对敏感信息进行简单的解密处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.learn.chapter2.test;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

import org.apache.ibatis.io.Resources;

public class ReadXMLandProperties {

	@SuppressWarnings("unused")
	public static void main(String[] args) {
		InputStream cfgStream = null;
		Reader cfgReader = null;
		
		InputStream proStream = null;
		Reader proReader = null;
		
		Properties properties = null;
		
		try{
			//读取配置文件
			cfgStream = Resources.getResourceAsStream("mybatis-config.xml");
			cfgReader = new InputStreamReader(cfgStream);
			
			
			//读取属性文件
			proStream = Resources.getResourceAsStream("jdbc.properties");
			proReader = new InputStreamReader(proStream);
			
			properties.load(cfgStream);
			
			//解密为明文
			properties.setProperty("username", decode(properties.getProperty("username")));
			properties.setProperty("password", decode(properties.getProperty("password")));
			
		}catch (Exception e) {
			// TODO: handle exception
		}
				
		
	}
	
	private static String decode(String str){
		//解密
		return str;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值