List of usage examples for org.apache.ibatis.type TypeAliasRegistry registerAlias
public void registerAlias(String alias, String value)
From source file:org.flowable.common.engine.impl.AbstractEngineConfigurator.java
License:Apache License
protected void registerCustomMybatisMappings(AbstractEngineConfiguration engineConfiguration) { String cfgPath = getMybatisCfgPath(); if (cfgPath != null) { Set<String> resources = new HashSet<>(); ClassLoader classLoader = engineConfiguration.getClassLoader(); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); }// w w w. j a v a 2 s . c o m List<MybatisTypeAliasConfigurator> typeAliasConfigurators = new ArrayList<>(); List<MybatisTypeHandlerConfigurator> typeHandlerConfigurators = new ArrayList<>(); try (InputStream inputStream = classLoader.getResourceAsStream(cfgPath)) { DocumentBuilderFactory docBuilderFactory = createDocumentBuilderFactory(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document document = docBuilder.parse(inputStream); NodeList typeAliasList = document.getElementsByTagName("typeAlias"); for (int i = 0; i < typeAliasList.getLength(); i++) { Node node = typeAliasList.item(i); MybatisTypeAliasConfigurator typeAlias = new MybatisTypeAliasConfigurator() { @Override public void configure(TypeAliasRegistry typeAliasRegistry) { try { typeAliasRegistry.registerAlias( node.getAttributes().getNamedItem("alias").getTextContent(), Class.forName(node.getAttributes().getNamedItem("type").getTextContent())); } catch (Exception e) { throw new FlowableException("Failed to load type alias class", e); } } }; typeAliasConfigurators.add(typeAlias); } NodeList typeHandlerList = document.getElementsByTagName("tagHandler"); for (int i = 0; i < typeHandlerList.getLength(); i++) { Node node = typeHandlerList.item(i); MybatisTypeHandlerConfigurator typeHandler = new MybatisTypeHandlerConfigurator() { @Override public void configure(TypeHandlerRegistry typeHandlerRegistry) { try { typeHandlerRegistry.register( node.getAttributes().getNamedItem("javaType").getTextContent(), node.getAttributes().getNamedItem("handler").getTextContent()); } catch (Exception e) { throw new FlowableException("Failed to load type handler class", e); } } }; typeHandlerConfigurators.add(typeHandler); } NodeList nodeList = document.getElementsByTagName("mapper"); for (int i = 0; i < nodeList.getLength(); i++) { Node node = nodeList.item(i); resources.add(node.getAttributes().getNamedItem("resource").getTextContent()); } } catch (IOException e) { throw new FlowableException("Could not read IDM Mybatis configuration file", e); } catch (ParserConfigurationException | SAXException e) { throw new FlowableException("Could not parse Mybatis configuration file", e); } if (typeAliasConfigurators.size() > 0) { if (engineConfiguration.getDependentEngineMybatisTypeAliasConfigs() == null) { engineConfiguration.setDependentEngineMybatisTypeAliasConfigs(typeAliasConfigurators); } else { engineConfiguration.getDependentEngineMybatisTypeAliasConfigs().addAll(typeAliasConfigurators); } } if (typeHandlerConfigurators.size() > 0) { if (engineConfiguration.getDependentEngineMybatisTypeHandlerConfigs() == null) { engineConfiguration.setDependentEngineMybatisTypeHandlerConfigs(typeHandlerConfigurators); } else { engineConfiguration.getDependentEngineMybatisTypeHandlerConfigs() .addAll(typeHandlerConfigurators); } } if (engineConfiguration.getCustomMybatisXMLMappers() == null) { engineConfiguration.setCustomMybatisXMLMappers(resources); } else { engineConfiguration.getCustomMybatisXMLMappers().addAll(resources); } } }