首页 > 数据库 >mybatis在读取配置文件,创建SqlSessionFactory对象时。需要对密码进行解密,再连接数据库,该如何操作。

mybatis在读取配置文件,创建SqlSessionFactory对象时。需要对密码进行解密,再连接数据库,该如何操作。

时间:2023-12-27 16:13:32浏览次数:41  
标签:xml SqlSessionFactory resource 配置文件 inputStream sqlSession mybatis null newFile

1、我看了网上很多的方法,主要是说通过新建一个jdbc.properties来获取数据库

连接池。但是我试了并没有作用。

 

2、解决办法:简单粗暴。先用后代码读取原有的mybatis-configuration.xml,

然后将密码进行解密后,设值到xml中。最后把解密的xml作为源。

去生成一份新的xml文件,把心的xml文件来生成SqlSessionFactory就可以了。

public static void init() {
		login();
		
		String resource = "mybatis-configuration.xml";
        InputStream inputStream = null;
        File newFile = null;
		try {
			newFile = getValue(resource);
			inputStream = new FileInputStream(newFile);
			SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder()
	                .build(inputStream);
	        sqlSession = sessionFactory.openSession();
//	        personInfoMapper = sqlSession.getMapper(PersonInfoMapper.class);
	        pScheduleTaskMapper = sqlSession.getMapper(PScheduleTaskMapper.class);
	        sqlSession.clearCache();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(inputStream != null){
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				newFile.delete();
			}
		}
}

  

