Mybatis Generator生成Mapper读写分离工具

通过Mybatis Generator生成Mapper java类和xml配置文件后,执行我的工具即可实现读写分离。

package org.rwsplit.main;

import java.io.Closeable;
import java.io.IOException;

public class Base {
	protected static void close(Closeable... closeAbles){
        if (closeAbles == null || closeAbles.length <= 0) {
            return;
        }
        for (Closeable closeAble : closeAbles) {
            if (closeAble != null) {
                try {
                    closeAble.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
package org.rwsplit.main;

import java.io.File;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;

import org.rwsplit.util.FileUtil;


public class RwSplit extends Base{
	static Properties ENVPRO = new Properties();
	static String FILENAME = "rwsplit.properties";
	static String JAVA_READ_HOME = "java.home";
	static String XML_READ_HOME = "xml.home";
	static String JAVA_EXT = "Mapper.java";
	static String XML_EXT = "Mapper.xml";
	
	public static void main(String[] args) {
		InputStreamReader envFr = null;
        FileInputStream fis = null;
        try {
        	fis = new FileInputStream(FILENAME);
            envFr = new InputStreamReader(fis,"UTF-8");
            ENVPRO.load(envFr);
            String javaHome = ENVPRO.getProperty(JAVA_READ_HOME);
            reJava(javaHome);
            String xmlHome = ENVPRO.getProperty(XML_READ_HOME);
            reXml(xmlHome);
        } catch (IOException e) {
        	e.printStackTrace();
        } finally {
            close(envFr,fis);
        }
	}
	
	private static void reJava(String javaHome) throws IOException {
		File file = new File(javaHome);
		File[] javaFile = file.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(JAVA_EXT) && !name.contains("Read") && !name.contains("Write");
			}
		});
		for(File java : javaFile){
			int index = java.getName().lastIndexOf(JAVA_EXT);
			File writeJava = new File(java.getParentFile(),"write/"+java.getName().substring(0, index)+"Write"+JAVA_EXT);
			FileUtil.copyFile(java, writeJava);
			replaceJavaContent(writeJava);
			File readJava = new File(java.getParentFile(),"read/"+java.getName().substring(0, index)+"Read"+JAVA_EXT);
			FileUtil.copyFile(java, readJava);
			replaceJavaContent(readJava);
			java.delete();
		}
	}
	
	private static void reXml(String xmlHome) throws IOException {
		File file = new File(xmlHome);
		File[] xmlFile = file.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(XML_EXT) && !name.contains("Read") && !name.contains("Write");
			}
		});
		for(File xml : xmlFile){
			int index = xml.getName().lastIndexOf(XML_EXT);
			File writeXml = new File(xml.getParentFile(),"write/"+xml.getName().substring(0, index)+"Write"+XML_EXT);
			FileUtil.copyFile(xml, writeXml);
			replaceNamespace(writeXml,"Write");
			File readXml = new File(xml.getParentFile(),"read/"+xml.getName().substring(0, index)+"Read"+XML_EXT);
			FileUtil.copyFile(xml, readXml);
			replaceNamespace(readXml,"Read");
			xml.delete();
		}
	}
	
	public static void replaceJavaContent(File java) throws IOException {
		String content = FileUtil.readTxtByNIO(30*1024, java);
		content = content.replaceFirst("(package .*?);", "$1."+(java.getName().contains("Read")?"read":"write")+";");
		String c2 = content.replaceFirst("public interface \\w+", "public interface "+java.getName().substring(0,java.getName().lastIndexOf(".")));
		FileUtil.writeFile(java, c2);
	}

	public static void replaceNamespace(File xml, String type) throws IOException {
		String content = FileUtil.readTxtByNIO(30*1024, xml);
		String c2 = content.replaceFirst("namespace=\"(.*?)\\.(\\w+)Mapper\"", "namespace=\"$1."+type.toLowerCase()+".$2"+type+"Mapper\"");
		FileUtil.writeFile(xml, c2);
	}


}
package org.rwsplit.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class FileUtil {

	public static void copyFile(File src, File dest) throws IOException {
		FileInputStream in = new FileInputStream(src);
		if (!dest.exists()){
			dest.getParentFile().mkdirs();
			dest.createNewFile();
		}
		FileOutputStream out = new FileOutputStream(dest);
		int c;
		byte buffer[] = new byte[1024];
		while ((c = in.read(buffer)) != -1) {
			for (int i = 0; i < c; i++)
				out.write(buffer[i]);
		}
		in.close();
		out.close();
	}

	public static void writeFile(File file,String content) throws IOException {
		file.delete();
		if (!file.exists()){
			file.getParentFile().mkdirs();
			file.createNewFile();
		}
		FileOutputStream out = new FileOutputStream(file, true);
		out.write(content.getBytes("utf-8"));
		out.close();
	}
	
	/**
	 * NIO读取文件
	 * 
	 * @param fin
	 * @return String
	 */
	public static String readTxtByNIO(int bufSize, File file){
		return readTxtByNIO(bufSize, file, "UTF-8");
	}
	
	/**
	 * NIO读取文件
	 * 
	 * @param fin
	 * @return String
	 */
	public static String readTxtByNIO(int bufSize, File file, String charset) {
		RandomAccessFile randomAccessFile = null;
		FileChannel fcin = null;
		StringBuilder strBuf = new StringBuilder();
		ByteBuffer rBuffer = ByteBuffer.allocate(bufSize);
		byte[] bs = new byte[bufSize];
		try {
			randomAccessFile = new RandomAccessFile(file, "r");
			fcin = randomAccessFile.getChannel();
			while (fcin.read(rBuffer) != -1) {
				int rSize = rBuffer.position();
				rBuffer.rewind();
				rBuffer.get(bs);
				rBuffer.clear();
				String tempString = new String(bs, 0, rSize, charset);
				strBuf.append(tempString);
			}
		}  catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fcin.close();
				randomAccessFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return strBuf.toString();
	}

}
java.home=C:/workspace2/apus/trunks/1.0/apus-web/src/main/java/com/odianyun/swift/apus/mapper
xml.home=C:/workspace2/apus/trunks/1.0/apus-web/src/main/resources/mapper

猜你喜欢

转载自jdkleo.iteye.com/blog/2318149