实战:mapper文件转sql语句

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_15153911/article/details/88706667

实战:mapper文件转sql语句

实战:mapper文件转sql语句

最近看码云的项目,优秀的开源项目,有表结构sql,有说明文档,也不妨有些半开源的项目,有实体类,就没有sql文件。
咋怎?你想orm层与数据库交互,那orm的框架必然与数据库有关系。今天就做一个mybatis的mapper文件转为sql语句。

思路:
1、遍历某个文件下的所有xml文件
2、找出resultMap的字符串
3、截取含有column="user_id"的字符串
4、拼接成sql语句

在这里插入图片描述
第一部分,遍历某个文件夹下获取后缀为xx的文件

String path = "D://";
			File file = new File(path);
			if (file.exists()) {
				File[] files = file.listFiles();
				if (files.length == 0) {
					System.out.println("文件夹是空的!");
					return;
				} else {
					for (File file2 : files) {
						if (file2.isDirectory()) {
//							System.out.println("文件夹:" + file2.getAbsolutePath());
//							traverseFolder2(file2.getAbsolutePath());
						} else {
//							System.out.println("文件:" + file2.getAbsolutePath());
							String xml = _txtUtils.readTxtFile(file2.getAbsolutePath());
							String tableName = getXmlToTable(xml.trim());
							int a = xml.indexOf("<id");
							int b = xml.indexOf("</resultMap>");
							xml = xml.substring(a,b);
//							System.out.println(xml.trim());
							changeXmlToSql(xml.trim(), tableName);
							System.out.println("");
						}
					}
				}
			} else {
				System.out.println("文件不存在!");
			}

上面的代码是没完善的,如xx.contains(".xml")

第二部分,整理成字段的形式

String[] xmls = xml.split("/>");
		String[] strs = new String[xmls.length];
		for(int i = 0;i <xmls.length; i++){
//			System.out.println(xmls[i]);
			List<String> columns = _pachongUtils.getCode(xmls[i],"a","column=\"(.+?)\"");

			if(columns.size()>0){
				String colcumn = columns.get(0);
				if(xmls[i].contains("<id")){
					strs[i] = colcumn+",1,11,-1,id";
				}else{
					strs[i] = colcumn+",2,255,1,"+colcumn;
				}
			}
		}

代码下载:http://47.98.237.162/detail/1/198

猜你喜欢

转载自blog.csdn.net/sinat_15153911/article/details/88706667