根据mybatis-generator自动生成的mapper来逆向生成数据库sql文件

版权声明:本文为博主原创文章,未经博主允许不得转载。你想转载请附加连接哦 https://blog.csdn.net/dmw412724/article/details/82256371
public class Demo {
	
	public static void main(String[] args) throws Exception {
		generateSql("F://cc", "F://cc/aaa.sql");
	}
	/**
	 * 生成sql
	 * @param dirPath mapper.xml的父级文件夹
	 * @param sqlFile 选择你将要生成sql的文件
	 * @throws IOException
	 */
	private static void generateSql(String dirPath,String sqlFile) throws IOException{
		FileWriter fw = null;
		try {
			File dir = new File(dirPath);
			File sql = new File(sqlFile);
			if (sql.exists()){
				sql.delete();
			}
			sql.createNewFile();
			fw = new FileWriter(sql);
			
			if (dir.exists() && dir.isDirectory()){
				File[] files = dir.listFiles();
				for (File file : files) {
					if (file.isFile() && file.getName().endsWith(".xml")){
						System.out.println(file.getName());
						fw.append("\r\n");
						fw.append(getSql(file));
						fw.flush();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if (fw != null)
			fw.close();
		}
	}
	
	private static String getSql(File xmlfile) throws DocumentException{
		SAXReader saxReader= new SAXReader();
		Document document = saxReader.read(xmlfile);
		Element root = document.getRootElement();
		Element resultMap = root.element("resultMap");
		Tab tab = new Tab();
		tab.setTableName(getTableName(root));
		tab.setColumns(getColumns(resultMap));
		return tab.toString();
	}
	private static Map<String,String> getColumns(Element resultMap){
		List<Element> elements = resultMap.elements();
		Map<String,String> map = new LinkedHashMap<String, String>();
		for (Element element : elements) {
			map.put(element.attribute("column").getValue(), element.attribute("jdbcType").getValue());
		}
		return map;
	}
	private static String getTableName(Element root){
		Element selectByPrimaryKey = root.element("select");
		String selectStr = selectByPrimaryKey.getTextTrim();
		String tableName = selectStr.split("from")[1].trim().split(" ")[0].trim();
		return tableName;
	}
}
public class Tab {
	
	private String tableName;
	
	private String idName;
	
	private String idType;
	
	private Map<String,String> columns;
	
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public String getIdName() {
		return idName;
	}
	public void setIdName(String idName) {
		this.idName = idName;
	}
	public String getIdType() {
		return idType;
	}
	public void setIdType(String idType) {
		this.idType = idType;
	}
	public Map<String, String> getColumns() {
		return columns;
	}
	public void setColumns(Map<String, String> columns) {
		this.columns = columns;
	}
	@Override
	public String toString() {
		
		StringBuffer sb = new StringBuffer("DROP TABLE  IF EXISTS "+tableName+";");
		sb.append("\r\n");
		sb.append("CREATE TABLE ");
		sb.append(tableName);
		sb.append("(");
		sb.append("\r\n");
		Set<Entry<String,String>> set = columns.entrySet();
		int size = set.size();
		int i = 0;
		for (Entry<String,String> entry : set) {
			i++;
			sb.append("\t"+entry.getKey()+" "+format(entry.getValue()));
			if (size != i){
				sb.append(" ,");
				sb.append("\r\n");
			}
			
		}
		sb.append("\r\n");
		sb.append(");");
		sb.append("\r\n");
		return sb.toString();
		
	}
	private String format(String val){
		if (val.equals("VARCHAR")){
			return "VARCHAR(255)";
		}
		return val;
	}
	
	
}

猜你喜欢

转载自blog.csdn.net/dmw412724/article/details/82256371