通过实体中@TableName(“test“)找到项目中所用的表

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.text.StrFormatter;
import cn.hutool.core.text.StrJoiner;

import java.io.File;
import java.io.FileFilter;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

public class Test {

	public static void main(String[] args) {
		String rootPath = "E:/work/test/src/main/java/com/bjs/ceshi/entity";  //实体类路径
		
		List files = FileUtil.loopFiles(FileUtil.file(rootPath), -1, new FileFilter() {
			@Override
			public boolean accept(File pathname) {
				return pathname.getName().endsWith("DO.java");   //实体类结尾名
			}
		});
		
		List lines = new ArrayList<>();
		files.forEach( file -> {
			List oriLines = FileUtil.readLines(file, Charset.forName("utf-8"));
			for (int i = 0; i < oriLines.size(); i++) {
				String line = oriLines.get(i);
				if(line.contains("@TableName(\"") && line.contains("\")")) {
					String tmp = line.replace("@TableName(\"", "").replace("\")", "");
					lines.add(tmp);
					System.out.println(tmp);
				}
			}
		});
		
		System.out.println();
		String sqlStr = "SELECT TABLE_NAME AS 表名,TABLE_COMMENT AS 表注释 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'mall_test' and TABLE_NAME in ({});";
		if (lines.size() > 0) {
			System.out.println(StrFormatter.format(sqlStr, StrJoiner.of("','", "'", "'").append(lines)));
		}
	}
}

你可能感兴趣的:(java)