private static File getValue(String resource) throws XmlException, IOException{
		Resources.getResourceAsStream(resource);
		InputStream inputStream = null;
		try{
			inputStream = Resources.getResourceAsStream(resource);
			XmlObject xmlObject = XmlObject.Factory.parse(inputStream);
			Node node = xmlObject.getDomNode();
			NodeList nodeList = node.getChildNodes();
			if(nodeList != null){
				int length = nodeList.getLength();
				for(int index =0;index<length;index++){
					Node nodeIndex = nodeList.item(index);
					String localName = nodeIndex.getLocalName();
					if("configuration".equals(localName)){
						NodeList nodeList2 = nodeIndex.getChildNodes();
						int length2 = nodeList2.getLength();
						for(int index2 =0;index2<length2;index2++){
							Node nodeIndex2 =nodeList2.item(index2);
							String localName2 = nodeIndex2.getLocalName();
							if("environments".equals(localName2)){
								NodeList nodeList3 = nodeIndex2.getChildNodes();
								int length3 = nodeList3.getLength();
								for(int index3 = 0;index3<length3;index3++){
									Node nodeIndex3 = nodeList3.item(index3);
									String localName3 = nodeIndex3.getLocalName();
									if("environment".equals(localName3)){
										NodeList nodeList4 = nodeIndex3.getChildNodes();
										int length4 = nodeList4.getLength();
										for(int index4=0;index4<length4;index4++){
											Node node4 = nodeList4.item(index4);
											String localName4 = node4.getLocalName();
											if("dataSource".equals(localName4)){
												NodeList nodeList5 = node4.getChildNodes();
												int length5 = nodeList5.getLength();
												for(int index5 =0 ;index5<length5;index5++){
													Node node5 = nodeList5.item(index5);
													String localName5 = node5.getLocalName();
													if("property".equals(localName5) ){
														boolean isFlag = false;
														NamedNodeMap attributes = node5.getAttributes();
														for (int j = 0; j < attributes.getLength(); j++) {
									                        Node attribute = attributes.item(j);
									                        String attributeName = attribute.getNodeName();
									                        String value = attribute.getNodeValue();
									                        if(isFlag){
									                        	attribute.setNodeValue(AES256Util.decrypt(value));
									                        }
									                        if("name".equals(attributeName) && "password".equals(value)){
									                        	isFlag = true;
									                        }
									                    }
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			File folder = new File("C:\\temp");
			if(!folder.exists()){
				folder.mkdir();
			}
			File file = new File("C:\\temp\\mybatis-configuration.xml");
			xmlObject.save(file);
			return file;
		}catch(Exception e){
			throw new RuntimeException(e);
		}finally {
			if(inputStream != null){
				inputStream.close();
			}
		}
	}

  

3、最后:记得代码删除新生成的文件。

标签:xml,SqlSessionFactory,resource,配置文件,inputStream,sqlSession,mybatis,null,newFile
From: https://www.cnblogs.com/wwssgg/p/17930761.html

相关文章

  • 简易mybatis实现
    packagecom.ibatis.io.entity;​​publicclassUser{ privateIntegerid; privateStringname;​ privateIntegerage;​ publicIntegergetId(){   returnid; }​ publicvoidsetId(Integerid){   this.id=id; }​ ......
  • mybatisplus-批量更新
    1.<updateid="updateBatch"parameterType="java.util.List"><foreachcollection="list"item="item"index="index"separator=";">UPDATEenterprise_oil_adjust_recordSET......
  • grep -Ev '#|^$' zabbix_server.conf 显示配置文件 命令
    grep-Ev'#|^$'zabbix_server.confroot@k8s-node01:/etc/zabbix#grep-Ev'#|^$'zabbix_server.confLogFile=/var/log/zabbix/zabbix_server.logLogFileSize=0PidFile=/run/zabbix/zabbix_server.pidSocketDir=/run/zabbixDBName=zabbixDBUser=......
  • 数据库清空,镜像编译,docker配置文件及服务启动完整流程
    前言:当数据库表发生变更的时候。 要做的事情:一、更改数据库表#删除表,更改数据库表(表结构改变)droptableifexistssrc20_v3;droptableifexistssrc20_valid_v4;droptableifexistssrc20_tick_v4;droptableifexistssrc20_mint_progress_v4;droptableifex......
  • JavaWeb - Day11 - 案例 - 员工管理、文件上传、修改员工、配置文件
    01.案例-员工管理-新增员工前面我们已经实现了员工信息的条件分页查询以及删除操作。关于员工管理的功能,还有两个需要实现:新增员工修改员工首先我们先完成"新增员工"的功能开发,再完成"修改员工"的功能开发。而在"新增员工"中,需要添加头像,而头像需要用到"文件上传"技......
  • mybatis的一级、二级缓存
    Mybatis的一级、二级缓存一级缓存:基于PerpetualCache的HashMap本地缓存,其存储作用域为Session,当Session进行flush或close之后,该Session中的所有Cache就将清空,默认打开一级缓存二级缓存是基于namespace和mapper的作用域起作用的,不是依赖于SQLsession,默认也是采用PerpetualCach......
  • MyBatis batchInsert 批量插入数据
    mybatis是一个Java的持久层框架,它支持定制化SQL、存储过程以及高级映射。通过MyBatis,开发者可以直接编写原生态SQL,避免了JDBC代码的繁琐。如何在MyBatis中进行批量插入操作。下面是一个简单的例子,说明如何在MyBatis中配置和使用批量插入:Mapper接口:publicinter......
  • 工作上使用mybatisplus时遇到的问题
    报错:org.apache.ibatis.session.Configuration.getLanguageDriver(Ljava/lang/Class;)原因:依赖低版本导致了了冲突,我们改为高版本即可思路1.根据引起问题的提示中的“org.apache.ibatis.session.Configuration.getLanguageDriver”,看看能否找到对应的类,如下所示:(shitf双击然后查......
  • @ConfigurationProperties(prefix = “xx.xx.xx“) 从配置文件中取值赋给类的属性
    @ConfigurationProperties(prefix=“xx.xx.xx“)从配置文件中取值赋给类的属性@ConfigurationProperties(prefix=“xx.xx.xx”)该注解的作用是从配置文件中取值赋给类的属性,当然也可以为方法的变量赋值/***服务访问URL*/@Component@ConfigurationProperties(value......
  • MyBatisPlus简介及快速搭建
    一、简介MyBatisPlus(简称MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强,不做改变,为简化开发,提高效率而生。特性及官网链接(简称苞米豆):可在IDEA中安装以下插件:MybatisX:支持跳转,自动补全生成SQL;dynamic-datasource:基于SpringBoot的多数据源组件,功能强悍,支持Seat......