private void loadXmlResource() { // Spring may not know the real resource name so we check a flag // to prevent loading again a resource twice // this flag is set at XMLMapperBuilder#bindMapperForNamespace if (!configuration.isResourceLoaded("namespace:" + type.getName())) { String xmlResource = type.getName().replace('.', '/') + ".xml"; // #1347 InputStream inputStream = type.getResourceAsStream("/" + xmlResource); if (inputStream == null) { // Search XML mapper that is not in the module but in the classpath. try { inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource); } catch (IOException e2) { // ignore, resource is not required } } if (inputStream != null) { XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName()); xmlParser.parse(); } } }
public Configuration parseCustomMybatisXMLMappers(Configuration configuration) { if (getCustomMybatisXMLMappers() != null) // see XMLConfigBuilder.mapperElement() for (String resource : getCustomMybatisXMLMappers()) { XMLMapperBuilder mapperParser = new XMLMapperBuilder(getResourceAsStream(resource), configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } return configuration; }
cleanKeyGenerators(context.evalNodes("insert|update"), namespace); cleanSqlElement(context.evalNodes("/mapper/sql"), namespace); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(resource.getInputStream(), sqlSessionFactory.getConfiguration(), resource.toString(), sqlSessionFactory.getConfiguration().getSqlFragments());
private void mapperElement(XNode parent) throws Exception { /* * 定义集合 用来分类放置mybatis的Mapper与XML 按顺序依次遍历 */ if (parent != null) { //指定在classpath中的mapper文件 Set<String> resources = new HashSet<>(); //指向一个mapper接口 Set<Class<?>> mapperClasses = new HashSet<>(); setResource(parent, resources, mapperClasses); // 依次遍历 首先 resource 然后 mapper for (String resource : resources) { ErrorContext.instance().resource(resource); InputStream inputStream = Resources.getResourceAsStream(resource); //TODO XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } for (Class<?> mapper : mapperClasses) { //TODO configuration.addMapper(mapper); } } }
XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), configuration, mapperLocation.toString(), configuration.getSqlFragments()); xmlMapperBuilder.parse();
private void loadXmlResource() { // Spring may not know the real resource name so we check a flag // to prevent loading again a resource twice // this flag is set at XMLMapperBuilder#bindMapperForNamespace if (!configuration.isResourceLoaded("namespace:" + type.getName())) { String xmlResource = type.getName().replace('.', '/') + ".xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource); } catch (IOException e) { // ignore, resource is not required } if (inputStream != null) { XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName()); xmlParser.parse(); } } }
private void loadXmlResource() { // Spring may not know the real resource name so we check a flag // to prevent loading again a resource twice // this flag is set at XMLMapperBuilder#bindMapperForNamespace if (!configuration.isResourceLoaded("namespace:" + type.getName())) { String xmlResource = type.getName().replace('.', '/') + ".xml"; // #1347 InputStream inputStream = type.getResourceAsStream("/" + xmlResource); if (inputStream == null) { // Search XML mapper that is not in the module but in the classpath. try { inputStream = Resources.getResourceAsStream(type.getClassLoader(), xmlResource); } catch (IOException e2) { // ignore, resource is not required } } if (inputStream != null) { XMLMapperBuilder xmlParser = new XMLMapperBuilder(inputStream, assistant.getConfiguration(), xmlResource, configuration.getSqlFragments(), type.getName()); xmlParser.parse(); } } }
private void mapperElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { if ("package".equals(child.getName())) { String mapperPackage = child.getStringAttribute("name"); configuration.addMappers(mapperPackage); } else { String resource = child.getStringAttribute("resource"); String url = child.getStringAttribute("url"); String mapperClass = child.getStringAttribute("class"); if (resource != null && url == null && mapperClass == null) { ErrorContext.instance().resource(resource); InputStream inputStream = Resources.getResourceAsStream(resource); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } else if (resource == null && url != null && mapperClass == null) { ErrorContext.instance().resource(url); InputStream inputStream = Resources.getUrlAsStream(url); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments()); mapperParser.parse(); } else if (resource == null && url == null && mapperClass != null) { Class<?> mapperInterface = Resources.classForName(mapperClass); configuration.addMapper(mapperInterface); } else { throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one."); } } } } }
private void mapperElement(XNode parent) throws Exception { if (parent != null) { for (XNode child : parent.getChildren()) { if ("package".equals(child.getName())) { String mapperPackage = child.getStringAttribute("name"); configuration.addMappers(mapperPackage); } else { String resource = child.getStringAttribute("resource"); String url = child.getStringAttribute("url"); String mapperClass = child.getStringAttribute("class"); if (resource != null && url == null && mapperClass == null) { ErrorContext.instance().resource(resource); InputStream inputStream = Resources.getResourceAsStream(resource); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } else if (resource == null && url != null && mapperClass == null) { ErrorContext.instance().resource(url); InputStream inputStream = Resources.getUrlAsStream(url); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, url, configuration.getSqlFragments()); mapperParser.parse(); } else if (resource == null && url == null && mapperClass != null) { Class<?> mapperInterface = Resources.classForName(mapperClass); configuration.addMapper(mapperInterface); } else { throw new BuilderException("A mapper element may only specify a url, resource or class, but not more than one."); } } } } }
public static void main(String[] args) throws IOException { UnpooledDataSource dataSource = new UnpooledDataSource( "com.mysql.jdbc.Driver", "jdbc:mysql://localhost:3306/mybatis", "root", ""); TransactionFactory transactionFactory = new JdbcTransactionFactory(); Environment environment = new Environment("Java", transactionFactory, dataSource); Configuration configuration = new Configuration(environment); configuration.getTypeAliasRegistry().registerAliases("tk.mybatis.simple.model"); configuration.setLogImpl(Log4jImpl.class); InputStream inputStream = Resources.getResourceAsStream("tk/mybatis/simple/mapper/CountryMapper.xml"); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, "tk/mybatis/simple/mapper/CountryMapper.xml", configuration.getSqlFragments()); mapperParser.parse(); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration); SqlSession sqlSession = sqlSessionFactory.openSession(); try { List<Country> countryList = sqlSession.selectList("selectAll"); printCountryList(countryList); } finally { sqlSession.close(); } }
SqlSessionFactory sqlParse() throws Exception { Map<String, InputStream> sqls = MybatisSqlXmlUtils.openInputs(db); Set<Map.Entry<String, InputStream>> entries = sqls.entrySet(); for (Map.Entry<String, InputStream> entry : entries) { InputStream in = entry.getValue(); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, entry.getKey(), configuration.getSqlFragments()); xmlMapperBuilder.parse(); in.close(); } return this; }
private void loadXmlResource() { String xmlResource = type.getName().replace('.', '/') + ".xml"; Reader xmlReader = null; try { xmlReader = Resources.getResourceAsReader(type.getClassLoader(), xmlResource); } catch (IOException e) { // ignore, resource is not required } if (xmlReader != null) { XMLMapperBuilder xmlParser = new XMLMapperBuilder(xmlReader, assistant.getConfiguration(), xmlResource, sqlFragments, type.getName()); xmlParser.parse(); } }
private void addSqlMappings(Configuration conf, URL mapperFilePath) { InputStream is = null; try { is = mapperFilePath.openStream(); XMLMapperBuilder xmlParser = new XMLMapperBuilder(is, conf, mapperFilePath.toString(), conf.getSqlFragments()); xmlParser.parse(); } catch (IOException e) { logger.error("error loading mybatis mapping config file.", e); } finally { if(is != null){ try { is.close(); } catch (IOException e) { } } } }
public void loadMapper(String mapperName) { String configFile = configFilePath(mapperName); InputStream input = null; try { input = getClass().getResourceAsStream(configFile); checkArgument(input != null, format("Can not find mapper XML file %s", configFile)); new XMLMapperBuilder(input, conf, mapperName, conf.getSqlFragments()).parse(); loadAndConfigureLogger(mapperName); } catch (Exception e) { throw new IllegalArgumentException("Unable to load mapper " + mapperName, e); } finally { Closeables.closeQuietly(input); } }
/** * 生成ResultMap * * @param entity 实体 * @return */ public static void buildResultMap(Configuration configuration, Class<?> entity) { try { String resource = "buildDefaultResultMap:" + entity.getName(); InputStream inputStream = createResultMapInputStream(entity); XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(inputStream, configuration, resource, configuration.getSqlFragments()); xmlMapperBuilder.parse(); } catch (Exception e) { log.error(e.getMessage(), e); } }
protected void parseMybatisXmlMapping(Configuration configuration, String resource) { // see XMLConfigBuilder.mapperElement() XMLMapperBuilder mapperParser = new XMLMapperBuilder(getResourceAsStream(resource), configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); }
protected void parseMybatisXmlMapping(Configuration configuration, String resource) { // see XMLConfigBuilder.mapperElement() XMLMapperBuilder mapperParser = new XMLMapperBuilder(getResourceAsStream(resource), configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); }
@Override public ResultMap call() throws Exception { if ( !configuration.hasResultMap( token.getResultMapId() ) ) { try ( InputStream in = new ByteArrayInputStream( ResultMapBuilder .build( token, metadata ).getBytes() ) ) { new XMLMapperBuilder( in, configuration, token.getResultMapId(), configuration.getSqlFragments() ).parse(); } } return configuration.getResultMap( token.getResultMapId() ); } } );
public Configuration parseCustomMybatisXMLMappers(Configuration configuration) { if (getCustomMybatisXMLMappers() != null) // see XMLConfigBuilder.mapperElement() for (String resource : getCustomMybatisXMLMappers()) { XMLMapperBuilder mapperParser = new XMLMapperBuilder(getResourceAsStream(resource), configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } return configuration; }
protected Configuration parseCustomMybatisXMLMappers(Configuration configuration) { if (getCustomMybatisXMLMappers() != null) // see XMLConfigBuilder.mapperElement() for (String resource : getCustomMybatisXMLMappers()) { XMLMapperBuilder mapperParser = new XMLMapperBuilder(getResourceAsStream(resource), configuration, resource, configuration.getSqlFragments()); mapperParser.parse(); } return configuration; }