TransController { @RequestMapping(value = "/toDas", method = RequestMethod.POST) public ServiceResult<String> convert(@RequestBody TransRequest transRequest, @CurrentUser LoginUser user, Errors errors) { try { Configuration configuration = createConfiguration(); InputStream inputStream = new ByteArrayInputStream(transRequest.getXmlContent().getBytes(StandardCharsets.UTF_8)); XMLMapperBuilder mapperParser = new XMLMapperBuilder(inputStream, configuration, "any.xml", configuration.getSqlFragments()); mapperParser.parse(); Collection<MappedStatement> statements = uniqueStatements(mapperParser.getConfiguration().getMappedStatements()); List<Statement> convertedStatements = newArrayList(); VelocityContext context = new VelocityContext(); Set<String> importResultMaps = importResultMaps(configuration); context.put("importResultMaps", importResultMaps); context.put("classComments", classComments(importResultMaps)); context.put("className", createClassName(mapperParser)); context.put("statements", convertedStatements); for(MappedStatement statement : statements) { if (statement.getSqlCommandType() == SELECT || statement.getSqlCommandType() == INSERT || statement.getSqlCommandType() == UPDATE || statement.getSqlCommandType() == DELETE) { try{ SqlSource sqlSource = statement.getSqlSource(); ArrayList<Seg> segs = new ArrayList<>(); LinkedHashMap<String, String> params = newLinkedHashMap(); Class parameterType = statement.getParameterMap().getType(); StringBuilder comments = new StringBuilder(); if(statement.isUseCache()) { } if(sqlSource instanceof RawSqlSource) { List<ParameterMapping> parameterMappings = get(get(sqlSource, "sqlSource"), "parameterMappings"); LinkedHashMap<String, String> parsedParams = getParams(parameterMappings); StaticTextSqlNode node = new StaticTextSqlNode(get(get(sqlSource, "sqlSource"), "sql")); walkNodes(node, configuration, statement, segs, new AtomicInteger(0), params, newLinkedList(), parsedParams, parameterType, comments, Sets.newHashSet()); } else { walkNodes(get(sqlSource, "rootSqlNode"), configuration, statement, segs, new AtomicInteger(0), params, newLinkedList(), newLinkedHashMap(), parameterType, comments, Sets.newHashSet()); } String resultType = getResultType(statement).getSimpleName(); if(parameterType != null){ params.clear(); params.put("p" + parameterType.getSimpleName(), parameterType.getName()); } Class resultClass = getResultType(statement); if(statement.getSqlCommandType() == SELECT ){ segs.add(new Seg("sqlBuilder." + getIntoType(resultClass) + ";")); } Statement converted = new Statement() .setId(statement.getId()) .setMethodBody(join(segs)) .setMethodType(statement.getSqlCommandType() == SELECT ? "query" : "update") .setResultType(statement.getSqlCommandType() == SELECT && !isPrimitiveType(resultClass) ? "List<" + resultType + ">" : resultType) .setParams(params) .setComments(comments.toString()); convertedStatements.add(converted); }catch (Exception e){ Statement converted = new Statement() .setId(statement.getId()) .setFail(true); convertedStatements.add(converted); e.printStackTrace(); } } } String finalResult = mergeTemplate(context); String formattedSource = finalResult; try { formattedSource = new Formatter().formatSource(finalResult); } catch (FormatterException fe) { fe.printStackTrace(); } return ServiceResult.success(formattedSource); }catch (Exception e){ e.printStackTrace(); return ServiceResult.fail(StringUtil.getMessage(e)); } }  @RequestMapping(value = "/toDas", method = RequestMethod.POST) ServiceResult<String> convert(@RequestBody TransRequest transRequest, @CurrentUser LoginUser user, Errors errors);  }
@Test public void getConvertCode() throws Exception{ TransController controller = new TransController(); TransRequest transRequest = new TransRequest(); URL url = TransToDasControllerTest.class.getResource("/mybatisMapperTest.xml"); transRequest.setXmlContent(Resources.toString(url, StandardCharsets.UTF_8)); ServiceResult<String> result = controller.convert(transRequest, null, null); System.out.println(result.getMsg()); URL url2 = TransToDasControllerTest.class.getResource("/expectedMybatisCode"); String expected = Resources.toString(url2, StandardCharsets.UTF_8); Assert.assertEquals(expected, result.getMsg()); }