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